Get top N files in X directory

largest() { dir=${1:-"./"}; count=${2:-"10"}; echo "Getting top $count largest files in $dir"; du -sx "$dir/"* | sort -nk 1 | tail -n $count | cut -f2 | xargs -I file du -shx file; }
You can simply run "largest", and list the top 10 files/directories in ./, or you can pass two parameters, the first being the directory, the 2nd being the limit of files to display. Best off putting this in your bashrc or bash_profile file
Sample Output
$ largest
Getting top 10 largest files in ./
711M	./Havoc.2005.UNRATED.DVDRip.XviD-PuRE
719M	./Lincoln 2012
799M	./[ ] - Here Comes the Boom (2012) BluRay 720p 800MB Ganool.mkv
950M	./Watchmen
1.2G	./Sucker Punch
1.4G	./The Buried Life
1.4G	./Stop-Loss
2.5G	./Django Unchained 2012 DVDSCR X264 AAC-P2P
 14G	./Justified Season 2 Complete 720p
 15G	./Justified Season 1 Completed 720p

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: