in reply to Re^2: UNIX command - remove 0 byte file
in thread UNIX command - remove 0 byte file

-delete is indeed a linuxism as opposed to a unix option of find, but xargs is indeed available under e.g. Solaris.

A potential problem with using xargs is that although it means only one execution of rm which bodes better performance, it also converts everything read in from the pipe into a single argument list, which above a certain limit will cause rm to Segmentation Fault, whereas -exec is guaranteed to work for an unlimited number of files. The choice therefore depends on the particular environment it is being applied to.

-M

Free your mind

  • Comment on Re^3: UNIX command - remove 0 byte file

Replies are listed 'Best First'.
Re^4: UNIX command - remove 0 byte file
by Anonymous Monk on Sep 23, 2005 at 12:48 UTC
    A potential problem with using xargs is that although it means only one execution of rm which bodes better performance, it also converts everything read in from the pipe into a single argument list

    No, it doesn't. xargs is smart enough that it knows the system limits, and splits of the task into several processes if needed. That's the beauty of xargs.

    $ find /opt/perl -print0 2> /dev/null | xargs -0 | wc -l 42 $
    Split up into 42 calls to /bin/echo (the default for xargs). Note also the use of -print0 and -0, that eliminates problems with filenames containing whitespace.
      Note also the use of -print0 and -0, that eliminates problems with filenames containing whitespace.
      I think that those are Linux-only features as well. I sure like them when I can use them, though. :)

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        Actually, they are not Linux-only feature. At worst, they are GNU-only features, and the GNU file/shell utilities happely run on all commonly used Unix platforms. And they work on Windows as well.
      Let's not forget the OP specifically said 'unix' rather than linux. Filenames with whitespace not being handled with xargs is certainly going to be a problem if this is supposed to be a generic solution. But if the whole file is enclosed in quotes when it has whitespace, rm will handle the argument list properly even if it went thru xargs:
      find /path/ -size 0 | perl -e 'while(<>) { chop; s/^(.*\s.*)$/\"$1\"/; + print "$_\n"; }' | xargs -n <whatever-filecount-limit> rm

      -M

      Free your mind

        Except that you now have problems if the filename contains double quotes. Or backquotes. Or dollars. If you want to do the quoting yourself, better wrap the arguments in single quotes, after first escaping any single quotes:
        find /path/ -size 0 | perl -ple 's/\x27/\x27"\x27"\x27/g; "\x27$_\x27"' | xargs -n ... rm
        Note the use of \x27 for single quote, lest it ends the argument to perl.

        Of course, if you're going to pipe into perl, there's not much point in calling xargs rm is there? Might as well do the removal from within perl:

        find /path/ -size 0 | perl -nle 'unlink or warn "unlink $_: $!\n"'
        Note that both pieces of code given above still fail on filenames containing newlines.
      I looked into this for xargs under Solaris and the manual makes no such claim, although it looks like you can do something similar manually using a combination of -l -s and -n options, assuming you know the system limits to target these parameters for.

      -M

      Free your mind

Re^4: UNIX command - remove 0 byte file
by mhacker (Sexton) on Sep 23, 2005 at 12:55 UTC
    Actually, xargs is supposed to handle that. It will break the command line when it gets too long (1024 words, I think) and run the command again.

    But the most annoying thing with xargs is that it doesn't handle spaces or other special characters well:

    server:~> ls a file server:~> ls | xargs rm a: No such file or directory file: No such file or directory
    You're forced to do tricks with sed (or perl -e) to get this to work.
      GNU xargs offers -i to deal with spaces.

      ls | xargs -i ls
      XargFoo