#!/bin/bash # bigfiles # List the n (or 10) largest files in the directory specified (or the # current directory by default), and all its subdirectories, together # with their sizes. # Version 5 # Ian Barnes, 1 February 2001 # Set default values debug= number="-10" # Parse command line arguments while [ ${#} -gt 0 ] do case ${1} in -*) # Assume that what's after the - is a number number=${1} ;; *) # It's a directory dir=${1} esac shift done [ $debug ] && echo number=${number} [ $debug ] && echo dir=${dir} # Get a list of all files (and directories etc) if [ ${dir} ] then if [ -d ${dir} ] then files=$(du -ka ${dir} | cut -f2) else echo Error: ${dir} is not a directory! exit 1 fi else files=$(du -ka | cut -f2) fi [ $debug ] && echo ${files} # Make sure we don't clobber any data tmp=".tmp_bigfiles" if [ -e ${tmp} ] then echo Error: $tmp already exists! exit 1 fi # Write list of all regular files with sizes to $tmp for f in ${files} do if [ -f ${f} ] then du -ka ${f} >> ${tmp} fi done [ $debug ] && cat ${tmp} # Sort the list and print the top -${number} sort -rn ${tmp} | head ${number} # Remove the temporary file rm -f ${tmp}