如何只在给定目录中查找文件,并使用 bash 忽略子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7715485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to only find files in a given directory, and ignore subdirectories using bash
提问by suffa
I looked at other similar questions, but didn't find one that would enable me to grasp the concept and make it applicable to my situation based on my limited time. I'm simply running the find command to find certain files, but some files in sub-directories have the same name which I want to ignore. Thanks for any help. Below is the command that I'm using:
我查看了其他类似的问题,但没有找到一个能让我在有限的时间内掌握概念并使其适用于我的情况的问题。我只是运行 find 命令来查找某些文件,但是子目录中的某些文件具有我想忽略的相同名称。谢谢你的帮助。下面是我正在使用的命令:
The files/pattern I'm interested in: /dev/abc-scanner, /dev/abc-cash ....
我感兴趣的文件/模式:/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/...
我想忽略后面的文件:/dev/.udev/...
回答by Mark Longair
If you just want to limit the find to the first level you can do:
如果您只想将查找限制在第一级,您可以执行以下操作:
find /dev -maxdepth 1 -name 'abc-*'
... or if you particularly want to exclude the .udev
directory, you can do:
...或者如果你特别想排除.udev
目录,你可以这样做:
find /dev -name '.udev' -prune -o -name 'abc-*' -print
回答by Stephen Darlington
Is there any particular reason that you need to use find
? You can just use ls
to find files that match a pattern in a directory.
您是否有任何特殊原因需要使用find
?您可以仅用于ls
查找与目录中的模式匹配的文件。
ls /dev/abc-*
If you do need to use find
, you can use the -maxdepth 1
switch to only apply to the specified directory.
如果确实需要使用find
,则可以使用-maxdepth 1
开关仅应用于指定目录。
回答by Chris J
This may do what you want:
这可能会做你想做的:
find /dev \( ! -name /dev -prune \) -type f -print
回答by Athur Snyder
find /dev -maxdepth 1 -name 'abc-*'
Does not work for me. It return nothing. If I just do '.' it gives me all the files in directory below the one I'm working in on.
对我不起作用。它什么都不返回。如果我只是做'.' 它为我提供了我正在处理的目录下的所有文件。
find /dev -maxdepth 1 -name "*.root" -type 'f' -size +100k -ls
Return nothing with '.' instead I get list of all 'big' files in my directory as well as the rootfiles/ directory where I store old ones.
用 '.' 不返回任何内容 相反,我得到了目录中所有“大”文件的列表以及存储旧文件的 rootfiles/ 目录。
Continuing. This works.
继续。这有效。
find ./ -maxdepth 1 -name "*.root" -type 'f' -size +100k -ls
564751 71 -rw-r--r-- 1 snyder bfactory 115739 May 21 12:39 ./R24eTightPiPi771052-55.root
565197 105 -rw-r--r-- 1 snyder bfactory 150719 May 21 14:27 ./R24eTightPiPi771106-2.root
565023 94 -rw-r--r-- 1 snyder bfactory 134180 May 21 12:59 ./R24eTightPiPi77999-109.root
719678 82 -rw-r--r-- 1 snyder bfactory 121149 May 21 12:42 ./R24eTightPiPi771098-10.root
564029 140 -rw-r--r-- 1 snyder bfactory 170181 May 21 14:14 ./combo77v.root
Apparently /dev
means directoryof interest. But ./
is needed, not just .
. The need for the /
was not obvious even after I figured out what /dev
meant more or less.
显然/dev
是指感兴趣的目录。但./
需要,而不仅仅是.
. /
即使在我弄清楚了/dev
或多或少的含义之后,对 的需求也不明显。
I couldn't respond as a comment because I have no 'reputation'.
我无法作为评论做出回应,因为我没有“声誉”。