如何在文件夹层次结构中找到所有不同的文件扩展名?
在 Linux 机器上,我想遍历文件夹层次结构并获取其中所有不同文件扩展名的列表。
从 shell 实现此目的的最佳方法是什么?
On a Linux machine I would like to traverse a folder hierarchy and get a list of all of the distinct file extensions within it.
What would be the best way to achieve this from a shell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
试试这个(不确定这是否是最好的方法,但它有效):
它的工作原理如下:
Try this (not sure if it's the best way, but it works):
It work as following:
不需要管道来
排序
,awk 可以做到这一切:No need for the pipe to
sort
, awk can do it all:我的无 awk、无 sed、无 Perl、无 Python 的 POSIX 兼容替代方案:
技巧是它反转行并在开头剪切扩展名。
它还将扩展名转换为小写。
输出示例:
My awk-less, sed-less, Perl-less, Python-less POSIX-compliant alternative:
The trick is that it reverses the line and cuts the extension at the beginning.
It also converts the extensions to lower case.
Example output:
递归版本:
如果您想要总数(看到扩展名的次数):
非递归(单个文件夹):
我基于 此论坛帖子,信用应该去那里。
Recursive version:
If you want totals (how may times the extension was seen):
Non-recursive (single folder):
I've based this upon this forum post, credit should go there.
Powershell:
感谢 http://kevin-berridge.blogspot.com/2007 /11/windows-powershell.html
Powershell:
Thanks to http://kevin-berridge.blogspot.com/2007/11/windows-powershell.html
添加我自己的变体。我认为这是最简单的,当效率不是一个大问题时,它会很有用。
Adding my own variation to the mix. I think it's the simplest of the lot and can be useful when efficiency is not a big concern.
查找所有带点的内容并仅显示后缀。
如果您知道所有后缀都有 3 个字符,那么
or 与 sed 显示所有后缀有 1 到 4 个字符。将 {1,4} 更改为您期望后缀中的字符范围。
Find everythin with a dot and show only the suffix.
if you know all suffix have 3 characters then
or with sed shows all suffixes with one to four characters. Change {1,4} to the range of characters you are expecting in the suffix.
我在这里尝试了很多答案,甚至是“最佳”答案。他们都没有达到我具体追求的目标。因此,除了过去 12 小时坐在多个程序的正则表达式代码以及阅读和测试这些答案之外,这就是我想出的,它完全按照我想要的方式工作。
如果您需要计算文件扩展名,请使用下面的代码。
虽然这些方法需要一些时间才能完成,并且可能不是解决问题的最佳方法,但它们是有效的。
更新:
根据 @alpha_989 长文件扩展名会导致问题。这是由于原始正则表达式“[[:alpha:]]{3,6}”造成的。我已更新答案以包含正则表达式“[[:alpha:]]{2,16}”。但是,任何使用此代码的人都应该知道,这些数字是最终输出允许的扩展时间的最小值和最大值。该范围之外的任何内容都将在输出中分成多行。
注意:原帖确实读过“- Greps for file extensions between 3 and 6 个字符(如果不符合您的需要,只需调整数字)。这有助于避免缓存文件和系统文件(系统文件位是搜索监狱)。 ”
想法:可用于通过以下方式查找特定长度的文件扩展名:
其中 4 是要包含的文件扩展名长度,然后还查找超出该长度的任何扩展名。
I tried a bunch of the answers here, even the "best" answer. They all came up short of what I specifically was after. So besides the past 12 hours of sitting in regex code for multiple programs and reading and testing these answers this is what I came up with which works EXACTLY like I want.
If you need a count of the file extensions then use the below code
While these methods will take some time to complete and probably aren't the best ways to go about the problem, they work.
Update:
Per @alpha_989 long file extensions will cause an issue. That's due to the original regex "[[:alpha:]]{3,6}". I have updated the answer to include the regex "[[:alpha:]]{2,16}". However anyone using this code should be aware that those numbers are the min and max of how long the extension is allowed for the final output. Anything outside that range will be split into multiple lines in the output.
Note: Original post did read "- Greps for file extensions between 3 and 6 characters (just adjust the numbers if they don't fit your need). This helps avoid cache files and system files (system file bit is to search jail)."
Idea: Could be used to find file extensions over a specific length via:
Where 4 is the file extensions length to include and then find also any extensions beyond that length.
在 Python 中,使用生成器生成非常大的目录,包括空白扩展名,并获取每个扩展名出现的次数:
In Python using generators for very large directories, including blank extensions, and getting the number of times each extension shows up:
由于已经有另一个使用 Perl 的解决方案:
如果您安装了 Python,您也可以执行以下操作(从 shell):
Since there's already another solution which uses Perl:
If you have Python installed you could also do (from the shell):
另一种方法:
find . -type f -name "*.*" -printf "%f\n" | -type f -name "*.*" -printf "%f\n" |而 IFS= 读取 -r;执行 echo "${REPLY##*.}";完成 | sort -u
您可以删除
-name "*.*"
,但这可以确保我们只处理确实具有某种扩展名的文件。-printf
是find
的打印内容,而不是 bash。-printf "%f\n"
仅打印文件名,删除路径(并添加换行符)。然后,我们使用字符串替换来使用
${REPLY##*.}
删除最后一个点。请注意,
$REPLY
只是read
的内置变量。我们可以使用我们自己的形式:while IFS= read -r file
,这里 $file 将是变量。Another way:
find . -type f -name "*.*" -printf "%f\n" | while IFS= read -r; do echo "${REPLY##*.}"; done | sort -u
You can drop the
-name "*.*"
but this ensures we are dealing only with files that do have an extension of some sort.The
-printf
isfind
's print, not bash.-printf "%f\n"
prints only the filename, stripping the path (and adds a newline).Then we use string substitution to remove up to the last dot using
${REPLY##*.}
.Note that
$REPLY
is simplyread
's inbuilt variable. We could just as use our own in the form:while IFS= read -r file
, and here $file would be the variable.到目前为止,没有一个回复能够正确处理带换行符的文件名(ChristopheD 的除外,它是在我输入此内容时才出现的)。下面的代码不是 shell 的单行代码,但是可以工作,而且速度相当快。
None of the replies so far deal with filenames with newlines properly (except for ChristopheD's, which just came in as I was typing this). The following is not a shell one-liner, but works, and is reasonably fast.
我认为最简单的&直接的方法是
在ChristopheD的第三种方法的基础上进行修改。
I think the most simple & straightforward way is
It's modified on ChristopheD's 3rd way.
我认为还没有提到这一点:
I don't think this one was mentioned yet:
接受的答案使用 REGEX,您无法使用 REGEX 创建别名命令,您必须将其放入 shell 脚本中,我使用 Amazon Linux 2 并执行以下操作:
我使用以下命令将接受的答案代码放入文件中:
sudo vim find.sh
添加此代码:
通过键入以下内容保存文件:
:wq!
sudo vim ~/.bash_profile
alias getext="./path/to/your/find .sh"
:wq!
<代码>。 ~/.bash_profile
The accepted answer uses REGEX and you cannot create an alias command with REGEX, you have to put it into a shell script, I'm using Amazon Linux 2 and did the following:
I put the accepted answer code into a file using :
sudo vim find.sh
add this code:
save the file by typing:
:wq!
sudo vim ~/.bash_profile
alias getext=". /path/to/your/find.sh"
:wq!
. ~/.bash_profile
你也可以这样做
you could also do this
我发现它既简单又快速...
I've found it simple and fast...
如果您正在寻找尊重
.gitignore
的答案,请检查下面的答案。If you are looking for answer that respect
.gitignore
then check below answer.Ondra Žižka 版本的另一个版本:
在区分大小写的文件系统上,恕我直言,不同的大小写不应被视为相同的扩展名。另外,我认为没有必要对文件进行计数来回答 OP 问题。
Another version of Ondra Žižka's one:
On case sensitive file systems different cases should imho not be treated as the same extension. Also I don't think counting files is necessary as an answer to OPs question.