How to List the Size of Each File and Directory and Sort by Descending Size in Bash

How to list the size of each file and directory and sort by descending size in Bash?

Simply navigate to directory and run following command:

du -a --max-depth=1 | sort -n

OR add -h for human readable sizes and -r to print bigger directories/files first.

du -a -h --max-depth=1 | sort -hr

List all directories sorted by size in descending order

I prefer to just go straight to comparing bytes.

du -b * | sort -nr

du -b reports bytes.

sort -n sorts numerically. Obviously, -r reverses.

My /tmp before I clean it -

104857600       wbxtra_RESIDENT_07202018_075931.wbt
815372 wbxtra_RESIDENT_07192018_075744.wbt
215310 Slack Crashes
148028 wbxtra_RESIDENT_07182018_162525.wbt
144496 wbxtra_RESIDENT_07182018_163507.wbt
141688 wbxtra_RESIDENT_07182018_161957.wbt
56617 Notification Cache
20480 ~DFFA6E4895E749B423.TMP
16384 ~DF543949D7B4DF074A.TMP
13254 AdobeARM.log
3614 PhishMeOutlookReporterLoader.log
3448 msohtmlclip1/01
3448 msohtmlclip1
512 ~DF92FFF2C02995D884.TMP
28 ExchangePerflog_8484fa311d504d0fdcd6c672.dat
0 WPDNSE
0 VPMECTMP
0 VBE

Using ls to list directories and their total sizes

Try something like:

du -sh *

short version of:

du --summarize --human-readable *

Explanation:

du: Disk Usage

-s: Display a summary for each specified file. (Equivalent to -d 0)

-h: "Human-readable" output. Use unit suffixes: Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB) and Pebibyte (PiB). (BASE2)

unix command - size of directory with order by size

You can try du -h --max-depth=1 | sort -hr

How to list files in a directory, sorted by size, but without listing folder sizes?

Would you please try the following:

dir="../../src/"
sudo find "$dir" -type f -printf "%s\t%p\n" | sort -nr | head -n 10 | cut -f2-
  • find "$dir" -type f searches $dir for files recursively.
  • The -printf "%s\t%p\n" option tells find to print the filesize
    and the filename delimited by a tab character.
  • The final cut -f2- in the pipeline prints the 2nd and the following
    columns, dropping the filesize column only.

It will work with the filenames which contain special characters such as a whitespace except for
a newline character.

List file names in order of file size in BASH

ls -rS will do the trick. The man page explains more: http://man7.org/linux/man-pages/man1/ls.1.html



Related Topics



Leave a reply



Submit