Disk Usage (du) Command Examples
A comprehensive guide to using the du
command for analyzing disk usage in Linux, featuring advanced and practical examples.
List all directory sizes and sort by size
du -ks * | sort -nr | cut -f2 | xargs -d '\n' du -sh
Print total size of specified files and subdirectories
du -sk * | awk '{print $1} END {print "[+z1<y]sy\nlyx\np"}' | dc
Display a numbered list of 50 biggets files sorted by size (human readable)
du -ah | sort -hr | head -n50 | cat -n
Ultimate current directory usage command
du -a max-depth=1 \
|sort -n \
|cut -d/ -f2 \
|sed '$d' \
|while read i; do
if [[ -f $i ]]; then
du -h "$i";
else
echo "$(du -h max-depth=0 "$i")/";
fi;
done
Easily find megabyte eating files or directories
du -cks * \
|sort -rn \
|while read size fname; do
for unit in k M G T P E Z Y; do
if [[ $size -lt 1024 ]]; then
echo -e "${size}${unit}\t${fname}";
break;
fi; size=$((size/1024));
done;
done
List sub dir, sort by size
du --max-depth=1 |sort -n \
|awk 'function human(x) { s="KMGTEPYZ"; while (x>=1000 && length(s)>1)
{x/=1024; s=substr(s,2)} return int(x+0.5) substr(s,1,1)"iB" }
{gsub(/^[0-9]+/, human($1)); print}'
One line Perl Script to determine the largest file sizes on a Linux Server
du -k | sort -n | perl -ne 'if ( /^(\d+)\s+(.*$)/){$l=log($1+.1);$m=int($l/log(1024)); printf ("%6.1f\t%s\t%25s %s\n",($1/(2**(10*$m))),(("K","M","G","T","P")[$m]),"*"x (1.5*$l),$2);}' | more
Print total size of specified files and subdirectories
du -sk * | awk '{print $1} END {print "[+z1<y]sy\nlyx\np"}' | dc
Display a numbered list of 50 biggets files sorted by size (human readable)
du -ah | sort -hr | head -n50 | cat -n
### Easily find megabyte eating files or directories
```bash
du -cks * | sort -rn | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done
List complete size of directories (do not consider hidden directories)
du --max-depth=1 | grep -v '\.\/\.'
Sort the size usage of a directory tree by gigabytes, kilobytes, megabytes, then bytes.
du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e'
Find the 20 biggest directories on the current filesystem
du -xk | sort -n | tail -20
List top ten files/directories sorted by size
du -sb *|sort -nr|head|awk '{print $2}'|xargs du -sh
Find all directories on filesystem containing more than 99MB
du -hS / | perl -ne '(m/\d{3,}M\s+\S/ || m/G\s+\S/) && print'
List the size (in human readable form) of all sub folders from the current location
du --max-depth=1|sort -n|cut -f2|tr '\n' '\0'|xargs -0 du -sh 2>/dev/null
Ultimate current directory usage command
du -a --max-depth=1 | sort -n | cut -d/ -f2 | sed '$d' | while read i; do
if [ -f $i ]; then du -h "$i"; else echo "$(du -h --max-depth=0 "$i")/";
fi;
done
List the size (in human readable form) of all sub folder
du -sk * | sort -n | perl -pe '@SI=qw(K M G T P); s:^(\d+?)((\d\d\d)*)\s:$1." ".$SI[((length $2)/3)]."\t":e'
Show biggest files/directories
du --max-depth=1 | sort -r -n | awk '{split("k m g",v); s=1; while($1>1024){$1/=1024; s++} print int($1)" "v[s]"\t"$2}'
Display the human-readable with 3 decimal places
du -Lsbc * |awk 'function hr(bytes){hum[1024**4]="TiB";hum[1024**3]="GiB";
hum[1024**2]="MiB";hum[1024]="kiB";
for(x=1024**4;x>=1024;x/=1024){if(bytes>=x){return sprintf("%8.3f %s",bytes/x,hum[x]);}}return sprintf("%4d B",bytes);}{print hr($1) "\t" $2}'
Show total size of each subdirectory
du --max-depth=1 | sort -nr | awk ' BEGIN { split("KB,MB,GB,TB", Units, ","); } { u = 1; while ($1 >= 1024) { $1 = $1 / 1024; u += 1 } $1 = sprintf("%.1f %s", $1, Units[u]); print $0; } '
Colored bar graph
du -x --max-depth=1|sort -rn|awk -F / -v c=$COLUMNS 'NR==1{t=$1} NR>1{r=int($1/t*c+.5);
b="\033[1;31m"; for (i=0; i<r; i++) b=b"#"; printf " %5.2f%% %s\033[0m %s\n", $1/t*100, b, $2}'|tac
List sub dir, sort by size, the biggest at the end, with human presentation
du --max-depth=3 -x -k | sort -n | awk 'function human(x) { s="KMGTEPYZ"; while (x>=1000 && length(s)>1) {x/=1024; s=substr(s,2)} return int(x+0.5) substr(s,1,1)"iB" } {gsub(/^[0-9]+/, human($1)); print}'
Which files/dirs waste my disk space
du -xm --max-depth 2 /var/log | sort -rn | head
Easily find megabyte eating files or directories
du -cks * | sort -rn | while read size fname; do for unit in k M G T P E Z Y; do
if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break;
fi; size=$((size/1024)); done; done