10 Lesser Known Linux Terminal Tricks

command line tricks

You're never really a Linux guru if you're not conversant with the terminal. Though certain commands and syntaxes may be cumbersome to memorize, there are several helpful command line tips and tricks to make your work in the terminal much easier. They'll definitely get you more productive than before and make you enjoy using the terminal. Here are a few tricks we've got to share

1) Move to the previous directory

We all use cd .. to move to move to an upper directory. You can also use cd - to move to the previous directory - just like a back button.

test@linoxide:~/Downloads$ cd -
test@linoxide:~$ cd -

In this example, we were in the Downloads directory, and then moved back to the Home directory and finally back to the Downloads directory.

2) Repeat your last command

To replay as the previous command, just type !!. In this example, we'll repeat the previous command as root.

$ apt install vlc
 E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
 E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

$ sudo !!
 sudo apt install vlc
 vlc is already the newest version (2.2.2-5ubuntu0.16.04.3).

It's very useful when you have to run a command as root.

3) Keep executing a command until it succeeds

To keep executing a command until it finally succeeds, use the exit code of the command directly: while ! [command]; do sleep 1; done

$ while ! ./run.sh; do sleep 1; done
 cat: run.sh: No such file or directory
 cat: run.sh: No such file or directory

The command kept running until it found run.sh and printed out its content.

4) View progress of file transfers

In Linux, you cannot really know the rate of a file transfer progress until it's done. Using the pv command, you can monitor the progress of file transfers.

$ pv access.log | gzip > access.log.gz
 611MB 0:00:11 [58.3MB/s] [=> ] 15% ETA 0:00:59

5) Easily schedule events

Using the at command, you can easily schedule events at anytime.

echo wget https://sample.site/test.mp4 | at 2:00 PM

To view the queued jobs, type atq.

6) Display at output as a table

When you use the ls command or other commands to throw outputs, they are often very long and need scrolling. You can easily display all the outputs in a table form using the column -t command. In this example, we used the command

$ cat /etc/passwd | column -t

7) Keyboard Tricks

The clear command clears the terminal screen with a blank one. Pressing Ctrl + L on your keyboard does the same thing, but faster.

To go through previous commands, press Alt + . .

Ctrl + U clears the content you've typed already. Try this when you want to clear the password field in the command line.

To reverse search your command history, press Ctrl + R

8) Compress, split and encrypt  files

Trying to transfer large files across computers is a tedious task. We can easily do this by compressing the files and creating a multi-part archive if the files are extremely large. To encrypt, we add the -e switch.

$ zip -re test.zip AdbeRdr11010_en_US.exe run.sh Smart_Switch_pc_setup.exe
 Enter password:
 Verify password:
 adding: AdbeRdr11010_en_US.exe (deflated 0%)
 adding: run.sh (stored 0%)
 adding: Smart_Switch_pc_setup.exe (deflated 2%)

9) Stress test your battery

Do you want to check how long your battery can last under 100% CPU usage? Try this command:

$ cat /dev/urandom > /dev/null

10) Renaming/moving files with suffixes

If you want to quickly rename or move a bunch of files with suffix, try this command.

$ cp /home/sample.txt{,-old}

This will translate to:

$ cp /home/sample.txt /home/sample.txt-old

To rename files of a particular extension in batch, try this:

$ ls

$ rename 's/comes_here_/goes_there/' *.txt
$ ls

11) Replacement for id command

You can use awk in /proc/self/status to filter same results that id command gives

foo@localhost:/root$ awk -F: 'END {print "uid:"u" gid:"g" groups:"gg}{if($1=="Uid"){split($2,a," ");u=a[1]}if($1=="Gid"){split($2,a," ");g=a[1]}if($1=="Groups"){gg=$2}}' /proc/self/status
uid:1000 gid:1000 groups:       1000

Now, we have learned some interesting tricks you can use in the command line to make it fun and easier to use. Which one did you find most useful? Do you have tricks to add?

2 Comments... add one

  1. Actually CTRL-U is more powerful than just delete a string from terminal. CTRL-U moves all the string since the begin of the line (until cursor position) to a buffer. You can paste it from the buffer using CTRL-Y. Try! It's nice!


Leave a Comment