rm -rf ab returns find: `./ab': No such file or directory

Hi Gurus.

This is driving me a bit batty. I now if must be a simple matter but I cant find anything that references it.

I have a housekeeping script that searches for some huge dump directories then removes them using rm -rf.

find ./ -name 'ab' -exec rm -rf {} \;

This works but always returns the following

find: `./ab': No such file or directory

This would be ok on an "on the fly" delete, but I have this as part of a larger backup script and this message returns as something for us to error check.

Is there a way for me to delete a directory without getting this message returned?

Thanks!

You get the error because find cannot find the file 'ab', not from the rm command (-f removes that error).

You can add 2> /dev/null to the very end of the command to remove the error.

1 Like

Thanks Scott - that has resolved the issue.

Could I cheekily ask one more thing?

How do you mean that find cant find the file? Before the rm -rf it must find it in order to delete it, right? It just seems that after the rm -rf it tries to find the file again and then presents the error.

Why am I getting the error when the file is clearly there??

It's hard to know exactly where "there" is because I don't know what directory your script is in as it's being run.

But using -f with rm will suppress the "no such file or directory" error.

$ ll ab
ls: ab: No such file or directory
$ rm -f ab
$ <-- command prompt, i.e. no error message

Therefore, the error must come from find, not from rm.

1 Like
[dj@davidjlvm dj]$ pwd
/home/dj/dj
[dj@davidjlvm dj]$ ll
total 0
[dj@davidjlvm dj]$ mkdir ab
[dj@davidjlvm dj]$ ll
total 4
drwxrwxr-x. 2 dj dj 4096 Nov 12 15:26 ab
[dj@davidjlvm dj]$ find ./ -name 'ab' -exec rm -f {} \;
rm: cannot remove `./ab': Is a directory
[dj@davidjlvm dj]$ find ./ -name 'ab' -exec rm -rf {} \;
find: `./ab': No such file or directory
[dj@davidjlvm dj]$ ll
total 0
[dj@davidjlvm dj]$ 

find doesn't seem to like the trailing / in the path name.

Try removing it.

i.e.

find . -name .......

My find at least prints something more useful:

find: .//ab: No such file or directory
1 Like

When -depth is not used, the find command visits a directory before its contents. So, when find sees ab, it performs the -exec and afterwards tries to descend into ab to see what's there. That's where the error message comes from.

You could use -prune to prevent the descent.

find ./ -name 'ab' -exec rm -rf {} \; -prune 

Regards,
Alister

2 Likes

Thanks to all. This one is closed now. Got the workaround and got the explanation too.