sortingarchivetarbz2

List all files in a .tar.bz2, sorted by size


I use this command to list all files in an archive:

tar jtvf blah.tar.bz2

How to list them sorted by size? Or list only the biggest files (i.e. files bigger than, say, 10MB)?


Solution

  • list files, filter by size, print only size+space+path, and sort by size only, descending order:

    On every machine I tested (back in 2016 when this answer was written: don't know... similar things but older, plus FreeBSD 8 probably; today: Artixlinux, Ubuntu 20.04, Ubuntu 22.04, CentOS 7, Devuan 3, Devuan 4), the output has 5 columns total, and 3rd is size, 5th is filename):

    size=10485760
    tar tvf blah.tar.bz2 \
        | awk -v size="$size" '$3 >= size {print $3" "$5}' \
        | sort -t' ' -k1,1nr
    

    If you have whatever undefined OS one editor of this answer has, maybe you have the data in fields 5 and 9 instead, and then use:

    size=10485760
    tar tvf blah.tar.bz2 \
        | awk -v size="$size" '$5 >= size {print $5" "$9}' \
        | sort -t' ' -k1,1nr