Stupid Bash Tricks

cd -

Go back to the previous directory (“OLDPWD”)

$ cd ~/super/incredibly/long/path/goes/here
$ pwd
$ … some operations ...
$ cd /some/other/place
$ cd -

alt+

Reuse your last command line argument. Repeat to cycle past arguments.

$ mkdir awesome-directory-with-a-really-long-name
$ cd <alt+.>
$ cd -
$ rmdir <alt+.><alt+.>

Bang Command

Reuse your last command by name. History shortcut.

$ docker compose up
$ vim Dockerfile
$ rm -rf coverage-reports/
$ echo “Friday afternoons are super productive”
$ !docker
docker compose up
...

Easy as caret cake

Replace and execute previous command, add to history.

$ echo “Physical auctions = the #1 place to buy cars”
$ ^Physical^ACV^
$ systemctl status postgresql.service
$ ^status^stop^
$ docker compose up
$ ^up^down^

Guess I need root

Execute that last command as root.

$ systemctl start unison_sync.service
$ sudo !!

Curly wildcard

Replace and execute previous command, add to history.

$ rm sales_leads_2022_{03,04,05}.csv
$ rm sales_leads_20[20-22]_01.csv
$ cp {*.mp3,*.ogg} ~/Music

Move within your command line

$ git checkout ACV-20_my_new_feature
(Oops, git doesn’t know what that is)
$ <up><ctrl+left> “-b “
$ git checkout ACV-20_this_isnt_a_branch
(Man I’m really bad at git cli)
$ <up><ctrl+w> “master”

ctrl+a for beginning of line, ctrl+e for end of line. Handy list of similar shortcuts here.

git aliases

Git config / meld tool

  • logline - concise git log messages
  • submod - init and update, cause I can never remember
  • dd - directory diff (using difftool of your choosing)

Fun scripts

  • down - see what you just d/l
  • calc - simple CLI calculator
  • teatimer - don’t let your tea get bitter
  • moar_requests - poor man’s performance testing

rsync local backup

Simple backup system to a USB stick. Runs an rsync command to put home directory and /etc directories on a USB stick for “offsite” backup.

etc/cron.weekly $ ll
-rwxr-xr-x 1 root root 312 May 19  2022 0anacron
lrwxrwxrwx 1 root root  31 Oct 13  2022 backup_system -> /usr/local/bin/backup_system.sh
-rwxr-xr-x 1 root root 813 Feb 10  2022 man-db

Dropbox replacement

Poor man’s NAS (“offsite storage”).

  • Tp-link router
  • samba
  • unison
  • systemd / cron (anacron)

Dropbox replacement

Yubikey 2FA

Guide to CLI setup: https://hackernoon.com/use-a-yubikey-as-a-mfa-device-to-replace-google-authenticator-b4f4c0215f2

Use 2FA authentication, people. But don’t use an SMS code.

$ ykman oath list
$ ykman oath code paypal.com

Yubikey