Having an error, no space left on the device?
But you don't know which folder is consuming the most space?
Bash script below will get all the folder size and display
the size for each folder.
How to use the script below?
Open your favourite terminal editor, vi, vim or other
editor.
At terminal, type: vi dirfoldersize.sh
Copy and paste the code below to the editor.
Save the file.
Then make the script executable, chmod +x dirfoldersize.sh
Run the script by typing, ./dirfoldersize.sh
If everything goes well, you will get the folder size for
each folder on the directory where the script runs. See sample output below.
===============
#code begins start copy below
#!/bin/bash
curdir=$(pwd)
compath="$curdir/dirlist.txt"
ls -l | grep ^d | awk '{print $9}' > $compath
while IFS= read -r var
do
foldersize=$(du -sh
$var |awk '{print $1}')
echo
"$curdir/$var folder size: $foldersize"
done < "$compath"
#code ends, end copying from above line
===============
Code explanation:
#!/bin/bash == bash script header
curdir=$(pwd) == get the current working directory
compath="$curdir/dirlist.txt" == complete path
plus the file name of the file
ls -l | grep ^d | awk '{print $9}' > $compath
ls -l | grep ^d ==
grab only folders or directories
awk '{print $9}' == print only column 9 which has the
directory names
> $compath == save or redirect the output to the text
file in compath variable
foldersize=$(du -sh $var |awk '{print $1}') == get the size
of the folder in human readable for and grab the folder size only
echo "$curdir/$var folder size: $foldersize" ==
display a nice output for the user
Sample output:
/var/crash folder size: 4.0K
/var/cvs folder size: 4.0K
/var/db folder size: 120M
/var/empty folder size: 8.0K
/var/games folder size: 2.0T
/var/gdm folder size: 4.0K
/var/lib folder size: 137M
/var/local folder size: 4.0K
Cheers..till next time..happy scripting.
================================
Free Android Apps:
Click on links below to find out more:
Multiplication Table for early learners
Catholic Rosary Guide for Android:
Divine Mercy Chaplet Guide (A Powerful prayer):
Comments
Post a Comment