April 13, 2009

UNIX case study - 16

Forcing pipe output

Pipe does not necessarily pass on the information to the next command unless the next command "listen" to the input. In the following example, we are getting the file list from the current directory.

# locate my.cnf | ls -lt | head
total 19136
-rw-r--r-- 1 root root 9557 Apr 13 14:52 1304.txt
drwxr-xr-x 8 root root 4096 Apr 13 13:19 Desktop
drwxr-xr-x 14 root root 4096 Apr 13 12:12 firefox

You need to use xargs command to forcefully hand over the output of the first command to the second command that does not behave.

# locate my.cnf | xargs ls -lt
-rw-r--r-- 1 root root 5299 Mar 6 12:35 /etc/mysql/my.cnf
-rw-r--r-- 1 root root 4321 Jan 21 10:16 /root/daily/todel/todel_shantanu/my.cnf
-rw-r--r-- 1 root root 3017 Aug 13 2008 /etc/my_cnf_bckup/my.cnf.rpmsave

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.