如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:56:41  来源:igfitidea点击:

How to echo directories containing matching file with Bash?

bashshellcommand-linefindgrep

提问by Grundlefleck

I want to write a bash script which will use a list of all the directories containing specific files. I can use findto 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

-printfformat

%hLeading 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 %hspecifier expands to ".".

-printf格式

%h文件名的前导目录(除最后一个元素外的所有目录)。如果文件名不包含斜杠(因为它在当前目录中),%h说明符扩展为".".

回答by xverges

On OS Xand FreeBSD, with a findthat lacks the -printfoption, this will work:

OS XFreeBSD 上find如果没有-printfoption,这将起作用:

find . -name *.class -print0 | xargs -0 -n1 dirname | sort --unique

The -n1in xargssets to 1 the maximum number of arguments taken from standard input for each invocation of dirname

-n1xargs从标准输入采取的每次调用参数设置为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