[PLUG] filenames with spaces, shell programming ...

Russell Senior seniorr at aracnet.com
Fri May 20 18:19:02 UTC 2005


>>>>> "Paul" == Paul Heinlein <heinlein at madboa.com> writes:

Russell> I have got a file containing file names with spaces and
Russell> exclamation marks, etc.  I want to, e.g., cmp -v two versions
Russell> of these files, something like: $ cmp -v $DIR1/$foo
Russell> $DIR2/$foo When I stupidly do something like: $ for foo in
Russell> $(cat fnames) ; do echo $foo ; cmp -v $DIR1/$foo $DIR2/$foo ;
Russell> done the filenames get split at spaces in the filenames.  How
Russell> do I keep them from being split?

Paul> I typically combine find -print0 with xargs -0, though it might
Paul> be tough to duplicate your loop exactly. In a directory
Paul> containing files or directories with spaces in their names,
Paul> compare these three commands:

Paul> 1. find * -prune | xargs stat 2. for f in *; do stat $f; done
Paul> 3. find * -prune -print0 | xargs -0 stat

Paul> The first two do roughly the same thing: tokenize filenames on
Paul> whitespace so that you end up with bad results or errors.

Paul> The last one does the right thing: find null-terminates the
Paul> results and xargs splits on the null terminator, not whitespace.

Problem is, I am more or less stuck with the file of filenames as a
starting point.  I need to re-tokenize them, I guess, but how?


-- 
Russell Senior         ``I have nine fingers; you have ten.''
seniorr at aracnet.com



More information about the PLUG mailing list