Home » Questions » Computers [ Ask a new question ]

Linux utility for finding the largest files/directories [closed]

Linux utility for finding the largest files/directories [closed]

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

Asked by: Guest | Views: 236
Total answers/comments: 5
Guest [Entry]

"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"
Guest [Entry]

"For a quick view:

du | sort -n

lists all directories with the largest last.

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

or, again, avoiding the redundant * :

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

lists all the directories in the current directory with the largest last.

(-n parameter to sort is required so that the first field is sorted as a number rather than as text but this precludes using -h parameter to du as we need a significant number for the sort)

Other parameters to du are available if you want to follow symbolic links (default is not to follow symbolic links) or just show size of directory contents excluding subdirectories, for example. du can even include in the list the date and time when any file in the directory was last changed."
Guest [Entry]

"A GUI tool, KDirStat, shows the data both in table form and graphically. You can see really quickly where most of the space is used.

I'm not sure if this is exactly the KDE tool you didn't want, but I think it still should be mentioned in a question like this. It's good and many people probably don't know it - I only learned about it recently myself."
Guest [Entry]

"A Combination is always the best trick on Unix.

du -sk $(find . -type d) | sort -n -k 1

Will show directory sizes in KB and sort to give the largest at the end.
Tree-view will however needs some more fu... is it really required?

Note that this scan is nested across directories so it will count sub-directories
again for the higher directories and the base directory . will show up at the end as the total utilization sum.

You can however use a depth control on the find to search at a specific depth.
And, get a lot more involved with your scanning actually... depending on what you want.
Depth control of find with -maxdepth and -mindepth can restrict to a specific sub-directory depth.

Here is a refined variation for your arg-too-long problem

find . -type d -exec du -sk {} \; | sort -n -k 1"
Guest [Entry]

I like gt5. You can navigate the tree and open subdirectories to drill down for more detail. It uses a text-mode web browser, such as lynx, to display the results. Install elinks for best results.