Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Piping commands in GNU/Linux

Interesting thing in shell programming is piping of commands.

Which means the output of a command instead of displaying on the screen, becomes input to another command.

We can do a lot of magic using this feature, mainly while writing shell scripts.

Suppose we want to know how mainy files are available in current directory.

$ ls | wc -l

will do the trick.

Here the command ls  lists the files present in the current directory. The symbol '|' redirects the output of first command to the second command viz. wc -l.  The second command basically counts the lines in standard input and prints the result, ie the number of files presents in the directory.

Let us see another example.

If we want to see the RAM consumption using command mode, we use the free command. The output is like the following.


$ free
       total       used      free   shared    buffers     cached
Mem:   1018220     993360    24860       0     334284     153740
-/+ buffers/cache: 505336   512884
Swap:  1015800        116  1015684



In the above output we are interested in only free memory ie. the value of the 4th column in second line. For getting the value of free memory alone we need write our command as follows.

First filter out the second line using the following pipe.

free | head -2 | tail -1

The command head -2 cuts the first two lines from the output and directs the output to the last command tail -1 which will cut one line from the bottom and gives output. The resulting output would be as follows.

Mem:   1018220     993360    24860       0     334284     153740

But this is not what we are interested in. we want exactly the value of the free memory. For achieving that we need to implement few more commands piped.

First we reduce the space between the columns to one.


free | head -2 | tail -1 | tr -s "  " " "


This will reduce the spaces between the columns into one makiing it into a trackable array. The resulting output would be...


Mem: 1018220 993360 24860 0 334284 153740


Now using the last command piped, we get the final result.


free | head -2 | tail -1 | tr -s "  " " " | cut -d " " -f 4


The last command cut basically cuts the portion of text in the form of columns. Since we have only only line in our case, the column number 4 is cut and resulted value is what we wanted, the free memory. The result is...

24860


So, using this we can directly get the value of free memory of the system. This method is very much useful in writing shell scripts where we need to access lot of system data.

Suppose in other application programming languages like php, we can directly pass the command to get the required output.

<?php


$memory = system("free | head -2 | tail -1 | tr -s "  " " " | cut -d " " -f 4");


echo "Free memory available: $memory";


?>

The above sample code demonstrates the functionality.


This post first appeared on Techno Fervor, please read the originial post: here

Share the post

Piping commands in GNU/Linux

×

Subscribe to Techno Fervor

Get updates delivered right to your inbox!

Thank you for your subscription

×