I usually use the command line bellow to remove directories on file system. But, for the 1st time find
is returning names with white spaces on it, and rm
doesn't recognize it.
find . -name .AppleDouble* -exec rm -rf {} \;
It throws the following error: find: './Aerosmith/Young Lust/.AppleDouble': No such file or directory
Is it possible to achieve it using just a single line like this one? Or should I use some kind of script? If the script is the solution, please, could you post an example?
You want
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 thatfind
, by default is bredth first, not depth first.find
has no idea what therm
command is doing, so after yourm
, 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:
And find will be smart enough to do the right thing.
(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.