UNIX Shell-在一个文件夹下找到所有不同类型的文件扩展名

发布于 2025-01-23 14:14:08 字数 419 浏览 2 评论 0原文

有关外壳命令的小问题。

我有一个顶级文件夹,其中包含不同类型的扩展名的文件。 例如,该文件夹包含文件.java,.xml等...

在此顶级文件夹下,它还包含基础文件夹,儿童文件夹,它们本身包含相同和/或其他类型的文件,以及其他可能的其他儿童文件夹。

我正在尝试将所有类型的文件列在顶级文件夹下,一直向下。

我尝试使用命令查找,但它要求我提前知道所有类型,我不知道。

请问,请问获取所有类型的文件的正确命令是什么?

预期的结果输出就像:

  • .java
  • .xml
  • .png

(如果可能的话,使用计数,但可以完全可以)

  • .java 86
  • .xml 23
  • .png 42

Small question regarding a Shell command please.

I have a top level folder, which contains files of different types of extensions.
For instance, the folder contains files .java, .xml, etc...

Under this top level folder, it contains also underlying folders, child folders, which themselves contains same and/or other types of files, and possibly other child folders.

I am trying just to list all the types of files under the top level folder, all the way down.

I tried using the command find, but it requires me to know in advance all the types, which I do not know.

May I ask, what is the correct command to get all types of files please?

An expected result output would be like:

  • .java
  • .xml
  • .png

(if possible, with the count, but totally ok if not)

  • .java 86
  • .xml 23
  • .png 42

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

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

发布评论

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

评论(1

∞梦里开花 2025-01-30 14:14:08

,也可以写一个脚本,例如,要做所有事情。

#!/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} += 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

或任何外壳(与Newlines的文件名无法正常运行;但是,如果使用GNU Userland工具,则有一些方法):

find . -name "*.*" | sed 's/.*\.\([^.]\{1,\}\)$/\1/' | sort | uniq -c

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} += 1;
    }
     }, @ARGV);
for my $ext (sort { $a cmp $b } keys %extensions) {
    say "$ext\t$extensions{$ext}";
}

or using 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

or any shell (Won't work well with filenames with newlines; there are ways around that if using, say, GNU userland tools, though):

find . -name "*.*" | sed 's/.*\.\([^.]\{1,\}\)$/\1/' | sort | uniq -c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文