Monday, April 10, 2006

Linux : Use of Pipes in commands

A pipe ( | ) is used to send output of one command to some other command. Many a time, use of pipe simplifies our task to a great extent. I'll discuss a few examples

Count number of files in a directory

ls | wc -l

List all users on a system who are running some processes (process owners)

ps -ef | cut -d " " -f1 | sort | uniq

List all process-listings that contain the word "mozilla"

ps -ef | grep mozilla

Find the word "services" in all .htm files

find . -name "*.htm" | xargs grep "services"

Sort lines in a file and print on the console

cat filename | sort

Killing mozilla

ps -ef | grep "mozilla" | cut -f1 | xargs kill

Print last 10 lines of a file

tail filename | lpr

Here's a good page on Linux Tips.

This wikipedia page has more information on unix pipes.

2 comments:

Anonymous said...

Hey actually this pipe commands design is based on pipe and filter architecture style. This type of style is very useful for frequent modification or addition of new components (filter). Actually I heard unix pipe command example during my architecture course.

Shrinivas

Ketan said...

You are right. Pipes and filters are related. See the wikipedia article.