Shell Script To Find Directories Which Consume Highest Space

Shell script to show X number of directories that consume space in a path. This script useful if user need quickly to clean up space, so first user need to check what directory take most of space and the delete according to your wish.

Commands used are du with different keys , then sort and head commands.

Shell script


#check if user input argument

if [ $# -eq 0 ]; then

#if no argument print next messge and exit from script
echo "Usage: $0 "

exit 1

# Save first arguments to variables
#set value for variable count value 1

#just print empty line
echo ""

#Print next message:
echo "Here is the ${HeadValue} biggest directories located in ${CheckedDir}:"

echo ""

#Getting list of directories and space they use.
du -a --max-depth=1 --one-file-system ${CheckedDir}/ |

#next we sort result
sort -rn |

sed "1d" |

# next we get only first X directories
head -"${HeadValue}" |

#next print result to user
while read size dirrr ; do
#counting size in Mb

size="$(( size / 1024 ))"

#show output for user
echo "N°${count} : ${dirrr} is ${size} Mb"



echo ""

Script Output

./ /home/yevhen/

Here is the biggest directories located in /home/yevhen/:

N°1 : /home/yevhen//Fly is 14043 Mb

N°2 : /home/yevhen//VirtualBox VMs is 5837 Mb

N°3 : /home/yevhen//Downloads is 2224 Mb

N°4 : /home/yevhen//.icedove is 963 Mb

N°5 : /home/yevhen//Downloads is 645 Mb

N°6 : /home/yevhen//.wine is 602 Mb

N°7 : /home/yevhen//.cache is 324 Mb

N°8 : /home/yevhen//Dropbox is 324 Mb

N°9 : /home/yevhen//.config is 263 Mb

N°10 : /home/yevhen//.local is 153 Mb

script check directory space

Bobbin Zachariah 7:37 am

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.