How to Find Large Files in Linux Using Command Line?

Linux is an open-source well-known operating system that offers a list of commands to perform different operations, such as moving, copying, listing, and much more. The user can easily access and manage the system using the command line interface. Keeping this in view, this post enlists the possible methods to find large files in Linux using the command line interface with the following outcomes:

Method 1: How to Find Large Files Using “ls” Command in Linux? 

The “ls” command is beneficial for listing the available files and directories of the specified file/directory. When no directory is specified, it displays the content of the “home” directory by default.

Syntax:

The basic syntax of the “ls” command is defined below: 

$ ls [OPTION]... [FILE]...

The “ls” command offers a list of important options that are listed below in table format:

OptionsDescription
lhIdentifies the size of all the files and directories in a human-readable format such as kilobyte, Megabyte, and Gigabyte, etc.
ltShows all the files and directories arranged by the last modified date.
lrDisplay all the files and directories in reverse order.
ltrRepresents all the files and directories in reverse order when the files and directories are arranged according to the modified date.
lnPrint the UID(user identifier) and GID(group identifier) number in place of the root user name and group name. 
lRDenotes all the files, directories, and sub-directories on the terminal.
lSSort the directories and files according to their size.

Example 1: Find Files/Directories By Size

Here, we have combined two different options of the “ls” command, which are ‘l’ and ‘S’ that will display the largest to smallest file in descending order, as shown below:

$ ls -lS

The output of the above command sort all the directories and files by size.

Example 2: Find Files/Directories By Size in Human-Readable Format

The combination of the “lhs” options denotes the size of the files and directories in human-readable format, i.e., in kilobytes, as shown below: 

$ ls -lhS

Let’s have a look at the second method. 

Method 2:  How to Find Large Files Using “find” Command?

The “find” is a command line utility used to search files or directories at the provided path. The user can search with the filename, username/group name, date, permissions, type, and other parameters. It searches the directories recursively, i.e., finds all the subdirectories too.

Syntax:

The general syntax of the “find” command is written below: 

$ find [where to start searching from]
[expression determines what to find] [-options] [what to find]

The “find” command also contains a list of essential options that are listed in the below table:

OptionsDescription
-PDoes not display the symbolic links.
-HDo not follow the symbolic links.
-LDisplay the symbolic links.
-nameSearch the file with the specific file name.
-typeFind the directories with name.
-maxdepthShows the limited searched files.
-sizeSearch the file according to the specified size, e.g., 100M, 50K, etc.

Now, move to the following “find” command used to find the large files/directories in the entire Linux operating system:

$ find /path/to/directory -type f -exec du -hs {} \;| sort -rh | head -n 1

The structure of the above command is illustrated below:

  • find /path/to/directory -type f: It list down all the files available in the current directory and subdirectories.
  • du -hs: It displays all the file sizes in a human-readable format.
  • sort -rh: It shows the file sizes in reverse order.
  • head -1: It represents the top largest file of the current system. 

Some examples are described below to find the large files in Linux.

Example 1: Find the Largest File 

Run the following “find” command to search the top largest files under the current directory, as can be seen below:  

$ find ./ -type f -exec du -sh {} \; |sort -rh|head -n 1

In our case, the top largest executable file has a size “of 171 MegaBytes” in the current directory.

Example 2: Find Large Directories

Execute the below-given command on the terminal to find the largest executable directory/file under the current directory:

$ find ~/  -maxdepth 1 -mindepth 1  -type d  -exec du -hs {} \;| sort -rh | head -n 1

The output displays the file having size “354M” available in the system

Example 3: Find the File Larger Than Specific Size

The user can find a file containing a specific size from the current working directory or the specific directory. 

Suppose the below command is utilized to find the file whose size is greater than “10M” into the “home” directory:

$ find ~/ -size +10M -ls

Let’s practice the third method to find the large file in Linux. 

Method 3:  How to Find Large Files Using “du” Command?

The “du” command stands for the “disk usage” command. It determines the amount of space used by the subdirectories and the files inside the current directory. 

Syntax

The working of the “du” commands depends on its general syntax:

du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

The “du” command also contains a list of supported options that perform different operations. These options are illustrated in the below table:

OptionsDescription
-aList all the files and directories, including hidden files.
-cDisplays the total of the current directory.
-tShows the last modified time and date.
-hPrints the files/directories size in human-readable format e.g., kilobytes, megabytes, gigabytes, etc.
-sProvide the disk usage summary.
-XExcludes the unnecessary or unwanted files having extension dill.

Let’s get into the examples!

Example 1: Find the Top Largest Files in the Current Directory

Use the following ”du” command to get the top largest files and directories available in the current working directory. 

For instance, the command provided below will fetch the top five directories via the command:

$ du -sh * | sort -rh | head -5

The description of the above command is described below:

du -sh: Identifies the list of files and directories. 

sort -rh:  Arrange the files and directories by size in human-readable format, e.g., kilobytes, megabytes, etc.

head -5: Prints the top five largest files in the current directory.

The sizes and the names of the first five files/directories are 

Example 2: Find Largest Directories And Subdirectories

The “du” command is also beneficial for listing down the required no of top largest executable files, directories, and subdirectories in the specified directory. 

$ du -ahx / | sort -rh  | head -8

The directories’ sizes and absolute paths are printed on the terminal.

Conclusion 

In Linux, the “ls” and “find” and the “du(disk usage)” commands are utilized to find large files. The user can display the largest files and directories with the help of these three commands. In this post, all the possible methods are briefly illustrated to find the largest files in Linux by using the “ls”, “find”, and “du” commands.