How to use File Filters commands in Linux/Unix

File Filters commands in Linux/Unix

Linux flavors come with various powerful file filtering commands. Filter commands are used to filter the output so that the required things can easily be picked up. You can get fast results and which O/P you want just with the help of some simple commands.

Different file filter commands used in Linux or Unix are as follows: 

#wc
#less
#more
#head
#tail
#sort
#sed

1) Wc:



 The wc command used to see the no of characters in a file
$wc - c <file name>

[root@rahul ~]# wc –c file1


The wc command used to see the no of words in a file
 $wc -w <file name>

[root@rahul ~]# wc –w file1


The wc command used to see the no of lines in a file
 $wc -l <file name>

[root@rahul ~]# wc –l file1


The wc command used to see the no of lines, words, characters at a time
$wc <file name>

[root@rahul ~]# wc file1



2)Less :


The less command is used to see the output line wise or page wise
$less  <file name>

[root@rahul ~]# less /etc/passwd


Note : press Enter key to scroll down line by line
      Use d to go to the next page
      Use b to go to previous page
      Use / to search for a word in the file
Use v to go vi mode where you can edit the file and once you save it you will back to less command

      3)  More :

More is exactly same like less command
$more <file name>

[root@rahul ~]# more /etc/passwd


Note : press Enter key to scroll down line by line
      Use d to go to the next page
      Use b to go to previous page
      Use / to search for a word in the file
Use v to go vi mode where you can edit the file and once you save it you will back to more command

     4)      Head :

It is used to display the top 10 lines of the file
$head <file name>

[root@rahul ~]# head /etc/passwd


To Display the custom lines:
$head –n  <file name> (where n can be any number)

[root@rahul ~]# head -5 /etc/passwd


     5)      Tail :

It is used to display the last 10 lines of the file
$tail <file name>

[root@rahul ~]# tail /etc/passwd


        To Display the custom lines:
         $tail –n  <file name> (where n can be any number)

[root@rahul ~]# tail -5 /etc/passwd


    6)      Sort :

It is used to sort the output in numeric or alphabetic order
$sort <file name>

[root@rahul ~]# sort file1

Linux is opensource
Linux is opensource
Linux is opensource
Welcome to linux
Welcome to linux


To sort the file according to numbers:

$sort –d <file name> or $sort –h <file name>


[root@rahul ~]# sort –h file1

1.Welcome to linux
2.Linux is opensource
3.Linux is opensource
4.Welcome to linux
5.Linux is opensource


To remove the duplicate entries from the output

$sort –u <file name>


[root@rahul ~]# sort –u file1

Linux is opensource
Welcome to linux


     7)      Sed :

Sed stands for stream editor, which is used to search a word In the file and replace it with the word required to be in the output.

Note: it will only modify the output, but there will be no change in the original file

$sec  ‘s/searchfor/replacewith/g’ <file name>


[root@rahul ~]# sed ‘s/linux/unix/g’ file1

Linux is opensource
Welcome to unix
Linux is opensource



Post a Comment