Unix shell - 在一个文件夹下查找所有不同类型的文件扩展名
Unix shell - find all different types of file extensions under one folder
请问一个关于 Shell 命令的小问题。
我有一个顶级文件夹,其中包含不同类型扩展名的文件。
例如,该文件夹包含文件 .java、.xml 等...
在此顶级文件夹下,它还包含底层文件夹、子文件夹,它们本身包含相同的 and/or 其他类型的文件,可能还有其他子文件夹。
我只是想列出顶级文件夹下的所有文件类型,一直向下。
我试过使用命令find,但它需要我提前知道所有类型,我不知道。
请问,获取所有类型文件的正确命令是什么?
预期的输出结果如下:
- .java
- .xml
- .png
(如果可能的话,有计数,但如果没有也完全没问题)
- .java 86
- .xml 23
- .png 42
Or you could write a script in, say, Perl to do it all.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use File::Find;
# Takes directory/directories to scan as a command line argument
# or current directory if none given
@ARGV = qw/./ unless @ARGV;
my %extensions;
find(sub {
if ($File::Find::name =~ /\.([^.]+)$/) {
$extensions{} += 1;
}
}, @ARGV);
for my $ext (sort { $a cmp $b } keys %extensions) {
say "$ext\t$extensions{$ext}";
}
或使用bash:
#!/usr/bin/env bash
shopt -s dotglob globstar
declare -A extensions
# Scans the current directory
allfiles=( **/*.* )
for ext in "${allfiles[@]##*.}"; do
extensions["$ext"]=$(( ${extensions["$ext"]:-0} + 1))
done
for ext in "${!extensions[@]}"; do
printf "%s\t%d\n" "$ext" "${extensions[$ext]}"
done | sort -k1,1
或任何 shell(不能很好地处理带有换行符的文件名;不过,如果使用 GNU userland 工具,则有解决方法):
find . -name "*.*" | sed 's/.*\.\([^.]\{1,\}\)$//' | sort | uniq -c
请问一个关于 Shell 命令的小问题。
我有一个顶级文件夹,其中包含不同类型扩展名的文件。 例如,该文件夹包含文件 .java、.xml 等...
在此顶级文件夹下,它还包含底层文件夹、子文件夹,它们本身包含相同的 and/or 其他类型的文件,可能还有其他子文件夹。
我只是想列出顶级文件夹下的所有文件类型,一直向下。
我试过使用命令find,但它需要我提前知道所有类型,我不知道。
请问,获取所有类型文件的正确命令是什么?
预期的输出结果如下:
- .java
- .xml
- .png
(如果可能的话,有计数,但如果没有也完全没问题)
- .java 86
- .xml 23
- .png 42
Or you could write a script in, say, Perl to do it all.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use File::Find;
# Takes directory/directories to scan as a command line argument
# or current directory if none given
@ARGV = qw/./ unless @ARGV;
my %extensions;
find(sub {
if ($File::Find::name =~ /\.([^.]+)$/) {
$extensions{} += 1;
}
}, @ARGV);
for my $ext (sort { $a cmp $b } keys %extensions) {
say "$ext\t$extensions{$ext}";
}
或使用bash:
#!/usr/bin/env bash
shopt -s dotglob globstar
declare -A extensions
# Scans the current directory
allfiles=( **/*.* )
for ext in "${allfiles[@]##*.}"; do
extensions["$ext"]=$(( ${extensions["$ext"]:-0} + 1))
done
for ext in "${!extensions[@]}"; do
printf "%s\t%d\n" "$ext" "${extensions[$ext]}"
done | sort -k1,1
或任何 shell(不能很好地处理带有换行符的文件名;不过,如果使用 GNU userland 工具,则有解决方法):
find . -name "*.*" | sed 's/.*\.\([^.]\{1,\}\)$//' | sort | uniq -c