Linux command to remove directories with white spaces
Solution 1:
You want
find <whatever> -print0 | xargs -0 rm -rf
Solution 2:
Contrary to what people will first assume is the problem, the problem you are having is NOT with the spaces in the filename. {}
will correctly pass the filename to rm. The problem you are having is that find
, by default is bredth first, not depth first. find
has no idea what the rm
command is doing, so after you rm
, find is trying to then cd into the directory which you just removed, and you get a file not found error, because the directory it tries to cd into is gone.
@womble's answer works because it delays removing the directory. It will search through the directory for other things to remove from the directory first. This is not effecient, since its silly to look for files to remove in a directory if you are going to remove that entire directory anyway.
In GNU find, you can use the -delete action:
find . -name .AppleDouble* -delete
And find will be smart enough to do the right thing.
find . -name .AppleDouble* -exec rm -rf {} +
(using + instead of ; to terminate the comand) Is equivalent to @womble's command using xargs, it will have the effect of buffering actions until it scans the entire directory or runs out of room on the command line to rm, but it will suffer from the same problem in that it is uselessly searching for files to remove that are definitely going to be removed anyway.