20 Linux Command Tips and Tricks That Will Save You A Lot of Time
Brief: Here are some tiny but useful Linux commands, terminal tricks and shortcuts that will save you a lot of time while working with Linux command line.
Have you ever encountered a moment when you see your colleague using some simple Linux commands for tasks that took you several keystrokes? And when you saw that you were like, “Wow! I didn’t know it could have been done that easily”.
In this article, I’ll show you some pro Linux command tricks that will save you a lot of time and in some cases, from plenty of frustration. Not only your friends or colleagues will ‘wow’ at you, it will also help you increase your productivity as you will need fewer keystrokes and even fewer mouse clicks.
It’s not that these are Linux tips for beginners only. Chances are that even experienced Linux users will find some hidden gems that they were not aware despite using Linux for all these years.
In any case, you learn Linux by experience, be it your own or someone else’s 🙂
Cool Linux terminal tricks to save time and increase productivity
You might already know a few of these Linux command tips or perhaps all of it. In either case, you are welcome to share your favorite tricks in the comment section.
Some of these tips also depend on how the shell is configured. Let’s begin!
0. Using tab for autocompletion
I’ll start with something really obvious and yet really important: tab completion.
When you are starting to type something in Linux terminal, you can hit the tab key and it will suggest all the possible options that start with string you have typed so far.
For example, if you are trying to copy a file named my_best_file_1.txt, you can just type ‘cp m’ and hit tab to see the possible options.
You can use tab in completing commands as well.
1. Switch back to the last working directory
Suppose you end up in a long directory path and then you move to another directory in a totally different path. And then you realize that you have to go back to the previous directory you were in. In this case, all you need to do is to type this command:
cd -
This will put you back in the last working directory. You don’t need to type the long directory path or copy paste it anymore.
2. Go back to home directory
This is way too obvious. You can use the command below to move to your home directory from anywhere in Linux command-line:
cd ~
However, you can also use just cd to go back to home directory:
cd
Most modern Linux distributions have the shell pre-configured for this command. Saves you at least two keystrokes here.
3. List the contents of a directory
You must be guessing what’s the trick in the command for listing the contents of a directory. Everyone knows to use the ls -l for this purpose.
And that’s the thing. Most people use ls -l to list the contents of the directory, whereas the same can be done with the following command:
ll
Again, this depends on the Linux distributions and shell configuration, but chances are that you’ll be able to use it in most Linux distributions.
4. Running multiple commands in one single command
Suppose, you have to run several commands one after another. Do you wait for the first command to finish running and then execute the next one?
You can use the ‘;’ separator for this purpose. This way, you can run a number of commands in one line. No need to wait for the previous commands to finish their business.
command_1; command_2; command_3
5. Running multiple commands in one single command only if the previous command was successful
In the previous command, you saw how to run several commands in one single command to save time. But what if you have to make sure that commands don’t fail?
Imagine a situation where you want to build a code and then if the build was successful, run the make?
You can use && separator for this case. && makes sure that the next command will only run when the previous command was successful.
command_1 && command_2
A good example of this command is when you use sudo apt update && sudo apt upgrade to upgrade your system.
6. Easily search and use the commands that you had used in the past
Imagine a situation where you used a long command couple of minutes/hours ago and you have to use it again. Problem is that you cannot remember the exact command anymore.
Reverse search is your savior here. You can search for the command in the history using a search term.
Just use the keys ctrl+r to initiate reverse search and type some part of the command. It will look up into the history and will show you the commands that matches the search term.
ctrl+r search_term
By default, it will show just one result. To see more results matching your search term, you will have to use ctrl+r again and again. To quit reverse search, just use Ctrl+C.