In UNIX, Unix like operating systems including Linux, the shell of the OS have a mechanism called pipe or pipeline which looks like this |. In fact this ASCII vertical bar character “|” is named as pipe character. Unless you are from old schooling, each and every linked article are important; most IT related professional bachelors degree programs exclude the parts which are quite important in real life. Also, you should know what is Terminal, historically we are using UNIX more effectively now than we used to do 40 years back – from Android, iOS to major servers – all runs some version of UNIX or Unix like OS. In this article we will not only discuss about the Examples of Pipe or Pipelines Command, but also go to a bit history. Specifically if you are a software engineer but it was not your major, these are very important and often neglected area. Just like, typing without looking at the keyboard without doing typographical errors need a huge time to master, application of piping needs long term usage.
History of Pipe or Pipelines Command
A pipeline consists of a chain of processing elements arranged to give the output of each element as in sequence; the name is by analogy to a physical pipeline. Usually there is some buffering the consecutive elements. The shell of the operating systems have a mechanism called pipe. This mechanism allows to chain the process so that the output of one process feeds directly. Each connection is implemented by an anonymous pipe. Programs filters are often used in this configuration. It is Douglas McIlroy who invented this concept for Unix shells. A very basic example of piping is :
1 | programme1 | programme2 |
The program programme1 is executed by the system that sends the results to programme2 which in turn returns the results to the standard output of the system. Working example :
---
1 | cut -d "" -f1 <access.log | sort | uniq -c | sort-rn | less |
This display the client IP addresses that accessed most frequently visited to a web server running Apache. Standard error (stderr) is an output stream typically used by programs to output error messages or diagnostics. Pipeline escapes this stderr. If you run man stderr
on OS X 10.9.x, you will get BSD Kernel Interfaces Manual.
We usually use simple pipelines where the shell connects a series of sub-processes via pipes and executes external commands within each sub-process. But it is possible for the shell to perform processing directly, this is called Pipemill :
1 2 | command | while read var1 var2 ...; do done |
this is a subshell – var1, var2 etc. will not be available after the while loop terminates.
Examples of Pipe or Pipelines Command
Our Tar Extract Progress Bar tutorial uses extreme level of piping. Run this on OS X :
1 | ls -al | more |
UNIX and BSD has UNIX Wheel Group. It gave me output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ➜ ~ ls -al | more total 1077960 drwxr-xr-x+ 160 abhishekghosh staff 5440 Aug 26 02:35 . drwxr-xr-x 5 root admin 170 Jul 5 13:24 .. -rw-r--r--@ 1 abhishekghosh staff 16 Dec 13 2013 .CF89AA64 -rw------- 1 abhishekghosh staff 3 Jan 17 2014 .CFUserTextEncoding -rw-r--r--@ 1 abhishekghosh staff 39940 Aug 24 16:29 .DS_Store drwx------ 9 abhishekghosh staff 306 Aug 23 14:01 .Trash -rw------- 1 abhishekghosh staff 49 Mar 16 01:04 .Xauthority drwxr-xr-x 4 abhishekghosh staff 136 Dec 30 2012 .adobe -rw------- 1 abhishekghosh staff 170 Mar 13 2013 .af_token -rw------- 1 abhishekghosh staff 12288 Jul 27 2013 .app.js.swp -rw------- 1 abhishekghosh staff 2075 Jul 20 00:24 .bash_history -rw-r--r-- 1 abhishekghosh staff 101 Jul 11 13:44 .bash_profile drwxr-xr-x 3 abhishekghosh staff 102 Feb 28 19:19 .bundler drwx------ 4 abhishekghosh staff 136 Aug 8 2013 .cache drwxr-xr-x 3 abhishekghosh staff 102 Feb 21 2014 .cocoapods drwx------ 6 abhishekghosh staff 204 Jan 18 2014 .config drwx------ 3 abhishekghosh staff 102 Jan 3 2013 .cups drwx------ 3 abhishekghosh staff 102 Feb 22 2013 .dbus-keyrings drwx------ 4 abhishekghosh staff 136 Feb 26 2013 .emacs.d -rw------- 1 abhishekghosh staff 12288 Aug 31 2013 .example.html.swp drwx------ 4 abhishekghosh staff 136 Dec 20 2012 .fbcmd drwx------ 11 abhishekghosh staff 374 Aug 14 13:34 .filezilla drwxr-xr-x 3 abhishekghosh staff 102 Nov 25 2012 .fontconfig |
I am not root
. In the same way, to list of all sub-directories in the current directory, we can use :
1 | ls -al | grep '^d' |
I used d to grab drwx
! Here is a combination of pipeline, redirection (>) and usage of && :
1 | ls | head -3 | tail -1 > myoutput.txt && nano myoutput.txt |
If I stopped before the && my output.txt
, I had to work more, it would waste time; it would demand to type cat myoutput.txt
– Piping is for not saving time, it is for logically making the things working. Run man pipe
on OS X, you will get pipe (8) – Postfix delivery to external command manual. tee
kind of sanitizes the raw usage of redirection :
1 | ps -ax | tee allprocess.txt | more |
You can run diff to compare over ssh :
1 | ssh user@remote-host "cat path/file.name" | diff path/file.name - |
Practically you need to understand which thing can be connected with which to get a practical result. Unless you have sufficient knowledge on UNIX commands, pipe will remain far from being useful. These are safer examples, do not run others’ pipe without knowing what are the individual commands can do.
Tagged With c99madshell Drwxr Off server ip drwxr-xr