w3reference home
Shell Tutorial


Bookmark and Share

Pipes

With the use of pipes, you can use the output of a program as the input of another one. Below is a simple example using ls command. Example:
w3user@ubuntu-a:/usr$ ls -rlt
total 152
drwxr-xr-x  10 root root  4096 2008-10-29 18:53 local
drwxr-xr-x   2 root root  4096 2008-10-29 18:58 X11R6
drwxr-xr-x   2 root root  4096 2009-02-02 01:33 games
drwxrwsr-x   6 root src   4096 2009-02-02 01:33 src
drwxr-xr-x  33 root root 12288 2009-02-23 20:39 include
drwxr-xr-x 299 root root 12288 2009-03-27 08:31 share
drwxr-xr-x 178 root root 61440 2009-03-28 09:00 lib
drwxr-xr-x   2 root root 12288 2009-03-28 09:00 sbin
drwxr-xr-x   2 root root 36864 2009-03-28 09:01 bin
w3user@ubuntu-a:/usr$ 
Now we will run the above command using pipes(adding grep).
w3user@ubuntu-a:/usr$ ls -rlt|grep bin
drwxr-xr-x   2 root root 12288 2009-03-28 09:00 sbin
drwxr-xr-x   2 root root 36864 2009-03-28 09:01 bin
w3user@ubuntu-a:/usr$ 
In the above example, the use of pipe does not display the result of the first command, ls -rlt. Instead the second command, grep is executed on the output of the first command. So the lines that contain the pattern, bin will be listed as the final output.

You can use as many pipes as needed in a single command. Example:

w3user@ubuntu-a:/usr$ ls -rlt|grep bin|sed -e 's/bin/replaced/g'
drwxr-xr-x   2 root root 12288 2009-03-28 09:00 sreplaced
drwxr-xr-x   2 root root 36864 2009-03-28 09:01 replaced
w3user@ubuntu-a:/usr$ 
Here we have used two pipes. You can use as many pipes as needed.
Code Validator
Learn FTP
Color finder
Link Checker
Free web designs
Coming soon!
Interview Questions...
'w3reference : Learn by examples ... Advanced to beginner's tutorials ...'
Ajax: AJAX tutorial1 | Apache: Apache HTTP Server | Restarting Apache | CSS: CSS Border | CSS Syntax | CSS Selector | CSS Comment | CVS: CVS Release | CVS Login | CVS Logout | CVS Annotate | Databases: Rolap Tutorial | OLAP Tutorial | OLTP Tutorial | data warehousing | Expect: HTML: html | Linux: Dot (.) conf files | Linux Mount Point | Linux Filesystem | SSH Tutorial | Linux Commands: cal | cat | cfdisk | chroot | MySQL: MySQL Commands | PHP: PHP Basics | PHP Variables | PHP Output (echo/print) | PHP String Concat | PL/SQL: PL/SQL Data Types | PL/SQL Control Structures | PL/SQL File Extensions | PL/SQL DBMS_OUTPUT package | Python: My first Python program | Shell: Starting Bash | Bash Redirection | Bash Pipes | Bash Variables | SQL: SQL Transactions | SQL Constraints | SQL Drop | SQL Union & Union All | SVN: svn architecture | SVN Repository | SVN Import | SVN Checkout | Tech: soap | Web Designing: Web Hosting | HTML/XHTML/CSS code validator | Learn FTP | Search Engine Optimization Tips | www: XML: XML vs HTML | XML Syntax | XML Tags, Elements and Attributes | XML Namespaces |
Sitemap | Disclaim | Privacy Policy | Contact | ©2007-2009 w3reference.com All Rights Reserved.