a mARTIAN dIARY

Of xargs and pipes

Filed under: TECHbabble — cafm @ 1:12 pm January 11, 2007

Linux is built on many principles but one thing that can be generalized, is KISS ;) (Keep It Simple Stupid). But the KISS is a KISS applied on the generalization itself as you can not surprisingly find various violations to this rule. One of the collieries of the same is "Rule of Composition: - Design programs to be connected to other programs". For a person who lost his virginity to windows, and who the twisted casting director called fate has conferred the role of an electronics engineer, this is one of the most lovable things about Linux after maybe command auto complete. Now after all this blah blah…I come to the point….

For those less informed or plainly bored Pipelining is what I was talking about in the first paragraph. Pipelining is the process of directing the output of one program directly to the input of another file seamlessly in the shell itself. Now enough has been said about it bu here my two pence worth. :P

See a command like grep "mars" solarsystem_txt will search for the text "mars" in the txt file solarsystem_txt.

Now consider unrelated command ls. It lists the files in a current directory.

Now the feature of pipelining lets you take the input of ls and give it as the input of grep.

ls | grep "mars" would output all the files with mars in their file name.

would output all the files with mars in their file name. would output all the files with mars in their file name.

Cool huh?

But what if you wanted to search not only the current directory but all subdirectories for a file with mars in the file name?

Let’s introduce another command find which outputs all the filenames recursively from the root folder you say. So a find . would list out all the filenames from the start of the current directory( signified by .) and downwards.

Now you would do a find . | grep "mars"

Ok I want to go one step further I want to search not file names, but inside files within all the subdirectories for "mars". The solution would be

find . | xargs grep "mars"

What xargs does is that it takes each line output by find and executes a grep "mars" on that. Presto :P output taiyaar

One thing I would like to say rather emphasize is the Rule of Diversity, there is not "one true way" to do a thing is linux, you can do the same thing is umpteen number of ways some ways better than the other. What was done in the above post can be surely done in at least 10 other ways of the top of my mind. But it’s the concept that’s important.

So finally ……Use your freedom and KISS :P

0 Return Tranmissions... »

No comments yet.

Subscribe to Comments Feed

Get updated via email for a new comment on this blog

Leave a comment



Disclaimer
The thoughts expressed in this blog are mine and should in no manner be linked to the organization(s) with which I am (or have been) associated.