Skip to main content

Linux bash script get folder size


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.

Comments

Popular posts from this blog

WMIC get computer name

WMIC get computer model, manufacturer, computer name and  username.
WMIC is a command-line tool and that can generate information about computer model, its manufacturer, its username and other informations depending on the parameters provided.
Why would you need a command line tool if there’s a GUI to check?
If you have 20 or 100 computers, or even more. It’s quite a big task just checking the GUI to check the computer model and username.
If you have remote computers, you need to delegate someone in the remote office or location to check.
Or you can just write a batch file or script to automate the task.
Here’s the code below on how get computer model, manufacturer and the username.
Open an elevated command prompt and type:
wmic computersystem get "Model","Manufacturer", "Name", "UserName"
Just copy and paste the code above, the word “computersystem” does not need to be change to a computer name.
A sample output below will be generated if the co…

Unable to install Teamviewer on Windows 7 and Windows 8

Installing Teamviewer on Windows 7 and Windows 8, has an error message of “rollback framework could not be initialized, installation aborted”.
To resolve this issue login to an administrator user account.
Go to an elevated command prompt and enable the hidden admin account.

Click on the link below, to check on how to open an elevated command prompt using Windows 7.

http://quickbytesstuff.blogspot.sg/2014/10/open-elevated-command-prompt.html


To open an elevated command prompt in Windows 10 check out link below:

http://quickbytesstuff.blogspot.sg/2015/08/quickly-access-command-prompt-or.html


Type:  net user administrator /active:yes
If the command prompt is not elevated you will receive this error:
System error 5 has occurred. Access is denied.
However if the command is successful, it will show:
The command completed successfully.