linux 递归获取Mac上的文件扩展名列表

qmelpv7a  于 2023-03-17  发布在  Linux
关注(0)|答案(2)|浏览(90)

我想要Mac上的文件扩展名列表。我想递归搜索所有文件。
预期产出:

tar.gz
  txt
  pdf
  png
  jpg

我尝试了以下方法:

find . | grep '\.'| tail -c 4  
  ls -lR | grep '\.'| tail -c 4

输出只是“txt”,但有更多的扩展名,如pdf,png,jpg等。我如何才能得到其余的预期输出?
输入文件示例

$ tree
.
├── foobar.tar.gz
├── 02_quest_cbc.pdf
├── 16_iron_panel.pdf
├── 16_lots.pdf
├── 18_cbc.pdf
├── 20_kaiser.txt
├── com
│   ├── 1.png
│   ├── 2.png
│   ├── project.txt
│   └── readme.txt
└── txt

1 directory, 11 files
8i9zcol2

8i9zcol21#

像这样,假设文件的文件名中除了扩展名之外没有点:

$ tree
.
├── foobar.tar.gz
├── 02_quest_cbc.pdf
├── 16_iron_panel.pdf
├── 16_lots.pdf
├── 18_cbc.pdf
├── 20_kaiser.txt
├── com
│   ├── 1.png
│   ├── 2.png
│   ├── project.txt
│   └── readme.txt
└── txt

1 directory, 11 files
find . -type f | awk -F. '
    {
        if      (NF==2) {sub(/\.\//, ""); arr[$0]}
        else if (NF==3) {arr[$NF]}
        else if (NF>3)  {arr[$(NF-1)"."$NF]}
    }
    END{for (i in arr) print i}'
输出
tar.gz
pdf
png
sh
txt
一种更可靠的方法是使用homebrew安装GNU file,然后:
$ find . -exec file -i {} \; | perl -nE 'say $1 if m|\s+\w+/(\w+);|' | sort -u
hm2xizp9

hm2xizp92#

查找-类型f|awk -F“.”“{打印$NF}”|分类|独特的

相关问题