按文件数递归列出文件夹

Lie*_*yan 5 linux file-management

是否有任何 Linux 应用程序可以查找文件数量最多的文件夹?

baobab按文件夹的总大小对文件夹进行排序,我正在寻找一种工具,可以按文件夹中的文件总数列出文件夹。

我正在寻找的原因是因为复制数以万计的小文件非常慢(比复制几个相同大小的大文件慢得多),所以我想存档或删除那些文件数高的文件夹,这将是减慢复制速度(它现在不会加快速度,但是当我将来需要再次移动/复制它时会更快)。

har*_*ymc 6

Shell:列出按文件计数排序的目录(有关解释,请参阅文章):

单行(用于主目录):

find ~ -type d -exec sh -c "fc=\$(find '{}' -type f | wc -l); echo -e \"\$fc\t{}\"" \; | sort -nr
Run Code Online (Sandbox Code Playgroud)

一个脚本:

countFiles () {
    # call the recursive function, throw away stdout and send stderr to stdout
    # then sort numerically
    countFiles_rec "$1" 2>&1 >/dev/null | sort -nr
}

countFiles_rec () {
    local -i nfiles 
    dir="$1"

    # count the number of files in this directory only
    nfiles=$(find "$dir" -mindepth 1 -maxdepth 1 -type f -print | wc -l)

    # loop over the subdirectories of this directory
    while IFS= read -r subdir; do

        # invoke the recursive function for each one 
        # save the output in the positional parameters
        set -- $(countFiles_rec "$subdir")

        # accumulate the number of files found under the subdirectory
        (( nfiles += $1 ))

    done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d -print)

    # print the number of files here, to both stdout and stderr
    printf "%d %s\n" $nfiles "$dir" | tee /dev/stderr
}


countFiles Home
Run Code Online (Sandbox Code Playgroud)


bfh*_*fhd 5

我确信有一种方法可以用脚本来做到这一点,所以我去想通了。

如果您制作这样的 bash 脚本(假设我们将其称为“countfiles”):

#!/bin/bash
find . -type d | while read DIR; do
ls -A $DIR | echo $DIR $(wc -w);done
Run Code Online (Sandbox Code Playgroud)

然后运行它并像这样输出管道:

./countfiles | sort -n -k 2,2 > output
Run Code Online (Sandbox Code Playgroud)

然后您的输出文件将列出所有子目录,并在其后列出文件数(最后的文件数最多)。

例如。当我执行“尾部输出”时,在我的 /usr 文件夹上如上所述运行此脚本会显示这一点

./lib/gconv 249
./share/doc 273
./share/i18n/locales 289
./share/mime/application 325
./share/man/man8 328
./share/perl/5.10.1/unicore/lib/gc_sc 393
./lib/python2.6 424
./share/vim/vim72/syntax 529
./bin 533
./share/man/man1 711
Run Code Online (Sandbox Code Playgroud)

可能有更好的方法来做到这一点;我不太擅长 bash 脚本 :(


小智 2

尝试一下JDiskReport,它可能对您有用。 如果您运行 KDE, FileLight是另一种。

JDiskReport截图

SS#1