10 Lesser Known Linux Terminal Tricks

Channel: Linux
Abstract: $ cp /home/sample.txt /home/sample.txt-old To rename files of a particular extension in batchtry this command. $ cp /home/sample.txt{

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 -
 /home/eyramm
test@linoxide:~$ cd -
 /home/eyramm/Downloads
test@linoxide:~/Downloads$

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

How to use the Command Line or Term...

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

How to use the Command Line or Terminal for Absolute Beginners 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
 linoxide.com

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
text_comes_here_1.txt
text_comes_here_2.txt
text_comes_here_3.txt
text_comes_here_4.txt

$ rename 's/comes_here_/goes_there/' *.txt
$ ls
text_goes_there_1.txt
text_goes_there_2.txt
text_goes_there_3.txt
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?

Ref From: linoxide
Channels:

Related articles