Updated on 5/1/2023 – As you add more and more applications to your machine, the number of files and directories on your system can start to grow quickly. This can lead to a decrease in available disk space as some of these files become larger over time. To keep an eye on your disk space and identify the largest files taking up space, it’s important to know how to locate the biggest files on your system. In this article, we’ll cover a few different methods for finding the largest files in your Ubuntu & sld ( similar Linux distributions ).
Command 1 : Find
The find command will start searching in the directory that you specify and then will proceed to all accessible subdirectories. More than one starting directory can be specified for search. The command below will help us look for large files .
First open up your terminal and type in the command :
sudo-i
Next, issue the commands below to see the largest files while skipping the directories on the computer type this :
sudo find / -type f -printf “%s\t%p\n” | sort -n | tail -1
$ find $HOME -type f -printf ‘%s %p\n’ | sort -nr | head -10
There are further options you can use in order to find large files: the -size option. The next command for instance will display all files that are larger than 100MiB (this is not 100MB, see here if you are confused):
find / -size +100M -ls
Read: How to find the size of a file or directory on Linux using du and ncdu commands
If however you want to add a range of min and max sizes, you can just proceed as the following command which will find files between 100MiB and 200MiB:
find / -size +100M -size -200M -ls
This seems like the perfect application for find:
Finally the following command might also be very relevant here :
find $DIRECTORY -type f -exec ls -s {} \; | sort -n | tail -n 5
This will find all files in the folder $DIRECTORY which undergo ls -s. The result is then sorted numerically via the sort command and finally, the last five entries are displayed.
To summarize, the command above will then display the largest 5 files in the folder $DIRETORY.
As a final word about the find command, you can for instance look for files which are more recent : less or equal than n days (-ctime -n) or which belong to specific users (-user mrlinus).
Read: Removing duplicates from content: 6 effective tools in 2022
Command 2 : du
The du command which stands for disk usage estimates file space usage. It can actually be used to fetch the files and folders which are eating up excessive amounts of space on your hard disk .
First type in the command below in order to switch to root :
sudo-i
And then issue the command below :
sudo du -a /home | sort -n -r | head -n 20
du calculates the files sizes in the home directory which will then go through a “sort” operation and finally the output will be limited thanks to the “head” argument which considers only the top 20 largest files.
In order to display the directories with the biggest sizes in the current working folder, just run:
sudo du -a | sort -n -r | head -n 10
Here is an explanation of the command above :
du command: calculates the file space usage.
a : shows all files and directories.
sort command : Sorts the input stream of text files (from the entry of the pipe).
-n : –numeric-sort : string numerical value comparison.
-r : –reverse : the result of comparisons is reversed.
head : Reads the provided list and prepares it for standard output.
-n : tells head to how many lines to return. (In our case, we limited the display to the first 10 lines).
If you want to use KB, GB , just issue the command as follows :
du -hs * | sort -rh | head -n 10
The above command will show the largest directories which are consuming excessive disk space. You can go ahead and delete them if you think they are useless in order to free up some space.
In order now to display the largest directories/files including sub-folders, run :
du -Sh | sort -rh | head -n 10
Here is a breakdown of the command above:
du: The du command
-h : The sizes will be shown in human readable format (.e.g. 3MB).
-S : The subdirectories size will not be included.
Sort : Sorts the input stream of text files (from the entry of the pipe).
-r : –reverse : the result of comparisons is reversed.
-h : human readable formatted numbers comparison enabled (e.g., 2K, 1G).
head : Reads the provided list and prepares it for standard output.
-n : tells head to how many lines to return. (In our case, we limited the display to the first 10 lines).
If you want to exclude for example error message of type “permission denied” , simply add teh parameter : 2>/dev/null like for instance:
du -a /* 2>/dev/null | sort -nr | head -n 50
In order to find all files with the size in the GB range for instance, you could use both du command and grep command :
du -h -a /dir | grep “[0-9]G\b”
Read: What you need to do to secure Ubuntu
Command 3 : ls
The ls command is used to display information about directories and files.
So, to list the 5 top largest files in the /bin directory , issue the command below :
ls -lSh /bin | head -5
Read : How to keep Ubuntu clean
Using Baobab (Disk Usage Analyzer)
The Disk Usage Analyzer, formerly known as Baobab, is a tool for examining disk usage in the GNOME desktop environment. It functions like a file explorer and provides a graphical interface that displays the contents of the disk drive in an easily readable format. With this tool, you can scan specific parts of the filesystem, such as a single folder or the entire filesystem, as well as remote directories.
In order to install Baobab, issue the command below :
sudo apt install baobab
And then simply call baobab to launch it. You will see the window below :
Now you can choose the filesystem to analyze. Below you can see the folder hierarchy along with information about the size, modification date…
On the right part of the snapshot, you will see a kind of chart map which reflects the tree information. You can use this to look at the sizes of the folders and their content as well.
Read: How to install and uninstall applications in Ubuntu ? A Beginner’s guide
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.
How did you miss ncdu, arguably the most powerful text-based tool in this category?
Will check this and update accordingly…Thank you.
Thanks for passing by. You may want to check out another of our articles on ncdu here: https://net2.com/how-to-find-the-size-of-a-file-or-directory-on-linux-using-du-and-ncdu-commands/
Still waiting for the update on the ncdu command.
Thanks for passing by. You may want to check out another of our articles on ncdu here: https://net2.com/how-to-find-the-size-of-a-file-or-directory-on-linux-using-du-and-ncdu-commands/