Using Pipes at the Command Line, a Basic Overview

Apr 3, 2007 - 1 Comment

Terminal in OS X One of the essential functions of the command line of Mac OS X, Linux, or any Unix, is understanding some basics about how to use pipes. Essentially, pipes allow you to direct the output of one command into the input of another command, allowing the following command to manipulate, adjust, or work with the prior commands return. Knowing how and when to use pipes is vital to effective command line usage, and it’s a core piece of knowledge for Terminal users.

Without further introduction, here’s some information on command line pipes, what they do, and most importantly, how to use the pipes to control command line output, effectively ‘piping’ it to elsewhere:


The pipe symbol looks like |, (it’s the same key as your \ key, if you’re confused), and is immensely useful when you’re working in the command line. Here’s some examples:

ls -la | more

This takes the list command (with long and all flags) output and ‘pipes’ it through to the more command, allowing you to view the output one page at a time.

ps aux | grep user

This command takes the output of the process command, and reports back only process instances belonging to ‘user’

You can use pipe with virtually anything, so use your imagination.

Another common use for pipes are to combine with “less” to improve readability of lengthy output, when using ‘cat’ or something similar:

cat /etc/passwd | less

Pipes can be used in very advanced ways as well, taking the output of any command and redirecting that command output into the input of another command string, and then taking that output and redirecting it yet again, in a lengthy string of commands and pipes, this could look something like this:

cat /etc/OSXDaily.txt | grep "osxdaily test" '\n' | sort | uniq | less

Pipes can also be combined with redirects, and just about any other method of manipulating anything in the terminal.

We cover the Mac OS X command line relatively often here at OS X Daily, but our recent article Command Line usability basics: Redirection probably should have included a bit about pipes as well, so here we are. More advanced purposes would be best covered in another in depth tutorial to explore beyond the basics, so stay tuned.

.

Related articles:

Posted by: Paul Horowitz in Command Line, How to, Mac OS, Tips & Tricks

One Comment

» Comments RSS Feed

  1. kering says:

    usefull commands, thanks

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site