如何使用 Bash 回显包含匹配文件的目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2282686/
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 echo directories containing matching file with Bash?
提问by Grundlefleck
I want to write a bash script which will use a list of all the directories containing specific files. I can use find
to echo the path of each and every matching file. I only want to list the path to the directory containing at least one matching file.
我想编写一个 bash 脚本,它将使用包含特定文件的所有目录的列表。我可以find
用来回显每个匹配文件的路径。我只想列出包含至少一个匹配文件的目录的路径。
For example, given the following directory structure:
例如,给定以下目录结构:
dir1/
matches1
matches2
dir2/
no-match
The command (looking for 'matches*'
) will only output the path to dir1
.
命令(寻找'matches*'
)只会输出到 的路径dir1
。
As extra background, I'm using this to find each directory which contains a Java .class file.
作为额外的背景,我使用它来查找包含 Java .class 文件的每个目录。
回答by John Kugelman
find -name '*.class' -printf '%h\n' | sort -u
From man find
:
来自man find
:
-printf
format
%h
Leading directories of file's name (all but the last element). If the file name contains no slashes (since it is in the current directory) the%h
specifier expands to"."
.
-printf
格式
%h
文件名的前导目录(除最后一个元素外的所有目录)。如果文件名不包含斜杠(因为它在当前目录中),%h
说明符扩展为"."
.
回答by xverges
On OS Xand FreeBSD, with a find
that lacks the -printf
option, this will work:
在OS X和FreeBSD 上,find
如果没有-printf
option,这将起作用:
find . -name *.class -print0 | xargs -0 -n1 dirname | sort --unique
The -n1
in xargs
sets to 1 the maximum number of arguments taken from standard input for each invocation of dirname
的-n1
在xargs
从标准输入采取的每次调用参数设置为1的最大数量dirname
回答by ghostdog74
GNU find
GNU 查找
find /root_path -type f -iname "*.class" -printf "%h\n" | sort -u
回答by bufh
Ok, i come way too late, but you also could do it without find, to answer specifically to "matching file with Bash" (or at least a POSIX shell).
好吧,我来得太晚了,但你也可以在没有找到的情况下做,专门回答“使用 Bash 匹配文件”(或至少是 POSIX shell)。
ls */*.class | while read; do
echo ${REPLY%/*}
done | sort -u
The ${VARNAME%/*}
will strip everything after the last /
(if you wanted to strip everything after the first, it would have been ${VARNAME%%/*}
).
在${VARNAME%/*}
将剥离后的最后一切/
(如果你想第一之后剥离的一切,它会一直${VARNAME%%/*}
)。
Regards.
问候。
回答by davidqshull
Far too late, but this might be helpful to future readers: I personally find it more helpful to have the list of folders printed into a file, rather than to Terminal (on a Mac). For that, you can simply output the paths to a file, e.g. folders.txt, by using:
太晚了,但这可能对未来的读者有所帮助:我个人认为将文件夹列表打印到文件中比打印到终端(在 Mac 上)更有帮助。为此,您可以简单地使用以下命令输出文件的路径,例如 folders.txt:
find . -name *.sql -print0 | xargs -0 -n1 dirname | sort --unique > folders.txt
find . -name *.sql -print0 | xargs -0 -n1 dirname | sort --unique > folders.txt
回答by Jim
find / -name *.class -printf '%h\n' | sort --unique