Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I tried to understand the usage of xargs and did the following experiment.

ls | xargs | touch

I want to refresh the files dates and directoris in the curent directory.

Though it is a bit silly,for I could use a simpler form to achieve the same effect.

In my mind, xargs reads from the STDIN and turn it into the arguments for the other command(/bin/echo by default if the command is not specified). Am I misunderstanding something?

It failed and I am wondering why.

share|improve this question
    
It is recommended not to parse ls output. –  Francesco Turco Oct 14 '12 at 14:47
add comment

1 Answer 1

up vote 8 down vote accepted

It needs to be like this:

ls | xargs touch

The xargs command runs the touch command with a number of strings read from stdin. In your case, stdin for xargs is the output end of the pipe from ls.

The way you had the command:

ls | xargs | touch

xargs had no command to run against the strings (filenames) it would read from stdin. In that case, xargs simply prints each file name, and touch gets the list of file names on its standard input. But touch doesn't read from its standard input, and since you didn't give it any arguments, it should have printed an error message like:

touch: missing file operand
Try `touch --help' for more information.

(which you should have mentioned in your question).

share|improve this answer
    
Ah, so that's how it works. +1 –  MaxMackie Dec 15 '11 at 4:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.