Linux Bash Shell Tutorial :How to use Pipeline and filters . 8 tips and tricks

  In this post, I will share  8 tips that you can use to manage  terminal data using pipeline and filters.

1) less Command

less will allow you to quickly browse any file section by section in the terminal (standard output).

 In above example,when I piped "lshw" command to "less" command, the output is display section by section. you can use space bar or arrow sign to  move to next section of  the file to exit you can press "q"  

2) Sort command 
sort command will sort the output of the first command.

  In above example, "lsusb" command list the usb devices connected to my machine. once I pipe it to sort command ,the usb devices is sorted  accordingly.

3) grep command

grep command will capture certain file pattern and  give the output to the screen

  In above example,i want to search for hardware with the specific word  "Intel" on it.  "lshw" command will list all the hardware on my computer .By pipe it to "grep" command, it will filter the output to only show  the line with Intel specific word in it.

4) wc command

 wc is a short form of "word count" ."wc" command will display the total line, word and byte count of a file .

   In above example, I list usb devices on my computer and filter it through "wc" command. The output of this command will give some numbers referring to line,words and bytes . -l will give the total lines , -m will give the total bytes and -w will give the total words in the file.

5) head command

"head" command will print the first few line that we specified in the terminal.

   In above example, I want to display first 4 line of "lsusb" do this I pipe "lsusb" command by using head -(dash) n followed with number 4 .

6) tail command

"tail" command will print the last few line that we specified in the terminal.

To display few lines from the bottom of standard output,you can use tail command to do so.  pipe  lsusb command  to tail command followed with -n and followed with number of lines that you want to display. in this example, I display 3 bottom lines from lsusb output.

7) tee command

 "tee" program reads standard input and copies it to both standard output (by allowing the data to continue run down in the pipeline) and to one or more files. This is useful for capturing a pipeline’s contents at an intermediate stage of processing

  In above example, I run "lshw" command to list my computer hardware.
tee command  save lshw output to lshw.txt file And at the same time the output is send to "grep" command to look for intel word pattern, this command work as a T junction in our terminal.

8) uniq command

  "uniq" command is often used in conjunction with sort . "uniq" accepts a
sorted list of data from either standard input or a single filename argument and will remove any duplicate information.

   In above example, I list both files in user binary folder and binary folder.
by pipe it through "uniq" command, it will remove all the duplicate files between these two folder.

 If you want to see the list of duplicates file instead, you can  add -d option to "uniq" command

Read Previous : Linux Bash Shell Tutorial : How to shorten command in Linux Terminal
Next Post »