Linux 如何在shell中的递归目录中列出特定类型的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3528460/
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 list specific type of files in recursive directories in shell?
提问by Kumar Alok
How can we find specific type of files i.e. doc pdf files present in nested directories.
我们如何找到特定类型的文件,即嵌套目录中存在的 doc pdf 文件。
command I tried:
我试过的命令:
$ ls -R | grep .doc
but if there is a file name like alok.doc.txt
the command will display that too which is obviously not what I want. What command should I use instead?
但是如果有像alok.doc.txt
命令这样的文件名也会显示,这显然不是我想要的。我应该使用什么命令?
采纳答案by Benoit Courtine
If you are more confortable with "ls" and "grep", you can do what you want using a regular expression in the grep command (the ending '$' character indicates that .doc must be at the end of the line. That will exclude "file.doc.txt"):
如果你更喜欢“ls”和“grep”,你可以在grep命令中使用正则表达式做你想做的事情(结尾的'$'字符表示.doc必须在行尾。那将排除“file.doc.txt”):
ls -R |grep "\.doc$"
More information about using grep with regular expressions in the man.
有关在man 中使用带有正则表达式的 grep 的更多信息。
回答by ghostdog74
ls
command output is mainly intended for reading by humans. For advanced querying for automated processing, you should use more powerful find
command:
ls
命令输出主要供人类阅读。对于自动处理的高级查询,您应该使用更强大的find
命令:
find /path -type f \( -iname "*.doc" -o -iname "*.pdf" \)
As if you have bash 4.0++
好像你有 bash 4.0++
#!/bin/bash
shopt -s globstar
shopt -s nullglob
for file in **/*.{pdf,doc}
do
echo "$file"
done
回答by Sergiy Kolodyazhnyy
Some of the other methods that can be used:
可以使用的其他一些方法:
echo *.{pdf,docx,jpeg}
echo *.{pdf,docx,jpeg}
stat -c %n * | grep 'pdf\|docx\|jpeg'
stat -c %n * | grep 'pdf\|docx\|jpeg'
回答by Will C
Similarly if you prefer using the wildcard character *
(not quite like the regex suggestions) you can just use ls
with both the -l
flag to list one file per line (like grep) and the -R
flag like you had. Then you can specify the files you want to search for with *.doc
I.E. Either
同样,如果您更喜欢使用通配符*
(不太像正则表达式的建议),您可以ls
同时使用-l
标志来列出每行一个文件(如 grep)和-R
您拥有的标志。然后你可以指定你想用*.doc
IE搜索的文件
ls -l -R *.doc
or if you want it to list the files on fewer lines.
或者如果您希望它在更少的行上列出文件。
ls -R *.doc
回答by Leo Bastin
find . | grep "\.doc$"
This will show the path as well.
这也将显示路径。
回答by dead parrot software
We had a similar question. We wanted a list - with paths - of all the config files in the etc directory. This worked:
我们有一个类似的问题。我们想要一个包含路径的列表,其中包含 etc 目录中的所有配置文件。这有效:
find /etc -type f \( -iname "*.conf" \)
It gives a nice list of all the .conf file with their path. Output looks like:
它提供了所有 .conf 文件及其路径的漂亮列表。输出看起来像:
/etc/conf/server.conf
But, we wanted to DO something with ALL those files, like grep those files to find a word, or setting, in all the files. So we use
但是,我们想对所有这些文件做一些事情,例如 grep 这些文件以在所有文件中查找单词或设置。所以我们用
find /etc -type f \( -iname "*.conf" \) -print0 | xargs -0 grep -Hi "ServerName"
to find via grep ALL the config files in /etc that contain a setting like "ServerName" Output looks like:
通过 grep 查找 /etc 中包含“ServerName”等设置的所有配置文件,输出如下所示:
/etc/conf/server.conf: ServerName "default-118_11_170_172"
Hope you find it useful.
希望你觉得它有用。
Sid
锡德