Which command will print the sizes of all files and directories in the tmp directory (including hidden ones) and sort them by sizes from
largest to smallest in human readable format (e.g. 2 GB)?
The output could be as follows:
file.mpg 2GB file.avi 1.8GB .backtup 1.7 GB (this is directory)
I tried to use the ls and du commands but was not able to find the right switches.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Here’s a quick fix, use du + sort. Try this:
du -smc * | sort -n
This will ignore hidden files, but that’s another easy fix:
du -smc .[^.] .??* * | sort -n
This may cause warnings about if one or more of the above patterns don’t match a file. The first pattern .[^.] matches all two character filenames starting with . except for .., the second pattern, .??* matches all three letter or more filenames starting with . and * matches all files not starting with . For a more sophisticated listing such as finding all file larger than X across a whole filesystem, or maintaining a list of filesystem growth, I have some DIY shell script I’ve written and can share if your interested.
Method 2
To list the files anywhere under /tmp, sorted by size:
find /tmp -type f -exec du -k {} + | sort -k1n -k2
To list the files and directory trees immediately under /tmp, sorted by size:
du -k /tmp/..?* /tmp/.[!.]* /tmp* 2>/dev/null | sort -k1n -k2
To list all files and directory trees anywhere under /tmp, sorted by size:
du -ak /tmp | sort -k1n -k2
(An example to illustrate the difference between the three commands: if there’s a file /tmp/dir/file, the first command lists /tmp/dir/file, the second lists /tmp/dir, and the third lists both.)
All the commands above show sizes in kilobytes. While GNU du can output “human-readable” sizes (with k, M, G, etc. multipliers), sorting them is another matter. Recent enough GNU coreutils (≥7.4) can do it: just replace du -k with du -h and sort -k1n -k2 with sort -k1h -k2. Otherwise, here’s a crude awk script to convert to suffixed sizes (rounding down); just pipe the sort output above into it.
awk -vFS='t' -vOFS='t' '{
if ($1) $1 = substr($1,1,(length($1)-1)%3+1)
substr("kMGTPEZY",(length($1)-1)/3+1,1);
print}'
Method 3
I’m using following alias for it: alias ds='du -x --all --max-depth=1 . | sort -n'
It prints sizes of all files and 1-st level subdirectories of current dir.
Method 4
With the current version of gnu sort (and borrowing @penguin359 file pattern)
cd /tmp; du -sShc .[^.] .??* * | sort -h
With an older version of sort
cd /tmp
foo=$(du -sShc .[^.] .??* *)
for u in K M G T; do
echo "$foo" | egrep "^[0-9.]+$u" | sort -n
done
EDIT: added -S parameter to du to not include subdirectories.
Method 5
UPDATE: I’ve scrapped the previous script. Here is a new version, using
du and awk (the previous one used tree and sed)
This is the output of: dusort ~/test 1
================ dir 4.10 KiB /home/user/test/bdir dir 4.98 KiB /home/user/test/Kdir dir 104.91 MiB /home/user/test/Mdir dir 587.47 MiB /home/user/test/Gdir dir 692.39 MiB /home/user/test ================ f 0 Byt /home/user/test/new file f 42 Byt /home/user/test/.hi dd en ================
Here is the script
units() { awk -v pfix="$1"
'BEGIN { yect=6 # Array element-count
split("Byt KiB MiB GiB TiB PiB",lbl)
for (i=1;i<=yect;i++) { val[i] = (2**(10*(i-1)))-1 }
}
{ yess=yect # Array element-subscript
while ( $1 < val[yess] ){ yess-- }
num = $1 / (val[yess]+1)
sub(/^[0-9]*t*/,"")
if (yess!=1) { printf "%s %8.2f %s %sn", pfix, num, lbl[yess], $0 }
else { printf "%s %5d %s %sn", pfix, num, lbl[yess], $0 }
}'
}
tdir="/tmp/$USER/$(basename $0)"
[[ ! -d "$tdir" ]] && mkdir -p "$tdir"
file="$tdir/$(date +%N)"
echo "================"
dirs="$file.dirs"; du --max-depth=$2 -b $1 >"$dirs" ; <"$dirs" sort -n | units "dir"
echo "================"
filz="$file.filz"; { du --max-depth=$2 -ab $1 ; cat "$dirs" ; } | sort -n | uniq -u | units " f "
echo "================"
rm "$file."*
#
Method 6
find /tmp -exec du {} + | sort -nr | less
shows biggest files first, so you can quit as soon as you’ve seen enough.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0