- Home
- Knowledge Base
- Linux Tips and Tricks
- File Management
- 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
- 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. - 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
). - sort: This command sorts lines of text. The
-nr
option instructssort
to sort the output in reverse numerical order, from largest to smallest. - 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
find . -type f
: This section directsfind
to search for regular files (-type f
) in the current directory (.
).-printf '%s %p\n'
: This part specifies thatfind
should print the file name (%s
) and path (%p
) for each found file to standard output (\n
).sort -nr
: This segment instructssort
to sort the output offind
by file size (-n
) in reverse order (-r
), displaying the largest files first.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
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.
-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.
%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.
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.
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.
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
To include subdirectories in the search, use the -r
option with the find
command. This will recursively search all subdirectories within the specified directory.