如何在shell中列出递归目录中特定类型的文件?

发布于 2024-09-15 04:59:13 字数 186 浏览 3 评论 0原文

我们如何找到嵌套目录中存在的特定类型的文件,即 doc pdf 文件。

我尝试过的命令:

$ ls -R | grep .doc

但是如果有像 alok.doc.txt 这样的文件名,该命令也会显示该文件名,这显然不是我想要的。我应该使用什么命令?

How can we find specific type of files i.e. doc pdf files present in nested directories.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱,才寂寞 2024-09-22 04:59:13

如果您更喜欢使用“ls”和“grep”,则可以在 grep 命令中使用正则表达式执行您想要的操作(结尾的“$”字符表示 .doc 必须位于行的末尾。这将排除“file.doc.txt”):

ls -R |grep "\.doc$"

有关使用 grep 与正则表达式的更多信息,请参阅 man。

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 -R |grep "\.doc$"

More information about using grep with regular expressions in the man.

百合的盛世恋 2024-09-22 04:59:13

ls 命令输出主要供人类阅读。对于自动化处理的高级查询,您应该使用更强大的 find 命令:

find /path -type f \( -iname "*.doc" -o -iname "*.pdf" \) 

就像您有 bash 4.0++

#!/bin/bash
shopt -s globstar
shopt -s nullglob
for file in **/*.{pdf,doc}
do
  echo "$file"
done

ls command output is mainly intended for reading by humans. For advanced querying for automated processing, you should use more powerful find command:

find /path -type f \( -iname "*.doc" -o -iname "*.pdf" \) 

As if you have bash 4.0++

#!/bin/bash
shopt -s globstar
shopt -s nullglob
for file in **/*.{pdf,doc}
do
  echo "$file"
done
浅暮の光 2024-09-22 04:59:13
find . | grep "\.doc$"

这也将显示路径。

find . | grep "\.doc$"

This will show the path as well.

枫林﹌晚霞¤ 2024-09-22 04:59:13

可以使用的其他一些方法:

echo *.{pdf,docx,jpeg}

stat -c %n * | grep 'pdf\|docx\|jpeg'

Some of the other methods that can be used:

echo *.{pdf,docx,jpeg}

stat -c %n * | grep 'pdf\|docx\|jpeg'

苍白女子 2024-09-22 04:59:13

我们也有类似的问题。我们想要一个包含路径的列表,其中包含 etc 目录中的所有配置文件。这很有效:

find /etc -type f \( -iname "*.conf" \)

它提供了所有 .conf 文件及其路径的良好列表。输出看起来像:

/etc/conf/server.conf

但是,我们想对所有这些文件执行一些操作,例如 grep 这些文件以在所有文件中查找单词或设置。因此,我们

find /etc -type f \( -iname "*.conf" \) -print0 | xargs -0 grep -Hi "ServerName"

通过 grep 查找 /etc 中包含“ServerName”等设置的所有配置文件 输出如下:

/etc/conf/server.conf: ServerName "default-118_11_170_172"

希望您发现它有用。

席德

We had a similar question. We wanted a list - with paths - of all the config files in the etc directory. This worked:

find /etc -type f \( -iname "*.conf" \)

It gives a nice list of all the .conf file with their path. Output looks like:

/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

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:

/etc/conf/server.conf: ServerName "default-118_11_170_172"

Hope you find it useful.

Sid

听你说爱我 2024-09-22 04:59:13

如果您的文件扩展名与文件类型不匹配,您可以使用file实用程序。

find $PWD -type f -exec file -N \{\} \; | grep "PDF 文档" | awk -F: '{print $1}'

您可以使用要开始搜索的目录来代替 $PWDfile 甚至打印出 he PDF版本。

If you have files with extensions that don't match the file type, you could use the file utility.

find $PWD -type f -exec file -N \{\} \; | grep "PDF document" | awk -F: '{print $1}'

Instead of $PWD you can use the directory you want to start the search in. file prints even out he PDF version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文