Basic CLI Tips and Tricks II

This article is a continuation of our previous article – 40 Basic Command Line Tips and Tricks. The terminal is a really powerful tool when it comes to Linux, anything and almost everything on Linux can be done via the command line. Therefore it is necessary for us to have a basic idea of all the important commands and a few really important options that come along with them.

1. BASH ( Bourne-Again SHell ) is the most common shell in Linux. It’s freeware shell.

cat /etc/shells -> To find the available shells in your system 
/bin/shell-name  -> For temporary changing of the shell

/bin/sh :  To change to the sh shell.

To return back to the bash shell : /bin/bash

2. To print your home directory location:

echo $HOME

3. To print colouful text

echo -e ” \ 033[31m Hello  World” Note : There is no space between the \ and 0
Output : 
Hello World ( in Red )

Set foreground color:
echo -e ” \ 033[3xm” ( where x = 0 – 7 ) Note : There is no space between the \ and 0
Set background color :
echo -e ” \ 033[4ym” ( where y = 0 – 7 ) 
Note : There is no space between the \ and 0

4. The sort command can be used to sort lines of text files. It can be used for sorting of both numbers and characters or words.

For example if your file myfile.txt contains the following data :


If you want to sort it the following command gives you the sorted list

sort myfile.txt
Output :

5. The cut command is used for removing sections from each line of files. For instance consider a file myfile.txt with the following data


cut -c1-7 myfile.txt
Output :

cut -c4,8 myfile.txt


cut -d: -f2 myfile.txt

6. The paste command is used for the merging the lines of files.

paste <  file1 > < file2 >
Output shows that the two files are combined line-by-line with tabs separating the two.

7. The join Command is used for joining lines of two files on a common field.

File1 data 
Android1 30
Android2 40

File2 data :
Android1 40
Android2 50

join File1 File2

Output :

Android1 30 40

Android1 40 50

Note : Make sure the files are in sorted order.

8. uniq command is used to omit the repeated lines.

Myfile.txt data
Soji Jacob
Soji Jacob
Stella Jacob

uniq -u myfile.txt -> For non-duplicated lines use the -u flag.

Output :Stella Jacob

uniq -c myfile.txt -> For counting the number of times each one appears -c flag is used.
Output :
2 Soji Jacob
1 Stella Jacob

9. spell : It is a spell checking program which prints each misspelled word on a line of its own. When used with the -n option it gives the line numbers before lines.

spell -n myfile.txt gives the line number and the misspelled word in the myfile.txt file.

10. finger command : The finger command displays information about the system users.

11. Say you want to add a user but don’t know the command to do it. Don’t worry with the power of the Linux command line you don’t have to mug up the commands. You can use the man command with the option -k to search by keyword user for relevant commands.
Note : The man -k is equivalent to apropos command which is used to search the short manual page descriptions for keywords and display any matches.

12. The general forms of the sed command are as follows:

Substitution sed:  ‘s/regexp/replacement/g’ < file >
Deletion sed:  ‘< start >,< end > d’ < file >

sed ‘s/soji/rohin/g’ myfile.txt :
=> To change all occurrences of the word ‘soji’ to ‘rohin’ in the myfile.txt.txt filewhere “s” means substitute, and the “g” means make a global  change. If you leave off the “g” only the first occurrence on each line is changed

sed ’2,3d’ myfile.txt 
=> To remove lines starting from line number 2 and ending in line number 3 including the third line

13. With the ‘awk’ command, you can substitute words from an input file’s lines for words in a template or perform calculations on numbers within a file.
Ex : Consider a file with the following data

Soji is 65 Kg and Rohin is 75 Kg.
Sibi is 50 Kg and Sunil is 90 Kg.

awk ‘{ print $1 ” and ” $6  }’ < filename >
Output :
Soji and Rohin
Sibi and Sunil

awk ‘{ print “Average of both are ” ( $3 + $8 ) / 2 }’ < filename >
Output :
Average of both are 70
Average of both are 70

14. expr is used for evaluating expressions.

Ex: expr 10 + 3 gives 13.

Note: For multiplication it is \* and not *.

15. bc , the , Linux calculator program can be used to perform arithmetic calculation.
For instance if you want to multiply 10 and 3, just use the bc command and then give the expression 10 * 3 to get the desired output.
Note : In this case the multiplication symbol is the traditional “*” instead of “\*” as we used in expr .

16. whatis is a command that displays the manual page descriptions. If you explore your Linux system, you will find a lot of programs and you may not know what they do.

Ex : whatis cat
Output :
cat (1) – concatenate files and print on the standard output.

Note : man -f cat gives the same output.

17. The tee command reads from standard input and write to standard output and files.

ls -l | tee dir_list => For directing the output of ls -l to dir_list file.

When used with the -a option it is used for appending ( remember the >> operator )

18. zcat will read gzipped files without needing to uncompress them first.

zcat myfile.txt.gz => To read the gzipped file myfile.txt.gz

19. The file command is used to determine the file type.
A directory known as abc if cannot be identified from its color, can be identified from the file command.

file abc
Output :
abc: directory


20. The diff command is used to compare files line by line. When used with the -y option ( side by side ) the output is obtained in two columns differentiating the two files increasing readability.
Note : The sign “|” in the output indicates that there is a difference whereas the sign “>” and “<” indicate what is left out or added.

21. The cat command provides three useful options -v for displaying non-printing characters , -t prints “^I” for each Tab in the file and -e prints a “$” at the end of each line to indicate a NULL character.

cat -vet < filename >

22. mkdir used with the -p option creates nested directories.

23. The tac command concatenates and prints files in reverse.

24. The dict command can be used to find out the meaning of any word.

dict encumbered
gives you the meaning, thesarus words and sample sentences of the word encumbered.

Note: The dict package is neccessary for the usage of the dict command.

25. The style command analyses the surface characteristics of a document giving the sentence info, word usage, sentence beginnings and so on.

26. The locate command can be used to find files by name. It is an alternative to the find command and can be really useful.

locate < filename > gives the path of that filename.

You May Also Like

More From Author