Disk Management

Find Disk Space Used by Specific User Linux

Once in a while, you may need to take stock of the files owned by a specific user in a Linux system with several login users. This comes in handy when you want to free up some space and prevent your hard drive from getting depleted.  This is especially if some users have long since stopped using the system and their accounts have been disabled. So, how do you evaluate the disk space taken up by a specific user? Let’s find out.

Count the total disk space used by a particular user

To obtain the disk space used by a specific user, use the find command syntax as follows:

$ find /path/to/directory/ -user username_whose_files_are_to_be_counted -type f -printf "%s\n" | awk '{t+=$1}END{print t}'

Let’s break down this command syntax:

The first section –  find /path/to/directory/ – performs a search in the specified directory path.

The second section – -user username_whose_files_are_to_be_counted – restricts the search operation to a specific user only.

The third section – -type f  –  indicates that we are only searching for files and not directories. Empty directories usually take up 4kb, which is negligible.

The last section – -printf “%s\n” | awk ‘{t+=$1}END{print t}’

Prints out the size of the files.

Suppose you want to find out the disk usage of a user called james in the home directory. The command will be.

$ find /home  -user james -type f -printf "%s\n" | awk '{t+=$1}END{print t}'

If you are inside a directory, you can view the disk usage using the command shown,

$ find . -type f -printf "%u %s\n" \ | awk '{user[$1]+=$2}; END{for(i in user) print i,user[i]}'

For example, I will navigate to the Downloads directory, located in my home directory, and check the disk space used by specific users. The output clearly displays the disk space summary where two users have files saved in the current directory, which is the /home/james/Downloads directory.

And there you go. We have successfully seen how you can find the disk space by specific users in Linux.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.