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.
Rename Files with a New Extension and Other Brace Tricks
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
Make Output into Columns (For Example, the Output from mount)
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:
(Note: Substack crashes when you have the filename for your passwords. Therefore, the listing below says /etc/pa_ss_wd but you should take the underscores out when you really try this example.)
cat /etc/pa_ss_wd
cat /etc/pa_ss_wd | column -t -s :
Search and Replace the Last Command
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/
Remove Duplicate Lines in Unsorted Files
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.
Edit Command Line in Your Editor
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.
Fill Autocomplete on Command Line
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.
So Much More
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.