如何使用 bash 仅查找给定目录中的文件并忽略子目录
我正在运行 find
命令来查找某些文件,但子目录中的某些文件具有相同的名称,我想忽略它们。
我对这样的文件/模式感兴趣:
/dev/abc-scanner, /dev/abc-cash ....
命令:
find /dev/ -name 'abc-*'
返回的内容:
/dev/abc-scanner
/dev/abc-cash
...
...
...
/dev/.udev/names/abc-scanner
/dev/.udev/names/abc-cash
我想忽略后面的文件:/dev/.udev/...
I'm running the find
command to find certain files, but some files in sub-directories have the same name which I want to ignore.
I'm interested in files/patterns like this:
/dev/abc-scanner, /dev/abc-cash ....
The command:
find /dev/ -name 'abc-*'
What's being returned:
/dev/abc-scanner
/dev/abc-cash
...
...
...
/dev/.udev/names/abc-scanner
/dev/.udev/names/abc-cash
I want to ignore the latter files: /dev/.udev/...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您只想将查找限制在第一级,您可以执行以下操作:
...或者如果您特别想排除
.udev
目录,您可以执行以下操作:请注意
/dev< /code> 是问题的目录;您需要将其替换为您要搜索的目录。
If you just want to limit the find to the first level you can do:
... or if you particularly want to exclude the
.udev
directory, you can do:Note that
/dev
is the directory from the question; you will want to replace that with the directory you wish to search.您需要使用
find
有什么特殊原因吗?您只需使用 ls 即可查找与目录中的模式匹配的文件。如果您确实需要使用
find
,则可以使用-maxdepth 1
开关仅应用于指定目录。Is there any particular reason that you need to use
find
? You can just usels
to find files that match a pattern in a directory.If you do need to use
find
, you can use the-maxdepth 1
switch to only apply to the specified directory.这可能会做你想做的事:
This may do what you want:
我遇到了一个更普遍的问题 - 我想在匹配模式的目录中查找文件,但不在其子目录中查找文件。
我的解决方案(假设我们正在寻找直接位于
arch
目录中的所有cpp
文件):find . -path "*/arch/*/*" -prune -o -path "*/arch/*.cpp" -print
我无法使用 max深度,因为它首先限制了搜索,并且没有'不知道我想排除的子目录的名称。
I got here with a bit more general problem - I wanted to find files in directories matching pattern but not in their subdirectories.
My solution (assuming we're looking for all
cpp
files living directly inarch
directories):find . -path "*/arch/*/*" -prune -o -path "*/arch/*.cpp" -print
I couldn't use maxdepth since it limited search in the first place, and didn't know names of subdirectories that I wanted to exclude.
有一种替代品叫做生皮 (rh),它更容易使用。而不是:
你可以这样做:
-r 与“-m1 -M1”相同,与 find 的“-min深度 1 -max深度 1”相同,只是短得多。
Rawhide (rh) 可从 https://raf.org/rawhide 或 https://github.com/raforg/rawhide。它至少可以在 Linux、FreeBSD、OpenBSD、NetBSD、Solaris、macOS 和 Cygwin 上运行。
免责声明:我是 rawhide 的当前作者
There is an alternative to find called rawhide (rh) and it's much easier to use. Instead of:
You can do:
The -r is the same as "-m1 -M1" which is the same as find's "-mindepth 1 -maxdepth 1", just a lot shorter.
Rawhide (rh) is available from https://raf.org/rawhide or https://github.com/raforg/rawhide. It works at least on Linux, FreeBSD, OpenBSD, NetBSD, Solaris, macOS, and Cygwin.
Disclaimer: I am the current author of rawhide
如果您尝试搜索当前目录,请确保使用
./
作为目录,而不仅仅是.
如果我只是执行 '.'它为我提供了我正在工作的目录下的所有文件:
相反,您会想要类似的东西(此特定示例查找当前目录中高于特定大小的所有文件):
If you are trying to search the current directory, make sure that you use
./
as the directory, and not just.
If I just do '.' it gives me all the files in the directory below the one I'm working in:
Instead you will want something like (this specific example finds all files in the current directory above a specific size):