Efficiently working in a command line is all about using your keyboard to its full potential. Here are some useful tips and shortcuts I use regularly that you might find useful.
This list will be organized based on how much I personally use each shortcut or trick, but please take a peek at all of them since what I use day-to-day may be different from what you find useful.
Reverse incremental search
Reverse incremental search is a way of searching your history for previous commands you entered. Reverse-i-search can be performed by pressing
Ctrl+R in the command line followed by typing in any part of the previous command. This brings up the most recent usage of the phrase you just typed. Press Ctrl+R again to incrementally go through commands that contain the phrase that you previously used.
For example, lets say I wrote a script in Python that has 5 arguments. and I go to run it.
$ python script.py arg1 arg2 arg3 arg4 arg5
After I run that, I may want to check the output with
cat and maybe do some unrelated work. Later that day, I find out I need to run the same script but only need to change one or two of the parameters from my earlier run. At this point, I would use reverse-i-search to find this command that I used.
Ctrl+R to bring up the reverse-i-search prompt.
Then just type anything you remember from the command you want to reuse. In this case, I can type “python”, and probably get the command.
(reverse-i-search)`python': python script.py arg1 arg2 arg3 arg4 arg5
But what if I used another command that had the phrase “python” in it? In that case you can just keep pressing
Ctrl+R until you find the command you need. A better way to do it, though, is to just keep typing out the command, and eventually you will have a unique enough phrase that you will get the command you need without the need for incrementally crawling through your history.
I use this shortcut all the time to avoid retyping any commands I may have used before. It’s much faster than the alternative of seeing your previous commands one by one with the arrow keys or by typing
history and scanning the list for the command you want.
Most people using command line will probably know what a for loop is in their favorite programming language, but did you know that you can use bash for loops directly in command line to more easily perform repetitive tasks?
For loops in bash can be written in a single line or multiple lines. Here is what they can look like:
# single line for f in *.txt; do echo $f; done # multiple lines for f in *.txt do echo $f done
Now that you know what a for loop is, what can they do? Here is just a short list: modify several files in a directory at once, generate files based on a pattern, or go through a file line by line and perform an action. The possibilities are endless.
One of the most valuable shortcuts to learn about in Linux is the process of literally defining shortcuts, or aliasing. An alias in Linux is like an alias in real life – a different name to call something by. In Linux, this means you can set a phrase to perform a certain action by adding an alias to the
.bashrc in your home directory. Just edit the
~/.bashrc file and add the alias you want to use, and run
Here is an example of an alias I use all the time:
alias lsl='ls -lAh'
This shortcut lets me list all details about the files in this directory, including hidden files, and display them in a human readable format by just typing
There are many other use cases and it really just boils down to what commands are you using most often. You could, for example, use this to start an ssh connection to your workplace, or run maintenance, etc. Learning about this tool has saved me a ton of time while using command line and it can save you time as well.