Some tips to use the command line faster

Everybody knows how powerful the command line is in Linux, especially servers. And if you have to deal with the command line frequently, it is very important to work efficiently and quickly. From my years of experience in Linux, here are some tips I know to work faster with the command line. If you know any other tip, feel free to contribute.

1. Use "!"

If you use the command line a lot, it is very likely that you will run several different commands in a short span of time. And if you want to re-run a command that was just recently used, using "!" is much faster than using the arrow keys to find the command. With "!", you only have to remember the name of the command. For example, if you want to re run the last "vim" command, you just need to typ "!vim" and vim will open the last file without you typing the whole command.


2. Use "!!"

Using "!!" is the best way to re-call the previous command. It is actually faster than using the arrow keys. For example, when you forget to type "sudo" before the previous command, to re-run it with sudo, you just need to type "sudo !!".

Here is an example of using "!!":


3. Use the "history" command

This is the most helpful command IMO, it is used to show the commands you have used. The "history" command will give you a list of the commands and before each command there is an associated number. And to run a certain command, you just need to type "!number". For example, "!123" will run the #123 command in your command history.

Another use of "history" is to find what exact command you had used. For example, to find which files you have moved around with the "mv" command, you can use this command:
 history | grep "mv"   

And it will show you a list of all the "mv" commands you had used.


4. Use Ctrl + R to find the last matching command

Another way to know which command you just used is "Ctrl + R", but unlike the "history" command, "Ctrl + R" show only the last command. For example, if you want to know which file you just move with the most recent "mv" command, you can just hit Ctrl + R then type "mv", and it will show you the most recent "mv" command you just used.


5. Use "pushd", "popd" and "cd-" to switch directories

Typing the whole directory path for "cd" wastes time. You can just use "pushd" or "popd" to switch to and fro between two directories. It is very simple to use "pushd" and "popd". When you are in a certain directory and want to switch to another directory, instead of "cd", you can use "pushd":
 pushd /path/to/new/directory  

By using this command, the original directory path will be saved to use with "popd". After finishing work in the new directory, to return to the original directory, just type:
 popd  

And another way to go back to the previous directory is use "cd-"


6. Send a job to background with Ctrl + Z and "bg"

Sometimes a command will take long to finish, like when you use the "find" command, its best to just put the work to background. To put a running command to background, you will need to suspend it first. Just hit Ctrl + Z and the running command will be suspended.

After that, type "bg" and this command will be sent to background to run.
linux tips and tricks