The Linux command-line should not be tedious. Thankfully, Linux ships with many GNU utilities to make the command-line easy. Let’s look at 8 tips to help you speed up your command-line experience like a pro! Mastering the command line will make you fast, efficient, and love Linux even more.
Tip 1 – Use tab for auto-completion
This is one of the most basic yet valuable tips. Pressing the TAB key will allow you to see all possible options that relate to your command.
For example, let’s say you want to see the total amount of memory on our machine. You can remember that the command started with the letter l, but you can’t remember the rest of the command’s name.
You would then type the letter l and the TAB key to display all the command possibilities. Looking below, you then realize that your command name is ‘lsmem’.
anto@odin:~$ l Display all 126 possibilities? (y or n) l lessfile linkicc locale-gen lpr lsusb l2ping lesskey linux32 logger lprm ltrace l2test lesspipe linux64 login lpstat lsmem
Tip 2 – Execute the last command
Sometimes you need to re-run the last command. You could type in the command again, or you could use one of these tips.
You could also use the UP arrow to run a previous command. Most people are familiar with this one.
Or, you can use the exclamation sign along with the first letter of a previous command. Unlike using the UP arrow, you could skip a few keystrokes if you remember the last letter of your command.
You can see the example below:
anto@odin:~$ lsmem RANGE SIZE STATE REMOVABLE BLOCK 0x0000000000000000-0x00000000cfffffff 3.3G online yes 0-25 0x0000000100000000-0x000000042fffffff 12.8G online yes 32-133 Memory block size: 128M Total online memory: 16G Total offline memory: 0B anto@odin:~$ !l lsmem RANGE SIZE STATE REMOVABLE BLOCK 0x0000000000000000-0x00000000cfffffff 3.3G online yes 0-25 0x0000000100000000-0x000000042fffffff 12.8G online yes 32-133 Memory block size: 128M Total online memory: 16G Total offline memory: 0B
You could also run the exclamation sign along with -1 to run the previous command. Setting -2 will run the second last command that you have run.
Similarly, you can enter double exclamation to run the last command, as seen below:
anto@odin:~$ !! lsmem RANGE SIZE STATE REMOVABLE BLOCK 0x0000000000000000-0x00000000cfffffff 3.3G online yes 0-25 0x0000000100000000-0x000000042fffffff 12.8G online yes 32-133 Memory block size: 128M Total online memory: 16G Total offline memory: 0B
Lastly, you can also enter CTRL+P, which will display the previous command.
Tip 3 – Find and run a previous command
What if you needed to execute a specific command again, one which you used a while back? And you can’t remember the first character, but you can remember you used the word “serve”.
You can use the up key and keep on pressing up until you find your command. (That could take some time)
Or, you can enter CTRL + R and type few keywords you used in your last command. Linux will help locate your command, requiring you to press enter once you found your command. The example below shows how you can enter CTRL + R and then type “ser” to find the previously run “PHP artisan serve” command. For sure, this tip will help you speed up your command-line experience.
anto@odin:~$ (reverse-i-search)`ser': php artisan serve
You can also use the history command to output all the previously stored commands. The history command will give a list that is ordered in ascending relative to its execution.
You can then use the history item number with the exclamation command shown earlier to execute a specific command. You can see below how we run command 1031 from the history.
anto@odin:~$ history 1025 cd .. 1030 sudo docker run --name thg-qos -it thg-qos:latest /bin/sh 1031 lsmem 1032 php artisan serve 1033 docker ps anto@odin:~$ !1031 lsmem RANGE SIZE STATE REMOVABLE BLOCK 0x0000000000000000-0x00000000cfffffff 3.3G online yes 0-25 0x0000000100000000-0x000000042fffffff 12.8G online yes 32-133 Memory block size: 128M Total online memory: 16G Total offline memory: 0B
Tip 4 – Run multiple commands at once
Let’s suppose you want to execute three commands, one after the other. Normally, you run the first command, run the second command, and then finally run the third command.
Or, you can use the semicolon to separate all three commands and run them sequentially. The following example will create a test folder, enter it and then return it to the same folder.
anto@odin:~$ mkdir test; cd test; cd ..
Tip 5 – Use the alias command
You may have a specific command with preferred settings that takes a long to type. Or you can have a couple of commands that you need to run frequently sequentially.
You can take the work out of typing the commands over and over and use the alias command.
For example – we can create an alias called “mnt”. Alias will in-turn run “cd /mnt/containers/” whenever we run the “mnt” alias.
anto@odin:~$ alias mnt="cd /mnt/containers/" anto@odin:~$ mnt anto@odin:/mnt/containers$
Used properly, the alias command can really help you speed up your command-line experience.
Tip 6 – Use the ls alias
You probably know the “ls” command to list the contents of a directory. You most likely also use the “ls -alF” command.
If so, then you can use one of the follow ls aliases:
alias ll='ls -alF' alias la='ls -A' alias l='ls -CF
Tip 7 – Move to the start or end of a line
Let’s suppose you’re typing a lengthy command. And in-between, you decide to change something. If your cursor is midway through the command, you need to hit the left or right arrow. Instead of bashing the left and right arrows to get to start or end, use the shortcut.
You can hit the HOME key or press CTRL + A to move to the start of any command. Using the END key and CTRL + E, you can move to the end of any command.
Tip 8 – Unfreeze your Linux terminal from accidental CTRL+S
Windows users are familiar with hitting CTRL + S to save a file. But, if you use this in the Linux terminal while editing a file, then the terminal will freeze.
Instead of closing and reopening the terminal again, you can use a shortcut. Press CTRL + Q to unfreeze the terminal and start using it.
It takes time to adopt a new way, and change can be hard. But you can save yourself a lot of time if you invest a little time to speed up your command-line skills. Which of these commands do you think is the most useful?
About the Authors
Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.
Support the Cause
Support Anto Online and buy us a coffee. Anything is possible with coffee and code.