Friday, January 10, 2020

disk space - Linux utility for finding the largest files/directories





I'm looking for a program to show me which files/directories occupy the most space, something like:




74% music
\- 60% music1
\- 14% music2
12% code
13% other


I know that it's possible in KDE3, but I'd rather not do that - KDE4 or command line are preferred.


Answer




To find the largest 10 files (linux/bash):



find . -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}



To find the largest 10 directories:



find . -type d -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}



Only difference is -type {d:f}.




Handles files with spaces in the names, and produces human readable file sizes in the output. Largest file listed last. The argument to tail is the number of results you see (here the 10 largest).



There are two techniques used to handle spaces in file names. The find -print0 | xargs -0 uses null delimiters instead of spaces, and the second xargs -I{} uses newlines instead of spaces to terminate input items.



example:



$ find . -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}

76M ./snapshots/projects/weekly.1/onthisday/onthisday.tar.gz
76M ./snapshots/projects/weekly.2/onthisday/onthisday.tar.gz

76M ./snapshots/projects/weekly.3/onthisday/onthisday.tar.gz
76M ./tmp/projects/onthisday/onthisday.tar.gz
114M ./Dropbox/snapshots/weekly.tgz
114M ./Dropbox/snapshots/daily.tgz
114M ./Dropbox/snapshots/monthly.tgz
117M ./Calibre Library/Robert Martin/cc.mobi
159M ./.local/share/Trash/files/funky chicken.mpg
346M ./Downloads/The Walking Dead S02E02 ... (dutch subs nl).avi

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...