Find Largest Files and directories

### CHEATSHEET ALL OF THESE OPERATE INTO CURRENT DIRECTORY: ###
# One command, one liner per line.
du -a . | sort -n -r | head -n 10
shopt -s dotglob; du -hsx * | sort -rh | head -10; shopt -u dotglob
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11
find . -printf '%s %p\n'| sort -nr | head -10
find . -type f -printf '%s %p\n'| sort -nr | head -10
shopt -s dotglob; du -cks * | sort -rn | head; shopt -u dotglob
alias ducks='shopt -s dotglob; du -cks * | sort -rn | head; shopt -u dotglob'

 

I use these all the time, so I need a cheatsheet of it. Thanks NixCraft.

Source of information: http://www.cyberciti.biz/faq/how-do-i-find-the-largest-filesdirectories-on-a-linuxunixbsd-filesystem/

method 1 (just using find):

## Warning: only works with GNU find ##
find /path/to/dir/ -printf '%s %p\n'| sort -nr | head -10
find . -printf '%s %p\n'| sort -nr | head -10

method 2 (du – in kilobytes):

# du -a /var | sort -n -r | head -n 10

method 4 (du – human readable):

cd /path/to/some/where
shopt -s dotglob
du -hsx * | sort -rh | head -10
shopt -u dotglob

Whats the dotglob shopt for? shopt enables or disable a shell option for the current running bash. So it specially tells the bash shell to consider hidden files and folders with *, or else * only looks at none hidden files/folders. shopt -s dotglob enables it, and shopt -u dotglob disables it (which is the default shell option).

DOTGLOB ARTICLE <- my article

method 4b (du – human readable, but dont have human-reable sort):

# same as above method, but not all sort commands can sort by human reable
# this plays a cool trick of searching by human reable. I cover in article listed below.
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11

Article to sortby human readable, when sort cant do it because its missing -h option: MAKESHIFT HUMAN READABLE SEARCH

method3 (ducks):

################################################
# find to 10 files/dirs eating your disk space #
################################################
alias ducks='shopt -s dotglob; du -cks * | sort -rn | head; shopt -u dotglob'
# Run it as follows to get top 10 files/dirs eating your disk space, in current folder:
cd /path/to/some/where
ducks
# note that you can add this alias to your ~/.bashrc file, so its persistent.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *