Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


restart nginx -- 07.02.2021 %

Normally I use

$ sudo systemctl restart nginx

but recently I had to take more drastic measures.

sudo pkill -f nginx & wait $!
sudo systemctl start nginx

source – stackoverflow

\- [ nginx ]

run changed tests -- 01.02.2021 %

npm run test – $(git diff master –name-only | rg ’test.ts’) –coverage=false

\- [ npm, git ]

strip audio from video file -- 09.01.2021 %

Easy way to remove audio from a video file using ffmpeg

ffmpeg -i $input_file -c copy -an $output_file

source: superuser

\- [ ffmpeg ]

gzipping an existing tar -- 14.10.2020 %

Part of my work process is taking lots of screenshot, ~5 per day. Then I back them up in AWS S3 Glacier once a month, using freeze app. Like to start with creating a regular tar file in /tmp.

$ tar cvf /tmp/pic_dump_14_10_20.tar ~/Desktop/**/*.png

Then append few more images. r in this case sanding for append..

$ tar rfv /tmp/pic_dump_14_10_20.tar ~/Pictures/resized/*

Now that the tar is complete I double check it by listing the files.

$ tar tf /tmp/pic_dump_14_10_20.tar

Lastly I need to compress the tar and I was confused if I could use tar command itself to compress a tar into a tar.gz but turns you use gunzip.

$ gzip /tmp/pic_dump_14_10_20.tar

source: https://alvinalexander.com/blog/post/linux-unix/how-work-files-tar-gzip-tgz/ (4)

\- [ tar, gzip ]

tmux plus screen -- 17.08.2020 %

Recently I was sshed into my home pi server trying to sftp some big files from a remote server. Some of the transfers are huge, 30gb+. On our internet that will take a while. Not wanting to leave the shh terminal session open that whole time I used screen on the pi. Idea was to create a screen session start the transfer detach and comeback few hours later. Detaching was a bit tricky. <Ctrl><a> <d> is the default detach command for both tmux running on my mac and screen running on the pi. So when I tried to detach from the screen session tmux would detach instead. 😡

After messing with configs and some searching turns out if you press the tmux prefix key twice it sends it once to the child shell. So eventually I was able to detach from the screen session with:

<Ctrl><a> <Ctrl><a> <d>!!

sources:

\- [ screen, tmux ]

npm i vs npm ci -- 17.08.2020 %

Today I discovered npm ci from a colleague. It does a clean install wiping out the node_modules before installing. For me this is perfect because I often find myself doing

$ rm -rf node_modules && npm i

no need just run npm ci

*note: you’ll still want to do npm i when installing packages as npm ci does not update package*..

source:

\- [ npm, js ]

prevent vim auto new lines -- 13.08.2020 %

Sometimes when typing vim will automatically start a newline. This is an expected behavior but at times can be really annoying. Ex working with macros, you can recored one on a short line that breaks on longer lines 😟. The amount of text before vim will break to new line while typing is controlled via the textwidth setting. So a fix is pretty simple. If don’t want the behavior just set textwidth to a big number. ex:

: set tw=500

Here is a asciicast of the problem and solution in action:

asciicast

demo of setting tw

source – https://stackoverflow.com/questions/1272173/in-vim-how-do-i-break-one-really-long-line-into-multiple-lines

\- [ vim ]

auto find ssh keys -- 12.08.2020 %

I use to always pass a key when sshing ex:

$ ssh -i ~/.ssh/de2 travis@vxxxxxxxxxxxxxxxxxxx.megasrv.de

That can be a bit annoying. I know two fixes:

add private key to ssh-agent

$ ssh-add ~/.ssh/de2

Now when you ssh you need not include the -i ~/.ssh/de2 because the ssh-agent will find it automatically.

*note: this resets once you reboot

configure individual host

You can configure individual hosts to use a spefic private key my editing your ~/.ssh/config:

Host de2
    HostName vxxxxxxxxxxxxxxxxxxxx.megasrv.de
    User travis
    IdentityFile ~/.ssh/de2

Now you only need to

$ ssh de2

source – https://www.techrepublic.com/article/how-to-use-per-host-ssh-configuration/

\- [ ssh ]

using short server names -- 12.08.2020 %

It use to be when I was trying to ssh into one of my servers I would just: in the shell to get my fzz backwards command search, type ssh, then scroll up and down until I found the correct server. As my number of servers has grown this is no longer manageable because I don’t remember which IPs align with with server. Time for some human readable names.

By adding something like this to your ~/.ssh/config:

Host de1
    HostName vxxxxxxxxxxxxxxxxxxxx.megasrv.de
    User travis

Host de2
    HostName vyyyyyyyyyyyyyyyyyyyymegasrv.de
    User travis

Host de3
    HostName vzzzzzzzzzzzzzzzzzzz.megasrv.de
    User travis

Host nyc1
    HostName 123.123.123.1233
    User travis

sshing becomes as easy as:

$ ssh de2

source – https://www.techrepublic.com/article/how-to-use-per-host-ssh-configuration/

\- [ ssh ]

disable user -- 11.08.2020 %

In this case disabling the user named ubuntu from logging in. This includes logging in via ssh.

$ sudo usermod --expiredate 1 ubuntu
\- [ sysadmin ]