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 tellsfind
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
Merge PDF Files with Numerical Sort
How to Close a Netcat Connection After a Certain Character Is Returned in the Response
Identify Other End of a Unix Domain Socket Connection
Dummy Questions About Setting Up Git on Amazon Cloud Ec2
Stripping Single and Double Quotes in a String Using Bash/Standard Linux Commands Only
How to Perform Grep Operation on All Files in a Directory
Replace Whitespaces with Tabs in Linux
Glibc: Elf File Os Abi Invalid
Whiptail: How to Redirect Output to Environment Variable
Why Is It That Utf-8 Encoding Is Used When Interacting with a Unix/Linux Environment
Getting List of Network Devices Inside the Linux Kernel
How to Install Packages from Command Line on Suse
Weird Sigsegv Segmentation Fault in Std::String::Assign() Method from Libstdc++.So.6
Gedit Syntax Highlighting for Assembler Files (*.Asm)
Is It Possible That Linux File Descriptor 0 1 2 Not for Stdin, Stdout and Stderr
Recursively List All Files in a Directory Including Files in Symlink Directories
What Are Stalled-Cycles-Frontend and Stalled-Cycles-Backend in 'Perf Stat' Result