How to see the size of directories and find big files and directories in Ubuntu?

Are you out of disk space? Would you like to find out what eats all your disk space? It is very annoying to run out of disk space. Not only will low disk space hinder you from installing new programs, it can also cause problems for programs already installed on your system. What can you do to investigate this?

Here I will show you a couple of commands to help you find the information you need using the Ubuntu command line.

A good place to start is to find the total capacity of your hard drive. A great way to find it is by running the following command.

How to see the size of directories and find big files and directories in Ubuntu?

See the total capacity of your hard drive.

df -hlT

The df command stands for disk free, and it will show you the size of the hard drives and partitions and also the amount of occupied and free space.

df command in ubuntu

As you can see, the main partition runs with the ext4 file system and has a total size of 8.1GB, of which 2.7GB is free space.

Now, let us investigate where that 4.9 GB of used space is located.

See the size of the directions and subdirectories in Ubuntu.

Now we will run a command showing us the size of the different directories in the root of the partition running the Ubuntu system.

sudo du -h --max-depth=1 /

The command tells the system to show the size of the directories in the root directory. If you want to see a longer list, including the deeper subdirectories, you can set the depth to 2 or a higher number.

du command ubuntu

As you can see, the /boot directory occupies 247MB disk space, the /usr directory 3.1GB, and the /home directory 20MB, and the /snap directory 1.4GB.

You don’t want to touch directories you don’t know much about. But the /home directory and the /snap directory might include files you don’t necessarily need. Let us take a look at what’s hidden within those directories.

Here you can see an example of a /snap directory containing many installed programs and files.

size of snap directory

The total size of the /snap directory is 8,9GB. If you want to sort it by size, you will have to remove the -h argument (showing the file size in more readable numbers) and see the total size in bytes instead. You can do that with the following command.

sudo du --max-depth=1 /snap | sort -n -r

Here you will see a list of directories, starting with the largest directory. I quickly see that the Firefox directory occupies more than 1.3GB. Should I delete the browser? Should I decrease the size of the cache stored on the machine? Are there smaller browsers? These are all questions you can ask based on this. Should I delete the VLC Media Player and use a different media player instead?

Another directory that you can influence is the /home directory.

If you run the commands you have seen earlier, you will quickly discover what directories and subdirectories occupy a lot of space. Maybe you have many downloaded files in the /home/user/Downloads directory? You will quickly discover if you run the commands you have seen above.

Which files to delete to free disk space?

Have you found the directory occupying a lot of space? Which are the biggest files in that given directory? You can run the following command to find out.

ls -lSr | grep ^-

This command will list the files in the directory (and exclude the directories) in reverse order. As a result, the largest files will appear at the list’s bottom. The command is running in the folder you are in. As a result, you should be in the directory of interest when you run the command. You can also add the location you are interested in by running the command like this.

ls -lSr /home/user/Downloads | grep ^-

I hope these instructions have helped you. If you have further questions, comments, or something else on your heart, please write a comment below.

Leave a Reply