How to Find All Large Files in the Root Filesystem in Linux?

In Linux, finding the large files in the root filesystem is useful to identify and manage disk space usage. It provides an understanding of files/directories that are utilizing the large disk space. It also assists the users to manage disk space by clearing unnecessary big files. 

Considering the significance of the large files, this article will explain all possible methods to find the large files in the root filesystem in Linux.

Method 1: Using the find Command

The “find” command is a powerful tool for searching for files in a directory hierarchy. To find all large files in the root filesystem, execute the “find” command with the following parameters: 

  • find /: This specifies the starting directory for the find command. 
  • -type f: This tells find to only search for regular files (i.e., not directories, symlinks, etc.).
  • -size +100M: This tells find to search for files larger than 100 megabytes in size.
  • -exec ls -lh {}; This tells find to execute the ls -lh command on each file that matches the criteria specified above. 
  • awk ‘{ print $NF “: ” $5 }’: This awk command prints the last field of the input (i.e., the filename) followed by a colon and the fifth field (i.e., the file size) in a format.
$ find / -type f -size +100M -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'

The output shows that all files in the root directory are larger than 100 MB.

Method 2: Using the du Command

The “du” command can be utilized to estimate the disk usage of a directory. To find all large files in the root filesystem in Linux, use the “du” command with the “-ah” option. The description of the command is provided below:

  • du: This command stands for “disk usage” and is used to estimate file space usage.
  • -ah: This option tells du to display sizes in a “human-readable” format (i.e., in kilobytes, megabytes, etc.) and to display file sizes for both directories and individual files.
  • –max-depth=1: This option only displays the file size information for the root directory and its immediate subdirectories, and does not display information for deeper nested directories.
  • /: This specifies the starting directory for the du command.
  • sort -hr: This sorts the output of the du command in reverse order (-r) based on the file size in a human-readable format (-h).
  • head -n 20: This displays the top 20 lines of the sorted output.
$ sudo du -ah --max-depth=1 / | sort -hr | head -n 20

The above commands display all large files in the root filesystem in the human-readable format.

Method 3: Using the ncdu Command

The “ncdu” is a disk usage analyzer that provides a text-based user interface for exploring the disk usage of a directory. 

Install ncdu in Linux

To use “ncdu”, users first need to install it by running the following command:

$ sudo apt install ncdu     #Debian, Ubuntu and LinuxMint
$ sudo yum install ncdu     #CentOS-based
$ sudo dnf install ncdu     #Fedora-based

Example: Finding All Large Files in the Root FileSystem

Once installed, you can run the following command to find all large files in the root filesystem in Ubuntu:

$ ncdu /

It launches the “ncdu” interface and display the disk usage of the root directory (/):

Now users can view the size of each file and directory in descending order.

Conclusion

To find all large files in the root filesystem in Linux, you can utilize the “find”, “du”, and “ncdu” commands. These commands have multiple options to specify the size limit, sorting order, and path directory to display in the terminal. This guide has provided all feasible methods to find out large files in the root filesystem in Linux.