Befehl von Dateien und Größe


7

Gibt es einen Befehl zum Auflisten des Dateityps in einem Verzeichnis und der Größe?

Zum Beispiel .jpg 1 GB, .png 2 GB, .avi 3 GB usw., danke

Antworten:


7

Sie können fileden tatsächlichen Dateityp (MIME-Typ) anhand seines Inhalts anstelle der Dateierweiterung ermitteln und mit Pure Bash die Größensumme pro Typ aggregieren.

Schauen Sie sich dieses Beispiel an:

$ 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

Um die Ergebnisse zu überprüfen, entspricht die Summe aller oben genannten Werte genau den duBerichten:

$ du -sb Pictures/
177542402   Pictures/

Hier ist die verwendete Befehlszeile von oben, die als Skript besser lesbar kommentiert und formatiert ist:

#!/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
Sie können dies erheblich vereinfachen, indem Sie awkanstelle der Shell nach der Pipe Folgendes verwenden : find ... | awk '{a[$2]+=$1} END{for(i in a){print a[i],i}}'.
Terdon

5

Eine Methode wäre:

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

Ergebnis von meinem Desktop:

  873172992    1 mkv

Vielen Dank, ich werde versuchen
Stathis
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.