Home Uncategorized Advanced Linux Commands For Beginners

Advanced Linux Commands For Beginners

by Sandhiya Annamalai

In this article we are going to learn some more advanced commands in linux OS. Please check Basic Linux Commands for newbies. & Basic Commands in Linux Operating System if you’re interested in learning a few basic commands.

We will look at the following commands in this article:

  • grep
  • sort
  • wc
  • pipe
  • awk
  • find
  • ps
  • nohup
  • head
  • tail
  • jobs
  • kill

1.grep

  • grep is abbreviated as Global Regular Expression Print.
  • The grep filter searches a file for a particular pattern of characters or word and displays all lines that match the pattern.
  • It is generally used to search files or STDOUT (output of a command).
  • It can also be used to search a pattern in multiple files at once.

           Syntax: grep <search word> <filename>

$ grep "rose" rose.txt

Variations in grep command:

a) grep -c

The flag -c is used to display the number of times(count) the searched word is present.

 Syntax: grep –c <search word> <filename>

$ grep -c "rose" rose.txt
$ grep -c "Rose" rose.txt

b) grep -v

The flag -v ignores the lines that match with the searched word and prints the rest of the lines.

Syntax: grep –v <search word> <filename>

$ grep -v Rose rose.txt

c) grep -n

This variation is used to display the line number and the line where the search word is present in the file.

Syntax: grep –n <search word> <filename>

$ grep -n rose rose.txt

d) grep -l

This variation is used to list all the files containing the searched word.

Syntax: grep –l <search word> <*.txt>

$ grep -l "rose" *.txt

e) grep -o

This command is used to print the matched words in separate lines.

Syntax: grep –o <search word> <filename>

$ grep -o flowers flowers.txt

f) grep -i

This command is used to print the lines that match with the searched word. The search is not case-sensitive.

Syntax: grep –i <search word> <filename>

$ grep -i FLOWERS flowers.txt

2.sort

  • Sort command is used to order the records.
  • We can arrange the data alphabetically or numerically in ascending or descending order.

Syntax: sort <filename> (to print data in ascending order).

$ sort <name.txt>

Syntax: sort -r <filename> (to print data in reverse or descending order).

$ sort -r <name.txt>

3.wc

  • wc stands for word count.
  • As the name implies this command is mainly used to count the number of lines, number of words, number of bytes and characters in the file.

Syntax: wc <file name>

$ wc rose.txt

Variations in wc command:

  • wc –l  prints the number of lines in a file.
  • wc –w prints the number of words in a file.
  • wc –c prints the total number of bytes in a file.
  • wc –m prints the number of characters in a file.
  • wc –L prints only the length of the longest line in a file.

4.pipe

  • This command is used to combine two or more commands.
  • The standard output of one command acts as standard input to the next command.
  • The symbol (|) denotes pipe command.

Syntax : cat <filename> | grep <search word> <filename>

$ cat rose.txt | grep rose rose.txt

5.awk

  • awk command stands for the first letters of three authors’ names  (aho,weinberger,kernighan).
  • This command is widely used for text processing.
  • awk command searching for a specific word or pattern in a certain line or a certain column in a file you provide.

Syntax: awk ‘{action}’ <file name> (condition to be given in curly braces and its enclosed with single quotes)

For example if you want to print the 1st column of the file. Use awk ‘{print $1}’.

$0 – represents the entire column of the file.

 $1 – represents the first column of the file.

 $2 – represents the second column of the file.

  .

  .

  $NF – represents the last column of the file.

$ awk '{print $1}' rose.txt 

To print more than one column use awk command as awk ‘{print $1,$3}’ here $1 is the first column and $3 is the third column.

$ awk '{print $1,$3}' rose.txt 

Here (,) or (“ ”) is used to provide space between two columns and output is clearly readable.

6.find

  • find command is the most important and frequently used command in linux os.
  • It is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

Syntax: find [options] [path…] [expression]

$ find /home -name  sample.txt
  • To find the files names using the extension of the file.
$ find /home -name  "*.txt"
  • To find the files using type variation:
    • The -type f variation is used to find and list the file’s name only.
    • The -type d variation is used to find and list the directories name only.
$ find /home -type f
$ find /home -type d
  • To find and list the empty files and directories.
$ find /home -empty

7.ps

  • ps stands for process status.
  • This command is used to display or to view information related to the process running in the system.
$ ps
  1. PID – Every process is assigned a PID (Process IDentifier) which is a unique identifier associated with the running process.
  2. TTD-Controlling terminal associated with the process.
  3. TIME-Total time of CPU Usage,shows in minutes and seconds.
  4. CMD-The command that is executed by the process.

ps -ef

The -e option instructs ps to display all processes. The -f stands for full-format listing, which provides detailed information about the running processes.

$ ps -ef

Before we see about nohup command we should know about how to create a shell script(sh) file and how to execute the sh file.

  • Create a new sh file using touch command.Write the script you are going to run in sh file using vi command.
  • kindly check whether the sh file is having executable permission, if it is not there, please assign execute permission using chmod command.
  • To run the script file use ./filename command and the standard output will display in the terminal and the process continues and the output will display continously.To stop the process give ctrl+c.

8.nohup

  • nohup, no hang up command keeps the process running even after exiting the terminal or shell.
  • It prevents the process or jobs from receiving the signal SIGHUP.
  • SIGHUP (SIGnal Hang Up) is a signal used to close and exit the terminal.
$ nohup ./multiplication.sh > multiply.log

In the above process once ctrl+c given the shell script is stopped and the saved output can be viewed in the log file using cat command.

This nohup command with (&) symbol in the end is used to run the process in the background.

Syntax: nohup (./sh file) > (file name) &

$ nohup ./multiplication.sh > multiply.log &

9.head

This command print the first 10 lines of the specified file. If more file name is provided then data from each file is preceded by its file name.

Syntax: head <file name>

$ head multiply.log

10.tail

This command by default prints the last 10 lines of the specified file and exit.

Syntax: tail <file name>

$ tail multiply.log

tail -f

This command will show the last ten lines of a file. As the file gets updated, the tail command will show those lines in the output.

Syntax: tail -f <file name>

$ tail -f multiply.log

11.jobs

This command is used to list the processes running in the background.

$ jobs

12.kill

It is a built in command used to terminate the process manually.

Syntax: kill <PID/process name>.

kill -9 command sends a kill signal to terminate a process immediately or forcefully.

$ kill -9 80154

kill -15 command sends a kill signal to terminate the process gracefully

$ kill -15 81387

Thanks for reading this article. I hope it is useful for you.

You may also like