Use Command Line Faster
Every Linux user knows how powerful the command line is, especially when you manage servers. And if you have to deal with the command line frequently, it is very important to work efficiently and quickly. Here are some tips I know that would allow you to work faster with the command line. Feel free to include your improvements in the comments!
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 type
user@machine ~ % !vim
and the editor will open the last file without you typing the whole command.
Redo with '!!'
!! 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
user@machine ~ % iftop interface: enp1s0 IP address is: 10.10.0.2 MAC address is: 12:34:56:78:abcdefgh:90 pcap_open_live(enp1s0): enp1s0: You don't have permission to capture on that device (socket: Operation not permitted) user@machine ~ % sudo !! sudo iftop [sudo] password for user:
This is one the most helpful, self explanatory commands. The
<strong>history</strong> command will give you a list of used commands with an associated number. To run a certain command, you just need to type
!number. For example:
user@machine ~ % !123
will run the
#123 command in your command history.
Another use of
history would be to find the exact command you used. E.g., to find which files were moved with
mv, you can type:
user@machine ~ % history | grep 'mv'
it will show you a list of all the
mv commands you used, that are stored in your shell history.
You can modify the history size within your .bashrc, .profile, .zshrc, or a separate file with all your custom variables (don't forget to include it in your main shell config file) depending on the shell you use.
Another way find a command you used before is the key combination Ctrl+R. Unlike history, Ctrl+R shows only one command. For example, if you want to find out a file you moved some time ago, you can just hit Ctrl+R and type
mv. This will show you the most recent
mv usage. Ctrl+R can be also used to search for every matching string, not just the very last matching command.
Send a job to background
Sometimes a command will take long to finish. It's best then to put it 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
user@machine ~ % bg
and this command will be sent to background to run. To bring it back to the foreground, type
user@machine ~ % fg