- 3 Ways to find largest files in Linux
- Find the Largest Files with find command in Linux
- Find the Largest Files with du command in Linux
- Find the Largest Files with ls command in Linux
- How to Find Out Top Directories and Files (Disk Space) in Linux
- How to Find Biggest Files and Directories in Linux
- Find Largest Directories in Linux
- Find Out Top File Sizes Only
- Related Posts
- 28 thoughts on “How to Find Out Top Directories and Files (Disk Space) in Linux”
3 Ways to find largest files in Linux
In this blog post, we will discuss three different ways to find the largest files on a Linux system. This is a handy trick to know if you are running out of disk space and need to free up some space.
We will be using the “du” command, the “find” command, and the “ls” command to find the largest files. Let’s get started!
The following commands can be used to find the largest files.
- find /path/to/directory -type f -exec du -hs <> \;| sort -rh | head -n 1
- du -sh * | sort -rh | head -1
- ls -lSh /bin | head -1
- find ./ -type f -exec du -sh <> \; |sort -h|tail -1
- du -ah /home | sort -h -r | head -n 1
- find $directory -type f -exec ls -s <> \; | sort -n | tail -n 1
Find the Largest Files with find command in Linux
You can easily find the largest files in Linux using this command.
find /path/to/directory -type f -exec du -hs <> \;| sort -rh | head -n 1
This command will list all the files in the specified directory and print out the size of each file in human-readable format. It then sorts the output by file size to find the largest files.
The find command in Linux is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them.
It supports searching by file, folder, name, modification date, owner and permissions. By using the ‘-exec’ other Linux commands can be executed on files or folders found.
Let’s break down the command:
find /path/to/directory -type f -exec du -hs <> \;| sort -rh | head -n 1
Command | Description |
---|---|
find /path/to/directory -type f | List all the files under the specified directory and subdirectories |
du -sh | Display file size in human-readable format |
sort -rh | Reverse the result based on human-readable numbers |
head -1 | Display the first largest file |
If you want to find the largest files on the entire system, you can use the following command:
find / -type f -exec du -hs <> \; | sort -rh | head -n 1
This command will take a long time if you have many files on your server.
For instance, if you want to find the largest file in the directory /home/user/documents, you can run:
find /home/user/documents -type f -exec du -hs <> \; | sort -rh | head -n 1
It will list all the files within that directory and its subdirectories, calculate their sizes, sort them in descending order, and display the information of the largest file.
In this example, /home/user/documents/large_file.txt is the largest file within the specified directory, with a size of 2.5 gigabytes.
Find the largest files under the current directory including sub directory
find ./ -type f -exec du -sh <> \; |sort -rh|head -n 1
Find the largest directory under the current directory
find ./ -maxdepth 1 -mindepth 1 -type d -exec du -hs <> \;| sort -rh | head -n 1
Find the Largest Files with du command in Linux
The du command is used to estimate the space used by a file or directory. We can pipe the output of the du command to the sort command to sort the files by size. Then use the head command to print the first few lines of the output.
Open the terminal and type this command. It will list the largest files and directories under the current directory.
We use the following commands in our examples.
Command | Description |
---|---|
du -sh | Display file and directory size in human-readable format |
sort -rh | Reverse the result based on human-readable numbers |
head -1 | Display the first largest files |
This command is pretty useful when you need to find out which file or directory uses the most disk space under one specific directory.
For example, if we need to get the largest files under /etc directory, we can run the following commands.
cd /etc/
du -sh * | sort -rh | head -1
du -h -d 1 /etc/ | sort -rh | head -1
The commands du -sh * | sort -rh | head -1 and find ./ -type f -exec du -sh <> \; | sort -rh | head -n 1 serve a similar purpose of finding the largest file in a directory, but they differ in their approach.
- This command uses the du command to calculate the disk usage of each file and directory in the current directory (*).
- The du -sh * part lists the disk usage of each file and directory in a human-readable format.
- The output is then piped to sort -rh to sort the results in reverse order based on human-readable sizes.
- Finally, head -1 displays only the first line, which represents the largest file or directory.
find ./ -type f -exec du -sh <> \; | sort -rh | head -n 1:
- This command uses the find command to locate all regular files (-type f) starting from the current directory (./).
- The -exec du -sh <> \; part executes the du command on each file found and displays the disk usage in human-readable format.
- The output is then piped to sort -rh to sort the results in reverse order based on human-readable sizes.
- Finally, head -n 1 displays only the first line, which represents the largest file.
Both commands achieve the same goal of finding the largest file, but they differ in how they generate the file list. The first command (du -sh *) includes all files and directories in the current directory, while the second command (find ./ -type f) specifically focuses on regular files within the current directory and its subdirectories.
Choose the appropriate command based on your requirements and the specific context of the task at hand.
Find the Largest Files with ls command in Linux
The ls command is one of the most basic commands in Linux, and it is used to list the contents of a directory.
By default, the ls command sorts files alphabetically, but you can also use it to sort files by size, by date, or by other attributes.
When encountering issues or errors related to a specific directory, checking the largest files can help pinpoint potential problem areas. By reviewing the largest files, you might identify corrupted files, log files that have grown too large, or unexpected file sizes that could be causing the issue.
If you want to see the human readable form of file sizes, you can use the -lhS option. This will show you the files in a long list format and sort them by human readable file size.
For example, we can use the following command to get the 5 largest files under /bin directory.
If you want to see the reverse order of file sizes, you can use the -lrhS option. This will show you the files in a long list format and sort them by reverse order of file size.
Command | Description |
---|---|
find / -type f -exec du -hs <> ; | sort -rh | head -n 1 | Finds all regular files starting from the root directory (“/”), calculates the disk usage for each file, sorts them in reverse order based on human-readable sizes, and displays the information of the largest file. |
ls -lSh /path/to/directory | head -1 | Lists the files and directories in the specified directory (“/path/to/directory”), displays detailed file information in long format, sorts them by size in descending order, and shows only the information of the largest file or directory |
du -sh * | sort -rh | head -1 | Calculates the disk usage of all files and directories in the current directory, displays the sizes in human-readable format, sorts them in reverse order based on size, and shows only the information of the largest file or directory |
How to Find Out Top Directories and Files (Disk Space) in Linux
As a Linux administrator, you must periodically check which files and folders are consuming more disk space. It is very necessary to find unnecessary junk and free up from your hard disk.
This brief tutorial describes how to find the largest files and folders in the Linux file system using du (disk usage) and find command. If you want to learn more about these two commands, then head over to the following articles.
How to Find Biggest Files and Directories in Linux
Run the following command to find out top biggest directories under /home partition.
# du -a /home | sort -n -r | head -n 5
The above command displays the biggest 5 directories of my /home partition.
Find Largest Directories in Linux
If you want to display the biggest directories in the current working directory, run:
Let us break down the command and see what says each parameter.
- du command: Estimate file space usage.
- a : Displays all files and folders.
- sort command : Sort lines of text files.
- -n : Compare according to string numerical value.
- -r : Reverse the result of comparisons.
- head : Output the first part of files.
- -n : Print the first ‘n’ lines. (In our case, We displayed the first 5 lines).
Some of you would like to display the above result in human-readable format. i.e you might want to display the largest files in KB, MB, or GB.
The above command will show the top directories, which are eating up more disk space. If you feel that some directories are not important, you can simply delete a few sub-directories or delete the entire folder to free up some space.
To display the largest folders/files including the sub-directories, run:
Find out the meaning of each option using in above command:
- du command: Estimate file space usage.
- -h : Print sizes in human-readable format (e.g., 10MB).
- -S : Do not include the size of subdirectories.
- -s : Display only a total for each argument.
- sort command : sort lines of text files.
- -r : Reverse the result of comparisons.
- -h : Compare human readable numbers (e.g., 2K, 1G).
- head : Output the first part of files.
Find Out Top File Sizes Only
If you want to display the biggest file sizes only, then run the following command:
# find -type f -exec du -Sh <> + | sort -rh | head -n 5
To find the largest files in a particular location, just include the path beside the find command:
# find /home/tecmint/Downloads/ -type f -exec du -Sh <> + | sort -rh | head -n 5 OR # find /home/tecmint/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5
The above command will display the largest file from /home/tecmint/Downloads directory.
That’s all for now. Finding the biggest files and folders is no big deal. Even a novice administrator can easily find them. If you find this tutorial useful, please share it on your social networks and support TecMint.
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
28 thoughts on “How to Find Out Top Directories and Files (Disk Space) in Linux”
I need a shell script to display the largest folder in the server directory and user information. Reply
$ du -s * | sort -rn | awk '' drwxr-xr-x 33 tecmint tecmint 4096 Nov 16 10:46 .
Sorry for the two mistakes. First, if running as root, wildcard («*») expansion includes “dot directories“, and current dir («.») would always be the biggest one since it includes all others. And there was also a mistake in the awk statement. So this should do the trick: $ find . -mindepth 1 -maxdepth 1 -type d | xargs du -s * | sort -rn | awk ‘
$ find . -mindepth 1 -maxdepth 1 -type d | xargs du -s * | sort -rn | awk '' du: cannot access './VirtualBox': No such file or directory du: cannot access 'VMs': No such file or directory drwxr-xr-x 17 tecmint tecmint 184320 Nov 16 14:52 Downloads
@Ravi Sorry for replying here; the “reply link” is missing from your latest comment. > “but the command giving an error, see below” sorry again, remove the «*» from «xargs du -s *» .
The fixed command is :
$ find $HOME -mindepth 1 -maxdepth 1 -type d | xargs du -s | sort -rn | awk ‘
Thank you sharing this. I was trying to find these commands from ages and now I got the write answer Reply
It appears that if you use -h (human readable) with du, the sort gets confused as it doesn’t interpret the decimal unit prefix (i.e. the G, M, or K) properly, and you get the largest displayed numeric value. For example: 9G, 10K, 8K, 4M, 7G would sort to 10K, 9G, 8K, 7G, 4M. I noticed this when I got the following results:
# du -h /data.source/ | sort -n -r | head -n 5 1020K /data.source/mod_tile/default/9/0/0/1/176 1020K /data.source/mod_tile/default/10/0/0/1/178 1016K /data.source/mod_tile/default/11/0/0/35/65 996K /data.source/mod_tile/default/9/0/0/16/170 992K /data.source/mod_tile/default/9/0/0/1/160
# du /data.source/ | sort -n -r | head -n 5 680595148 /data.source/ 597114072 /data.source/pgsql 597114068 /data.source/pgsql/9.6 597114060 /data.source/pgsql/9.6/data 595032120 /data.source/pgsql/9.6/data/base
Hello Ravi, My questioned is to find the big file for particular date says we have thousand files of in folder with different date and need to find the biggest file of december 2016. Reply