文件和大小的命令

文件和大小的命令

是否有命令可以列出目录中的文件类型及其大小?

例如,.jpg 1GB、.png 2GB、.avi 3GB 等,谢谢

答案1

您可以file根据文件内容而不是文件扩展名来确定实际文件类型(MIME 类型),并且可以使用纯 Bash 来聚合每种类型的大小总和。

看一下这个例子:

$ find Pictures/ -printf '%s\t' -exec file --brief --mime-type {} \;|{ declare -A A;while IFS=$'\t' read -r B T;do A["$T"]=$((A["$T"]+B));done;for T in "${!A[@]}";do printf '%12d\t%s\n' "${A["$T"]}" "$T";done;}|sort -bnr
    72046936    image/jpeg
    57324445    image/png
    23712181    application/x-7z-compressed
    17144737    image/gif
     6563757    image/x-xcf
      697098    image/svg+xml
       53248    inode/directory

为了验证结果,上述所有值的总和恰好等于du报告的值:

$ du -sb Pictures/
177542402   Pictures/

这是上面使用的命令行,以脚本的形式进行了注释和格式化,使其更加易读:

#!/bin/bash

# Recursively find all files (and directories) in `Pictures/`,
# then output their size on disk in bytes, followed by a tab and the output of `file`,
# showing only the short MIME type without path and extra info (e.g. "image/png"):
find Pictures/ -printf '%s\t' -exec file --brief --mime-type {} \; | {

    # declare the `ARR` variable to be an associative array (mapping type strings to total size)
    declare -A ARR

    # parse the above output line by line, reading the tab-separated columns into 
    # the variables `BYTES` and `TYPE` respectively
    while IFS=$'\t' read -r BYTES TYPE ; do
        # add the current `BYTES` number to the corresponding entry in our `ARR` array
        ARR["$TYPE"]=$(( ARR["$TYPE"] + BYTES ))
    done

    # loop over all keys (MIME types) in our `ARR` array
    for TYPE in "${!ARR[@]}" ; do
        # output the total bytes (right-aligned up to 12 digits) followed by a tab and the type
        printf '%12d\t%s\n' "${ARR["$TYPE"]}" "$TYPE"
    done

# sort the resulting output table numerically, in descending order and ignoring leading space
} | sort -bnr

答案2

一种方法是:

find . -name '?*.*' -type f -printf '%b.%f\0' |
  awk -F . -v RS='\0' '
    {s[$NF] += $1; n[$NF]++}
    END {for (e in s) printf "%15d %4d %s\n", s[e]*512, n[e], e}' |
  sort -n

我的桌面的结果:

  873172992    1 mkv

相关内容