How to be fast in the Linux command line

The Linux command-line should not be tedious. Thankfully, Linux ships with many GNU utilities to make the command-line easy. These tips will help you be fast on the Linux command line. Mastering the command line will make you fast, efficient, and love Linux even more.

You may also be interested in: Most Used Linux/GNU Commands.

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. For example, 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. For example, you could type in the command again or 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. Then, 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 using 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. In addition, 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 earlier exclamation command to execute a specific command. For example, 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 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 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.

See below:

anto@odin:~$ alias mnt="cd /mnt/containers/"
anto@odin:~$ mnt
anto@odin:/mnt/containers$ 
You can add your alias command in the .bashrc file or .profile file to add the alias permanently.

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

For example, 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, 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.

Frequently asked questions

What Linux am I running?

There is a fast way of using the command-line to determine your OS name and Linux version. You can do this by using the following three commands: cat/etc/os-release, lsb_release -a, or hostnamectl. Or, you can use the command uname -r to find out the Linux kernel version that you are using.

Are Linux and UNIX commands the same?

A lot of Linux and Unix commands are the same. Note that Linux is not Unix but a Unix-like OS. They share the same philosophy but some standard commands differ. You can use the old Unix commands but the new and extended GNU commands come in handy.

Wrapping up

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?

You may also be interested in

Source:

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

One Comment on “How to be fast in the Linux command line”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.