El Jorge El Jorge - 2 months ago 6x
Linux Question

find command in bash script resulting in "No such file or directory" error only for directories?

UPDATE 2014-03-21

So I realized I wasn't as efficient as I could be, as all the disks that I needed to "scrub" were under

and named "
, etc." Here's the final script:

find $DIRTY_DIR -depth -type d -name .AppleDouble -exec rm -rf {} \;
find $DIRTY_DIR -depth -type d -name .AppleDB -exec rm -rf {} \;
find $DIRTY_DIR -depth -type d -name .AppleDesktop -exec rm -rf {} \;
find $DIRTY_DIR -type f -name ".*DS_Store" -exec rm -f {} \;
find $DIRTY_DIR -type f -name ".Thumbs.db" -exec rm -f {} \; # I know, I know, this is a Windows file.

Next will probably to just clean up the code even more, and add features like logging and reporting results (through e-mail or otherwise); excluding system and directories; and allowing people to customize the list of files/directories.

Thanks for all the help!


Before I incorporated the helpful suggestions provided by everyone, I performed some tests, the results of which were very interesting (see below).

As a test, I ran this command:

root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec echo rm -rf {} \;

The results (which is what I expected):

rm -rf /media/disk3/Videos/Chorus/.AppleDouble

However, when I ran the actual command (without

root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;

I received the same "error" output:

find: `/media/disk3/Videos/Chorus/.AppleDouble': No such file or directory

I put "error" in quotes because obviously the folder was removed, as verified by immediately running:

root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;

It seems like the
command stored the original results, acted on it by deleting the directory, but then tried to delete it again? Or is the
option of
, which is supposed to be for ignoring nonexistent files and arguments, is ignored? I note that when I run tests with the
command alone without the
command, everything worked as expected. Thus, directly running
rm -rf ... \nonexistent_directory
, no errors were returned even though the "non_existent_directory" was not there, and directly running
rm -r \nonexistent_directory
provided the expected:

rm: cannot remove 'non_existent_directory': No such file or directory

Should I use the
option instead of the
-exec rm ...
option? I had wanted to make the script as broadly applicable as possible for systems that didn't have
option for

Lastly, I don't presume it matters if
, ... are combined in an AUFS filesystem under
as the
command is operating on the individual disks themselves?
Thanks for all the help so far, guys. I'll publish the script when I'm done.


I'm writing a bash script to delete a few OS X remnants on my Lubuntu file shares. However, when executing this:

BASE_DIR="/media/disk" # I have 4 disks: disk1, disk2, ...

while [ $COUNTER -lt 5 ]; do # Iterate through disk1, disk2, ...
DIRTY_DIR=${BASE_DIR}$COUNTER # Look under the current disk counter /media/disk1, /media/disk2, ...
find $DIRTY_DIR -name \.AppleDouble -exec rm -rf {} \; # Delete all .AppleDouble directories
find $DIRTY_DIR -name ".*DS_Store" -exec rm -rf {} \; # Delete all .DS_Store and ._.DS_Store files

I see the following output:

find: `/media/disk1/Pictures/.AppleDouble': No such file or directory

Before I added the "-exec rm ..." portion the script found the /media/disk1/Pictures/.AppleDouble directory. The script works properly for removing DS_Store files, but what am I missing for the find command for directories?

I'm afraid to screw too much with the -exec portion as I don't want to obliterate directories in error.


You don't need to escape DOT in shell glob as this is not regex. So use .AppleDouble instead of \.AppleDouble:

find $DIRTY_DIR -name .AppleDouble -exec rm -rf '{}' \;

PS: I don't see anywhere $COUNTER being incremented in your script.