Commands

From James Van Dyne
Jump to navigation Jump to search

A collection of commands that are useful and I never seem to remember off the top of my head.

Curl[edit]

File Upload[edit]

  • curl -i -X POST -H "Content-Type: multipart/form-data" -F "file=@tanzawa.png" http://127.0.0.1:8000/files/upload

SSH[edit]

Socks Proxy[edit]

Setup a poor-man's secure tunnel and proxy traffic over ssh to a remote server. Useful when needing to access corporate networks without the benefit of a fully fledged VPN.

  • ssh -N -D 8080 [USER]@[SERVER_IP]

After setting up the tunnel, you need to setup your system to use the proxy. System Preferences -> Network -> Advanced -> Proxies

Socks proxy settings.png
Tunnel[edit]

Tunnel a remote port to a local port on your machine. Useful for accessing remote ports (e.g. databases) as if they were running on localhost.

  • ssh -L <locally accessible port>:<ip address to bind to>:<remote port> hoge@host.example.com
  • example: ssh -L 5434:127.0.0.1:5432 hoge@host.example.com

SED[edit]

Find/Replace[edit]

Recursively find and replace all instances foo and replace it with hoge, excluding the migrations directory. Using ripgrep because it's fast / ignores files in your .gitignore. Does not create a backup of modified files because, you're using git, right?!

Command is for FreeBSD sed, as found on macOS.

  • rg 'foo' -g '!migrations/'  --files-with-matches -0 | xargs -0 sed -i '' 's/foo/hoge/g'

Image[edit]

Gif[edit]

Create a gif from a .mov (screen recording with Quicktime player). ffmpeg to split into frames -> ImageMagick convert -> gifsicle to optimize

ffmpeg -y -i input.mov -f image2pipe -vcodec ppm - | convert -delay 2 -loop 0 -layers Optimize - gif:- | gifsicle -d 3 -O3 -o optimized.gif

Batch Resize Directory[edit]

Depends on ImageMagick

mogrify -resize 66x66 -path /OUTPUT_PATH/ /INPUT_PATH/*.jpg

Docker[edit]

I always forget docker commands.

Recreate a Single Container[edit]
$ docker-compose up --build --force-recreate --no-deps <container_name>
Backup Named Volume[edit]

Shutdown all containers that are using the volume before starting.

  1. Create a directory to store your backup.
  2. Create (and removes) a temporary container that mounts the volumes for an existing container (e.g. your db container) and then tars a specific directory to a backup file.
  3. Move db.tar to the desired host.
$ mkdir ~/backup
$ docker run --rm --volumes-from my_existing_container -v ~/backup:/backup ubuntu bash -c "cd /var/opt/mssql && tar cvf /backup/db.tar ."
Restore Named Volume[edit]

Make sure the volume doesn't exist locally on the new host.

  1. Create an empty volume with a name that matches your final volume name.
  2. Create (and remove) a container that mounts:
    1. Your named volume
    2. Your local directory where your .tar backup file is located
  3. Extract the backup tar into the named volume
$ docker volume create my_volume_name
$ docker run --rm -v my_volume_name:/recover -v ~/Downloads:/backup ubuntu bash -c "cd /recover && tar xvf /backup/db.tar"