7 tips to speed up your Linux command line navigation
Knowing a few key shortcuts can have a surprisingly positive impact on your productivity. As someone who sits frequently with others over a command line to help debug code and to navigate servers, it's easy to become frustrated on the typer's behalf. Working at the command line can be cumbersome when you don't know the best time-saving shortcuts to use. This article fixes that frustration for you.
Here are some essential shortcuts and key tips to help you speed up your command line usage.
Run that again as root — sudo
$ cat /var/log/messages
cat /var/log/messages: Permission denied.
Don’t use bash history with keystrokes: up arrow, left arrow, left arrow, left arrow, sudo
, ENTER.
Instead use: sudo !!
This little shortcut works because !!
is a shell place holder for the last command executed. Typing those seven characters will run that last command as root without those up and left arrow key presses. This shortcut also works without sudo
, if you want to run the last command again without changes, for some reason.
Search for that command I ran — Ctrl+r
What was that command I ran? Up arrow, up, up, up. Oh, there it is. ENTER.
You search through your history one step at a time but there's a better way. What if I told you there is an easy way to search your previous commands?
Don’t type: Up arrow, up, up, up, ENTER.
Instead: Ctrl+r
Note: Don't type the (+
). This means to use the Ctrl
key and the r
key together.
Simply use, Ctrl+r
, and type the first few letters of the command you want to repeat. If the search doesn’t match on the first result, just use Ctrl+r
a few more times to scroll through results—shown below searching for the cat
command.
(reverse-i-search)cat: sudo cat /var/log/messages
Go back to your home directory — cd
You would be amazed at how many people don’t know this. cd
. That’s right. Without any arguments, it takes you back to your home directory.
Go back to the last directory - cd -
Sometimes the simplest things are the best. Where you in the /var/www/foo
directory, but are now in /etc
? Simply type, cd -
and you will return to /var/www/foo
.
Don’t type: cd /var/www/foo
Instead: cd -
Job control — backgrounding, foreground, etc
This might take some getting used to, but when you get the hang of it you’ll never go back. Let’s say you are editing a file in vim
(well, you wouldn’t use nano
, would you?), and now you want to go and look in the /var/www/html
directory. You could quit vim
, browse to the directory, only to find that you want to edit the file again. Instead, you can send vim
to the background and come back to it later.
Type: Ctrl+z — This is a shortcut that backgrounds any existing foreground task. Useful for, but not limited to, less
, cat
, man
, vim
, etc.
Where did my foreground task go, you might ask. Simply type, jobs
to see it in a list.
$ jobs
[1] Stopped vim
Great. You can now go do something else. Whenever you want this back again, simply, type fg
. This will bring the background job (vim
) back to the foreground again. Note that the background process is paused, so if you’re running something like tail
on a file, the process will have some catching up to do. If you have multiple jobs running in the background, fg 3
, for example, resumes the third job in the list. Don’t forget to run the jobs
command to see a list of paused jobs.
Alias frequently used commands
If you often run a command with the same arguments, create an alias for it. I have many of them. I often use the
syntax, which is the command's normal name followed by an x. For example, with netstat
, I always run it with -n
(numeric addresses only) , -t
(tcp protocol), -a
(all), -u
(udp protocol), and -e
(extended output). netstat -ntaupe
— it rolls right off the tongue, right? I’m lazy (and might forget an option), so I aliased that to netstatx
like this;
$ alias netstatx="netstat -ntaupe"
Try it for anything you run regularly.
Don’t type: netstat -ntaupe
Instead: netstatx
A bonus shortcut
You can use the keyboard combination, Alt+.
, to repeat the last argument.
Note: The shortcut is Alt+.
(dot).
$ mkdir /path/to/mydir
$ cd Alt.
You are now in the /path/to/mydir
directory.
Summary
Invest a little time now to save time in the future will really speed up your life at the command line.
Want to test your sysadmin skills? Take a skills assessment today.
James Read
James Read is a Principal Solution Architect for EMEA Cloud and Service Providers and has been at Red Hat since 2011. A technologist at heart, James is passionate about code, containers, and cloud. More about me