1

I'm trying to understand how to delete directories using the Linux find command with -exec option, without getting a "No such file or directory" response. On my Raspberry Pi I go to /home/pi and type: mkdir testing So now I have created a new directory named /home/pi/testing. Next I try to delete the directory using this command:

find /home/pi/testing -type d -exec rm -rf {} \; 

I get the following response:

find: '/home/pi/testing': No such file or directory 

But the /home/pi/testing directory is gone. So the command seemed effective, but why the "No such file or directory" error?

1 Answer 1

6

Find is trying to enter the directory after it has been deleted. If you add the -depth option to your command line, find will run in depth-first mode in which it will attempt to process the contents of the directory before deleting it:

find /home/pi/testing -depth -type d -exec rm -rf {} \; 

That said, your find command line isn't very useful: for this operation, you would generally just run rm -rf /home/pi/testing.

Sign up to request clarification or add additional context in comments.

1 Comment

This helped, thanks a lot! Yes, ultimately I want to use the find command for more advanced operations than this, but wanted to make the example as simple as possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.