1. Home
  2. Knowledge Base
  3. Linux Tips and Tricks
  4. File Management
  5. How to Find the Top 10 Largest Files in a Directory using Linux

How to Find the Top 10 Largest Files in a Directory using Linux

Introduction

In Linux, it’s often necessary to locate the largest files in a directory or its subdirectories. This can be useful for identifying files that are consuming a significant amount of disk space and may require attention. This knowledgebase article provides a detailed explanation of a command that can be used to find the top 10 largest files in a directory.

The Command

To find the top 10 largest files in a directory (including subdirectories), use the following command:

find . -type f -printf '%s %p\n' | sort -nr | head -10

This command utilises four main commands

  1. find: This command searches for files and directories. The . argument specifies the current directory as the search scope. The -type f option indicates that only regular files should be considered.
  2. printf: This command formats and outputs data. The %s %p\n format string specifies that the file name (%s) and path (%p) should be printed to standard output (\n).
  3. sort: This command sorts lines of text. The -nr option instructs sort to sort the output in reverse numerical order, from largest to smallest.
  4. head: This command prints the first few lines of a file. The -10 option indicates that the first 10 lines of the sorted output should be displayed.

Breaking Down the Command

  1. find . -type f: This section directs find to search for regular files (-type f) in the current directory (.).
  2. -printf '%s %p\n': This part specifies that find should print the file name (%s) and path (%p) for each found file to standard output (\n).
  3. sort -nr: This segment instructs sort to sort the output of find by file size (-n) in reverse order (-r), displaying the largest files first.
  4. head -10: This final part limits the output to the top 10 largest files from the sorted results.

Using the Command

To locate the top 10 largest files within the current directory, execute the following command:

find . -type f -printf '%s %p\n' | sort -nr | head -10

This command will print the file names and paths of the top 10 largest files in the current directory.

Customising the Command

The command can be modified to search for files with a specific extension or to include subdirectories. For instance, to find the top 10 largest PDF files in the current directory and its subdirectories, use the following command:

find . -type f -name "*.pdf" -printf '%s %p\n' | sort -nr | head -10

This command will search for all PDF files (-name "*.pdf") in the current directory and its subdirectories (.) and display the top 10 largest ones.

FAQ – Find the Top 10 Files in a Directory

What is the find command?

The find command is a powerful tool in Linux for searching for files and directories based on various criteria. It allows you to specify the search scope, file type, name, and other attributes to locate specific files or groups of files.

What does the -type f option do?

The -type f option restricts the search to regular files, excluding directories and other file types. This ensures that the output only includes files that can consume disk space.

What does the %s %p\n format string do?

The %s %p\n format string in the printf command specifies how the file information should be formatted. The %s part prints the file name, while %p prints the absolute path of the file. The \n character indicates a newline, separating each file entry.

What does the sort -nr option do?

The sort -nr option sorts the output of find by file size in reverse order (-nr). This means that the largest files will be listed first.

What does the head -10 option do?

The head -10 option limits the output of sort to the top 10 largest files. This ensures that only the most substantial files are displayed.

How can I modify the command to search for files with a specific extension?

To search for files with a specific extension, such as PDF files, use the -name option with a wildcard pattern. For example, to find the top 10 largest PDF files, use the following command:
find . -type f -name "*.pdf" -printf '%s %p\n' | sort -nr | head -10

How can I include subdirectories in the search?

To include subdirectories in the search, use the -r option with the find command. This will recursively search all subdirectories within the specified directory.

Was this article helpful?
Go to Top