找到:`./folder':没有这样的文件或目录 [英] find: `./folder': No such file or directory

查看:113
本文介绍了找到:`./folder':没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  misha @ hp-laptop:〜/ work / c / 5 $ ls -l 
total 8
-rw-rw-r_1 1 misha misha 219 5月20日15:37 demo.c
drwxrwxr-x 2 misha misha 4096 5月20日测试

现在我想从该目录中除去文件 demo.c 的所有内容。下面是我想到的命令:

  find。 ! \(-name demo.c -o -name。\)-exec rm -Rf {} \; 

它的确如你所想的那样(意思是 test 和目录文件夹都没有了),但同时它也显示以下错误信息:

  find:`./folder':没有这样的文件或目录

你为什么这样认为?

解决方案


显示这个错误信息:

$ $ $ $ $ $ $ $ $ $ $ $ b $ find $


$ b

为什么会这样?

因为 find 识别 ./文件夹作为目录,当它第一次读取目录 find 标准或对其执行任何操作。它不能识别该动作将删除该目录,因此在执行动作之后,它将尝试下降到该目录以扫描其内容。但是,到目前为止,该目录不再存在。



有多种方法可以解决这个问题。还没有提到的是使用 -prune 操作。这告诉 find 不能下到符合测试的目录中:

$ p $ 找 。 ! \(-name demo.c -o -name。\)-exec rm -Rf {} \; -prune

在这里可以很好地发挥作用,而且它也有应用程序在你不删除目录,你不想限制搜索深度。



另外,避免影响的另一种方法是使用 find 接受多个基本路径进行测试的事实,如果您愿意,可以指定常规文件,并且在路径名扩展期间,任何前导在文件名中必须显式匹配。如果在你的情况下目标目录中没有点文件(除了 .. ),那么你可以完成你的目标是这样的:

  find *! -name demo.c -exec rm -Rf {} \; -prune 


This is the contents of the directory I'm working with:

misha@hp-laptop:~/work/c/5$ ls -l
total 8
-rw-rw-r-- 1 misha misha  219 May 20 15:37 demo.c
drwxrwxr-x 2 misha misha 4096 May 20 16:07 folder
-rw-rw-r-- 1 misha misha    0 May 20 16:06 test

Now I would like to remove everything from this directory except for the file demo.c. Here's the command I've come up with:

find . ! \( -name demo.c -o -name . \) -exec rm -Rf {} \;

It does exactly what you'd think it would do (meaning, the file test and the directory folder are gone), but at the same time it also displays the following error message:

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

Why do you think that is?

解决方案

it also displays this error message:

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

Why is that?

Because find recognizes ./folder as a directory when it first reads directory ., before considering whether it matches the find criteria or performing any action on it. It does not recognize that the action will remove that directory, so after performing the action, it attempts to descend into that directory to scan its contents. By the time it does that, however, the directory no longer exists.

There are multiple ways to address the problem. One not yet mentioned is to use the -prune action. This tells find not to descend into directories that match the tests:

find . ! \( -name demo.c -o -name . \) -exec rm -Rf {} \; -prune

That will serve nicely here, and it also has applications in areas where you are not deleting the directory and you do not want to limit the search depth.

Additionally, another way to avoid affecting . would be to make use of the fact that find accepts multiple base paths to test, that these can designate regular files if you wish, and that during pathname expansion any leading . in a filename must be matched explicitly. If, as in your case, there are no dotfiles in the target directory (other than . and ..), then you can accomplish your objective like this:

find * ! -name demo.c -exec rm -Rf {} \; -prune

这篇关于找到:`./folder':没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆