6 Bash Tricks You Can Use Daily

Work smarter, not harder at the Linux command line

Al Williams
Sep 2, 2020 · 4 min read
Photo by Kevin Ku from Pexels

Nothing defeats your Linux street cred faster than using a GUI to do basic tasks. Everyone knows the command line can be super productive for many tasks, but if you know a few simple tricks, it can be even more productive. I’ve tried to pass over the tricks I think most people know and focus on some that are a bit off the beaten path. Hopefully, you’ll find at least one or two you didn’t know already.

How many times have you wanted to rename a file to have an extension? You could type out the entire file name, of course:

mv /tmp/working-dir/readme.md /tmp/working-dir/readme.md.backup

But it is much more efficient to use brace expansion:

mv /tmp/working-dir/readme.md{,.backup}

The comma-separated items in the braces get repeated along with the prefix, so those two commands are exactly the same. That’s because the first item in the list is empty, so the first repetition is the prefix alone. Then the second repetition will add the .backup.

Sometimes it is useful to try it out with echo until you get the hang of it:

echo cp a{1,2,3}.txt backup-dir

This expands to:

cp a1.txt a2.txt a3.txt backup-dir

Bring up the command again, delete the echo, and you can easily copy the three files to the backup-dir. If you have files 1, 2, 3, 4, 5, 6, 7, 8, and 9, you can save some typing:

cp a{1..9}.txt backup-dir

Many commands output text that is hard to read because the data isn’t lined up well. For example, try:

mount

Use the column command to make things easier:

mount | column -t

For another example, try these two commands:

cat /etc/passwd
cat /etc/passwd | column -t -s :

Suppose you’ve just typed the following line:

print-report -c 2130 

Presumably, this creates a report for customer #2130. Now you need the same report for customer #2150. This will do it:

^2130^2150

This is actually shorthand for the longer history command:

!!:s/2130/2150/

It pays to remember that the substitution only applies once, so if you had something like:

print-report -c 2130 >output-2130

You would get the substitution:

print-report -c 2150 >output-2130

Probably not what you had in mind. You can get the change to apply multiple times with the longer syntax:

!!:gs/2130/2150/

It is an old trick to sort files and use uniq to remove duplicates. However, you might not want to sort files. For one thing, you might not want the file sorted and even if that doesn’t matter, sorting a large file is pretty compute-intensive.

The awk program can easily handle this:

awk ‘!seen[$0]++’ file

The awk program can do lots of things, of course. For example, you could easily make it reject lines that have unique fields, but that’s a longer topic about how awk identifies fields.

Suppose you are typing a very long command line:

PATH="/home/alw/bin:/home/alw/bin:/home/alw/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

You discover a mistake before you hit Enter. Or you discover it later and call the line back in your history. You can edit on the command line, of course, but if you prefer your editor of choice press Control+X Control+E. Your default editor (usually set with $EDITOR or $VISUAL) will start with the command loaded.

One tip: when you exit the editor the command will run, even if you abort the editor. So if you don’t want to run the command, delete it or put a # in front of the line before you exit.

You probably use the tab key completion on the command line all the time. If you don’t, then you should go look that up right away. But sometimes you want to copy all the matching items from the completion into the command line.

Try this in, for example, your /etc directory (but don’t press Enter yet):

ls a

When you hit the tab key, you’ll see a list of things that match starting with a like acpi, adduser,conf, apm, etc. Now type ESC * (that’s the Escape key followed by the star). All of those matching files are now in the command line.

You can edit the command line as usual or use the Control+X Control+E trick.

There are thousands of potential tricks, but these are some I use often that I don’t often see elsewhere. Bored? Do:

man bash

You can spend the best part of a day reading about all the different things you can make the shell do for you.

For Linux Users

User tips and tricks for those using Linux, WSL, or Linux-friendly apps like Chrome, GIMP, etc.

Al Williams

Written by

Engineer. Author. Team Leader. Lots of other things. I blog about hardware hacking for Hackaday (www.hackaday.com), but talk about other topics here.

For Linux Users

If you use Linux from the command line or from a GUI, we want to help you get the most from it. Look for tutorials, software reviews, and more. Using WSL or Cygwin? We got you covered. Some software topics will apply to any platform (e.g,. Chrome, Google Docs, etc.).

Al Williams

Written by

Engineer. Author. Team Leader. Lots of other things. I blog about hardware hacking for Hackaday (www.hackaday.com), but talk about other topics here.

For Linux Users

If you use Linux from the command line or from a GUI, we want to help you get the most from it. Look for tutorials, software reviews, and more. Using WSL or Cygwin? We got you covered. Some software topics will apply to any platform (e.g,. Chrome, Google Docs, etc.).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store