简体   繁体   English

使用带有 -exec 选项的 find 时“没有这样的文件或目录”

[英]"No such file or directory" when using find with -exec option

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.我试图了解如何使用带有 -exec 选项的 Linux find 命令删除目录,而不会收到“没有这样的文件或目录”响应。 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.在我的 Raspberry Pi 上,我转到 /home/pi 并输入: mkdir testing所以现在我创建了一个名为 /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.但是 /home/pi/testing 目录不见了。 So the command seemed effective, but why the "No such file or directory" error?所以该命令似乎有效,但为什么会出现“没有这样的文件或目录”错误?

Find is trying to enter the directory after it has been deleted. Find 正在尝试进入已删除的目录。 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:如果在命令行中添加-depth选项, find将以深度优先模式运行,在该模式下它将尝试在删除目录之前处理目录的内容:

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 .也就是说,您的find命令行不是很有用:对于此操作,您通常只需运行rm -rf /home/pi/testing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM