Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
cat fileNames.txt | xargs find . -name

I expect the above command to find the path of every
fileName in fileNames.txt, but this command produces no
output. Why?

share|improve this question
up vote 4 down vote accepted

A solution would be:

 xargs --max-args=1 find . -iname < fileNames.txt
share|improve this answer
    
If fileNames.txt contains one file name (or rather pattern) per line (as opposed to file names quoted properly for xargs), use xargs -d '\n'. – Gilles Nov 15 '10 at 20:40
    
@Gilles: For xargs that supports it, I would use xargs --arg-file fileNames.txt ... instead of the redirection. – Dennis Williamson Nov 15 '10 at 23:03

Regular xargs call removes the newline characters, putting all the lines from input in single command line. You can however use the -I <pattern> option, where command will be called for each line of input, with <pattern> replaced with line contents.

Note also, that you should probably add the -print option to have the paths listed.

cat fileNames.txt | xargs -I {} find . -name {} -print
share|improve this answer
    
what I don't get is why do we need curly braces after -I? – dharm0us Nov 15 '10 at 10:53
    
this is a "pattern", which will be searched (and found after -name) to show the place where a filename from fileNames.txt should be inserted – akavel Nov 15 '10 at 11:20
    
@dta: You could just as easily use xargs -I % ... -name % ... – Dennis Williamson Nov 15 '10 at 23:03

The -name option takes just a single argument. I'm not sure why you're not getting an error message. Try

sed 's/.*/-name "&"/' fileNames.txt | xargs find .
share|improve this answer
    
A single quote is missing after sed. So the correct version is : sed 's/.*/-name "&"/' fileNames.txt | xargs find . – dharm0us Nov 15 '10 at 10:12

The answers so far have a hard time with filenames containing space, ' or ":

$ echo \'\ \" |  xargs -I {} find . -name {} -print
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

$ echo \'\ \" | sed 's/.*/-name "&"/' | xargs find .
xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
find: missing argument to `-name'

If the files are made by users, you will experience funny named files. If you have GNU Parallel installed you can do:

cat fileNames.txt | parallel find . -name {} -print

Watch the intro video to learn more about GNU Parallel: http://www.youtube.com/watch?v=OpaiGYxkSuQ

GNU Parallel can be downloaded at ftp://ftp.gnu.org/gnu/parallel/ and has been reported to be working under Cygwin.

share|improve this answer
    
Actually, akavel's answer is correct in that respect, as -I disables input quoting. (And to nitpick, reinierpost's answer deals with ' correctly, but fails on ".) – Gilles Nov 15 '10 at 20:42
    
how do I get parallel for Cygwin? which package does it belong to? – dharm0us Nov 17 '10 at 5:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .