views:

512

answers:

5

I'm trying to write a bash command that will delete all files matching a specific pattern - in this case, it's all of the old vmware log files that have built up.

I've tried this command:

find . -name vmware-*.log | xargs rm

However, when I run the command, it chokes up on all of the folders that have spaces in their names. Is there a way to format the file path so that xargs passes it to rm quoted or properly escaped?

+2  A: 

Check out the -0 flag for xargs; combined with find's -print0 you should be set.

find . -name vmware-*.log -print0 | xargs -0 rm
Carl Norum
+9  A: 

Try using:

find . -name vmware-*.log -print0 | xargs -0 rm

This causes find to output a null character after each filename and tells xargs to break up names based on null characters instead of whitespace or other tokens.

Emil
+3  A: 

Do not use xargs. Find can do it without any help:

find . -name "vmware-*.log" -exec rm '{}' \;

L.R.
It's always good to avoid starting an extra process, especially something like this where you could provide something way too long to xargs. Note that `find` even has a `delete` action you can use instead of the `-exec ...` - but it's easier to customize this way. You also don't have to quote the curly braces, unless you're using an old shell like tcsh.
Jefromi
But this will launch an `rm` process for each file individually, instead of passing several filenames to `rm` like `xargs` does, so it's going to be slower.
jk
@jk: That's why newer `find` implementations have `find -exec rm '{}' +`, which will batch up arguments just like `xargs` does.
ephemient
@jk: You're right about the processes. As for the speed, there are always exceptions, but in my experience, it's not the rm process-starting that dominates. If you're deleting enough files that the rm time wins out, it's the actual disk activity holding you up. Otherwise it's the find time that matters anyway (think 5 matches out of 10000 files).
Jefromi
@ephemient: Good call. I'd totally forgotten that!
Jefromi
@ephemient: Thanks, I didn't know about that!
jk
A: 

GNU find

find . -name vmware-*.log -delete
ghostdog74
A: 

find . -name vmware-*.log | xargs -i rm -rf {}

rh0dium