Bash shortcuts I use often

Mac computer glowing in a dark room

He sendeth forth his commandment upon earth: his word runneth very swiftly.

Psalms 147:15, The Bible
Poem: Bourne Again
I'm born again like Matt Murdock โ€” 
Transformed when I use Bash;
Performing signs and miracles,
My fingers flinging fast ๐Ÿ‘จโ€๐Ÿ’ป.

Bash and the terminal are two of the best things to happen to computing. I can talk to the soul of the computer with just the tips of my fingers โ€” talk about sheer power! It's the OG ChatGPT ๐Ÿ˜‰.

In this blog post, I'd like to talk about Bash1 shortcuts that save me typing time.

Shell shortcuts

Rename a file using brace expansion ({}):

$ mv imaginary-playmate{,-rene-and-angela}.mp3
mv imaginary-playmate.mp3 imaginary-playmate-rene-and-angela.mp3

Run previous command with sudo using history expansion (!):

$ rm -rf /   # Delete folder without proper permission
Permission denied.
$ sudo !!   # Run last command with "sudo"
sudo rm -rf /

Run last command with a specific prefix:

$ ssh user@mmhq.me   # SSH to server

# Run a couple commands on server ...
# ...

$ !ssh   # Return to server
ssh user@mmhq.me

Print last command (so you know what it is before you run it):

$ !make:p   # print last "make" command without running it
make VERBOSE=1

# Press the "Up" key and hit "Enter" to run the command.

Copy folder and all its contents:

$ cp ~/Music/songs/ /run/media/flashdrive/   # Try to copy folders with the "-r" flag missing, fails
cp: -r not specified; omitting directory '/home/efe/Music/songs/'
$ cp -r !*   # Copy folder with the right flag, succeeds
cp -r ~/Music/songs/ /run/media/flashdrive/

Run a very long command; use the printed result in another command:

$ ls -1rth ~/Music/songs/*.mp3 | tail -1   # Print last added song in folder
/home/efe/Music/songs/imaginary-playmate.mp3
$ mid3v2 -l $(!!)   # Check ID3 metadata tag in MP3 file
mid3v2 -l $(ls -1rth ~/Music/songs/*.mp3 | tail -1)

Use last argument of last command:

$ cp blog/posts/.template.md blog/posts/something-exciting-i-have-to-say.md   # Create new blog post by copying template file
$ vim !$   # Open Markdown file in vim
vim blog/posts/something-exciting-i-have-to-say.md

Check the error status of the last command to know if I should run it again:

$ yay -Syu   # Run updates on my computer
$ echo $?   # Print last error code; prints "0" if successful and any other number if not
0

Switch directories without typing the directory names:

$ cd ~/Downloads   # Switch to "Downloads" directory
$ cd ~/Documents   # Switch to "Documents" directory
$ cd -   # Switch back to "Downloads" directory
$ cd   # Switch to home directory

Run a recently run command:

$ history | grep rm   # Check history for all "rm" commands
1094 rm -rf blog/posts/talk-a-lot.md
1096 rm -rf /tmp/cache
1099 rm -rf blog/posts/talk-some-more.md
$ !1096   # Run second-to-last command
rm -rf /tmp/cache

Keyboard shortcuts

Bottom line

You don't have to move like a snail ๐ŸŒ when using the terminal. Try these shortcuts out today and become the Speedy Gonzalez โšก of executing commands. (Leave your nerdy coworkers in awe ๐Ÿค“๐Ÿ™ƒ.)

Further reading


  1. Bash stands for Bourne-Again Shell. Yes, I will see it in heaven ๐Ÿ˜‡๐Ÿ™๐Ÿ˜‰.ย โ†ฉ

If you would like to reply to or comment on this blog post, feel free to email me at efe@mmhq.me.