Daily Archives: July 6, 2016

Commands to Analyze Space through Terminal

This article lists a few ways to analyze the usage space of the hard disk through Terminal.

Space occupied by all folder sorted ascending order:
sudo du -h * | sort -h

The command ‘du’ is used in terminal which stands for disk usage. But the command alone gives listing of all files along with their size. The above command sorts the list, so that the highest space folder appears in bottom of the list. SUDO added since if you want it to run in root folder, normal command won’t have access to system folders.

Space on disk:
df -h

Above command is not path sensitive, which means you can run it from anywhere and also don’t require sudo.

Disk space usage per user:
sudo find . -printf "%u %s\n" | awk '{user[$1]+=$2}; END{ for( i in user) print i " " user[i]}'

Above script (combination of commands) prints disk usage per user and is not path sensitive. The script uses find and awk (extraction command) to give a list of users along with the space they are consuming in bytes.

Viewing first top directories taking space
du --max-depth=1 2> /dev/null | sort -n -r | head -n20

The above command would list top few directories along with their size, sorted in descending order.

Third Party Utility:

On debian, install ncdu by following command:

sudo apt-get install ncdu -s

Once install, you can use the utility ncdu to easily see the size of various folders through a friendly user interface right within Terminal. Here’s the ncdu manual: https://dev.yorhel.nl/ncdu/man
If above command don’t install ncdu try forcing the install by following command:

sudo apt-get install -f -y ncdu

With above command you can pin point the folder which is taking up space in little time. Also note that the sometimes you need to go deeper by using the command, sudo su, to view space of files taken up by say, /var directory.