есть ли команда, чтобы перечислить, какой тип файлов находится в каталоге и размере?
, например, .jpg 1 ГБ, .png 2 ГБ, .avi 3 ГБ и т. Д., Спасибо
Вы можете использовать file
для определения фактического типа файла (MIME-типа) на основе его содержимого вместо расширения файла, и вы можете использовать чистый Bash для агрегирования суммы размера по типу.
Посмотрите на этот пример:
$ find Pictures/ -printf '%s\t' -exec file --brief --mime-type {} \;|{ declare -A A;while IFS= И чтобы проверить результаты, сумма всех значений выше точно равна тому, что 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
\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
Метод будет следующим:
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