简体   繁体   English

如何从目录中递归查找所有文件扩展名?

[英]How to find all file extensions recursively from a directory?

What command, or collection of commands, can I use to return all file extensions in a directory (including sub-directories)?我可以使用什么命令或命令集合来返回目录(包括子目录)中的所有文件扩展名?

Right now, I'm using different combinations of ls and grep , but I can't find any scalable solution.现在,我正在使用lsgrep不同组合,但我找不到任何可扩展的解决方案。

这个怎么样:

find . -type f -name '*.*' | sed 's|.*\.||' | sort -u
find . -type f | sed 's|.*\.||' | sort -u

也适用于 mac。

列出所有扩展名及其当前和所有子目录的计数

ls -1R | sed 's/[^\.]*//' | sed 's/.*\.//' | sort | uniq -c

if you are using Bash 4+如果您使用的是 Bash 4+

shopt -s globstar
for file in **/*.*
do
  echo "${file##*.}
done

Ruby(1.9+)红宝石(1.9+)

ruby -e 'Dir["**/*.*"].each{|x|puts x.split(".")[-1]}' | sort -u

嘘另一个:

find * | awk -F . {'print $2'} | sort -u
ls -1 | sed 's/.*\.//' | sort -u

Update: You are correct Matthew.更新:你是对的,马修。 Based on your comment, here is an updated version:根据您的评论,这是一个更新版本:

ls -R1 | egrep -C 0 "[^\\.]+\\.[^\\./:]+$" | sed 's/.*\\.//' | sort -u

I was just quickly trying this as I was searching Google for a good answer.我只是在快速尝试这个,因为我在谷歌搜索一个好的答案。 I am more Regex inclined than Bash, but this also works for subdirectories.我比 Bash 更倾向于 Regex,但这也适用于子目录。 I don't think includes files without extensions either:我也不认为包含没有扩展名的文件:

ls -R | egrep '(\\.\\w+)$' -o | sort | uniq -c | sort -r

Yet another solution using find (that should even sort file extensions with embedded newlines correctly):另一个使用 find 的解决方案(甚至应该正确地对带有嵌入换行符的文件扩展名进行排序):

# [^.]: exclude dotfiles
find . -type f -name "[^.]*.*" -exec bash -c '
  printf "%s\000" "${@##*.}"
' argv0 '{}' + |
sort -uz | 
tr '\0' '\n'

Another one, similar to others but only uses two programs (find and awk)另一个,与其他类似,但只使用两个程序(find 和 awk)

find ./ -type f -name "*\\.*" -printf "%f\\n" | awk -F . '!seen[$NF]++ {print $NF}'

-type f restricts it to just files, not directories -type f将其限制为仅文件,而不是目录

-name "*\\.*" ensures the filename has a . -name "*\\.*"确保文件名有一个. in it.在其中。

-printf "%f\\n" prints just the filename, not the path to the filename. -printf "%f\\n"只打印文件名,而不是文件名的路径。

-F . makes awk utilize a period as the field separator.使 awk 使用句点作为字段分隔符。

$NF is the last field, separated by periods. $NF是最后一个字段,由句点分隔。

!seen[$NF]++ evaluates to true the first time an extension is encountered, and false every other time it is encountered. !seen[$NF]++在第一次遇到扩展时评估为真,每隔一次遇到一次就为假。

print $NF prints the extension. print $NF打印扩展名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何递归查找目录中最新修改的文​​件? - How to recursively find the latest modified file in a directory? 如何按文件类型递归查找文件并将它们复制到目录? - How to find files recursively by file type and copy them to a directory? 如何找到目录中最新修改文件的时间戳(递归)? - How to find the timestamp of the latest modified file in a directory (recursively)? 如何在目录中递归查找文件修改的最新日期和时间? - How to recursively find the latest date and time of file modification in directory? 如何列出目录树中的所有二进制文件扩展名? - How to list all binary file extensions within a directory tree? 如何递归遍历目录以删除具有某些扩展名的文件 - How to loop through a directory recursively to delete files with certain extensions 如何在bash中的所有子文件夹中递归并发查找文件 - How to find a file recursively and concurrently in all subfolders in bash 显示和计算目录(带有子目录)中的所有文件扩展名 - Show and count all file extensions in directory (with subdirectories) 如何在不使用Bash脚本的情况下递归地获取包含工作文件夹的文件夹中多个扩展名过滤的所有文件 - How to recursively get all files filtered by multiple extensions within a folder including working folder without using find in Bash script 如何递归删除目录中所有文件中的插入符号(^)字符? - How to recursively remove the caret (^) character recursively in all files in a directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM