3 ms·
find ./ -name '*.log' | xargs rm Only do that if you know exactly what '*.log' will expand to (i.e. don't use it in scripts and avoid using it on the command l
by planckscnst 14y ago
find ./ -name '*.log' | xargs rm
Only do that if you know exactly what '*.log' will expand to (i.e. don't use it in scripts and avoid using it on the command line). This is because the delimiter for xargs is a newline character, but filenames can have a newline character in them. This can lead to unexpected results.
Almost everywhere I see xargs used, find ...-exec {} ; will work as well and find ...-exec {} + may work even better.
- gnaritas 14y agofind ./ -name '*.log' -print0 | xargs -0 rm Fixes that issue and xargs is far more efficient, it doesn't launch a new process for each line like exec does, but far more importantly, xargs is generalizable to all commands so you only have to learn it once; exec is just an ugly hack on find, you can't generalize it across all commands; xargs is much more unixy.
- masklinn 14y ago> it doesn't launch a new process for each line like exec does Exec doesn't either if you use "+" as a terminator: find . $(options) -exec command {} \; executes one command per match, find . $(options) -exec command {} + executes a single command with all matches > exec is just an ugly hack on find Obvious and complete nonsense, -exec is both an important predicate of find and a useful and efficient tool. -print0/xargs -0, now that is a hack.
- gnaritas 14y agoYou apparently missed the part where I said "but far more importantly".
- ralph 14y agoWanting to remove the files was so common that some finds have it built-in. Avoids even the overhead of -exec rm {} +. find -name '*.log' -delete
- gnaritas 14y agoTrue, but the issue isn't removing files, the issue is generalizing a command for mapping output of a command to another command. rm was just a simple example. xargs is far more useful than simply deleting files.