Linux Commands

Find large files Linux

A file system is the basic part of any operating system used to hold different data types in files and folders. These files and folders are necessary to provide an organized way to sequence the system’s data. Like many other operating systems, Linux has come up with its file system, which is slightly different from other operating systems. Some files and folders hold more data than others and take more storage on our system. Linux has come up with many of the commands to get information regarding the large files in our system. If you want to know about the largest files and directories of your Ubuntu 20.04 Linux operating system, then this article is meant for you. So, let’s start with the terminal launch using the “Ctrl+Alt+T.”

Update and Upgrade the System:

We will be starting this article with the system update. The update is quite important for Linux users when performing new commands, most probably when you want to install or download something from the command-line terminal. This is also necessary for the further processing of listing the largest files of the system. So, within the shell terminal, we need to use the “apt” update instruction with sudo privileges and press Enter to execute it. It will start updating your system and packages and be completed within some minutes.

$ sudo apt update

The next step after the system update is to upgrade our Ubuntu 20.04 Linux operating system. You need to utilize the apt “upgrade” command in the shell and the sudo privileges. It will start upgrading your system and show you a list of packages that will be upgraded, i.e., snapd.

$ sudo apt upgrade

While upgrading, the process will require your process confirmation by showing you the additional disk space it needs from your system. You have to tap “Y” to continue the upgrade.

It will be upgraded within a few minutes and, in some cases, in seconds if it’s already fully upgraded. The processing output for the package upgrade is shown below.

Use of “du” Instruction:

The word “du” stands for “disk usage” in Linux. It is used to find the largest space holding files and folders in the system. The “du” instruction can use many of the different flags. Let’s start with taking a look at the “du” command in the shell by mentioning the other options. Then, the word “du” will be used to get the space usage by the particular file or folder, followed by the flag “-a” used to count the files and path to the folder, i.e.,/home. We have been using the “sort” command within the “du” command to sort the files and folders outputted by the “du” command in ascending order of the memory space they contain, followed by the “-n” and “-r” flag. The “head” command has also been used within the same command to specify the total numbers of files or folders you want to list on your shell using the “-n” flag with the number, i.e., 5. The command shows the 5 records in ascending order.

$ sudo du -a /home/ | sort -n -r | head -n 5

You want to see the largest files and folders within the /var folder. The same command will be utilized with the path “/var.” Also, we have been specifying the number 10 in the head section of this command to list 10 records. The output shows the 10 records for the largest to smallest space holding folders in the /var directory of the Ubuntu 20.04 system.

$ sudo du -a /var | sort -n -r | head -n 10

The “du” command is difficult for some Linux users to understand. We also have to utilize the other flag options in the “du” command to make it easy for them. The first flag we will be using is the “-hsx” flag to display the output for the “du” command in a human-readable format. The “-h” option is especially used for the human-readable format, “-s” is used to display a total space of all Linux users, and “x” is used to ignore the directories on all file systems. This means this command will show you the total space used by each user file system separately on Linux. After this command execution, we have got 2 user records showing 182 MB space usage.

$ sudo du -hsx * | sort -rh | head -5

If you are using anything other than the Ubuntu 20.04 system, you can try some other methods to find out the largest file on your system. If you want to display all the largest files and folders with the space they contain in KB, GB, or MB, you have to use the character K, G, or M, respectively. S, we have been using the “for” loop to display 5 files or folders having large space in Kilobytes on the system. The output is shown below.

$ for i in k; do du –ah | grep [0-9]$i | sort –nr –k 1; done | head –n 5

The same can be utilized for GB and MB. You can see from the image below that we don’t have any folder holding space in GBs, while we have some records in MBs.

$ for i in G; do du –ah | grep [0-9]$i | sort –nr –k 1; done | head –n 5

Conclusion:

Finally! We have done all the basic examples to use the “du” command and find out the largest files on our system. We have not only displayed the largest simple files and folders from our system but also displayed the total memory held by all the users in our Ubuntu 20.04 system. We hope this article will help you learn something about Linux.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.