Tuesday, October 21

xargs -0

In the unix and cygwin word, I frequently use a command line like
   find . -iname '*.abc' -print0 | xargs -0 whatever
(changing abc and whatever as I see fit).

Now, a few years back someone got the bright idea that the -print0 and -0 arguments to find and xargs were "bad' and marked them obsolete.  Fortunately, more recent copies of documentation seem to have lost this "upgrade".

There were two reasons this was bad:

[1] It was a gratuitous change.  It did not solve anything.  It made the code slightly simpler, but interfaces are more important.

[2] It would have made dealing with file and directory names with spaces in them nearly impossible.

There was one supposed reason that this was good:  

[A] Using null character delimiters does not fit very well with some of the other documentation about these commands.

But whoever came up with this idea did not bother thinking that through.

Anyways, a different approach -- a much better option -- would be to add to xargs an option to use newlines to delimit words.  This would require no changes to find, would allow the use of other commands between find and xargs and would do the right thing for file names with spaces in them.

This would do the wrong thing for file names with newlines in them, but you could still use the null terminator approach for them.  And when have you ever encountered a file name with a newline in it?  That's just broken. 

Actually, I think we should get some kind of command to walk through a directory structure and find file and directory names that are outside some reasonable range (perhaps printable ascii, perhaps printable unicode, perhaps those characters which correspond to some language or some code page, if you want to overengineer the thing) and give them unique names that are within that range, and also generate some specialized before/after report that says what was changed.  Or perhaps better, generate a rename script that properly escapes the problematic characters.  And if that's not possible it points out other areas of the OS which need to be fixed.  But that's another story.

For now: xargs NEEDS to have an option to recognize words delimited by newlines.

   find . -iname '*.abc'  | grep def | xargs whatever
no such file or directory c:\Documents






No comments:

Post a Comment