- 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
- Finding the Biggest Files and Folders in Linux Command Line
- How to find the biggest folders in Linux?
- Adding Options
- Human-Readable Output
- Corrected Human-Readble Output
- Where are the largest files?
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 |
Finding the Biggest Files and Folders in Linux Command Line
Quick tutorial to show you how to find the biggest files on your Linux machine using a few commands that you may already be familiar with du, sort, and head.
This is a quick tutorial to show you how to find the biggest files on your Linux machine using a few commands that you may already be familiar with du, sort, and head.
To find the 10 biggest folders in current directory:
To find the 10 biggest files and folders in current directory:
du -ah | sort -hr | head -n 10
Read the rest of the article to get a detailed explanation of these commands.
How to find the biggest folders in Linux?
The du command is used for getting the disk usage. Sort command sorts the data as per your requirement. The head command displays the top lines of a text input source.
This is just one combination for getting the biggest files and directories in Linux command line. There can be several other ways to achieve the same result.
What happens if you run these three commands together without options? Your output probably won’t be very useful.
When you run these commands, unless specified with du, everything will run automatically using the current working directory as the source file.
Sort without options arranges items in numerical order, but this behavior is a little strange. 100 is considered less than 12 because 2 > 0. That’s definitely not what we want.
Head here defaults to displaying the first 10 items. Depending on the directory you want to analyze, you can tailor this to find large files quickly.
[email protected]:~$ du | sort | head 100 ./.local/share/evolution/addressbook 108 ./.mozilla/firefox/jwqwiz97.default-release/datareporting 112 ./.local/share/gvfs-metadata 12 ./.cache/fontconfig 12 ./.cache/gnome-software/screenshots/112x63 12 ./.cache/thumbnails/fail 12 ./.config/dconf 12 ./.config/evolution 12 ./.config/gnome-control-center/backgrounds 12 ./.config/ibus
Adding Options
So let’s look at what might be more typical options.
Adding -n to sort command means that items will be sorted by numeric value. Adding -r means that the results will be reversed. This is what we want when searching for the largest number.
I’m also going to add -5 to limit our results further than the default for head. This value is something that you should decide based on what you know about the system.
You may want to expand the value to a number greater than 10, or omit it entirely if there are many large files you are trying to filter. Otherwise, you may run it, delete several files, but still have space issues.
Okay, let’s put it all together and see what happens.
[email protected]:~$ du | sort -nr | head -5 1865396 . 1769532 ./Documents 76552 ./.cache 64852 ./.cache/mozilla 64848 ./.cache/mozilla/firefox
That’s better, you can quickly see where the largest files are. You can do better, though. Let’s clean it up with some more options.
Human-Readable Output
The human options for certain commands help present numbers in a way that is familiar to us. Let’s try adding that to the du command.
[email protected]:~$ du -h | sort -nr | head -5 980K ./.local/share/app-info 976K ./.local/share/app-info/xmls 824K ./.cache/thumbnails 808K ./.cache/thumbnails/large 804K ./.local/share/tracker
Corrected Human-Readble Output
Wait a second… Those numbers don’t make any sense. No, they don’t because You have only changed the content to human-readable for the du command. Sort has its own built-in function for human-readable numeric sort with -h. Both must be used to get the desired output. You can run into these kinds of issues often in Linux.
It’s important to experiment and make sure that your results “make sense” before using a command a specific way.
[email protected]:~$ du -h | sort -hr | head -5 1.8G . 1.7G ./Documents 75M ./.cache 64M ./.cache/mozilla/firefox/jwqwiz97.default-release 64M ./.cache/mozilla/firefox
Where are the largest files?
You can tell from the output that the Documents folder contains some larger files, but if you switch to that folder and run our command again, you don’t get the largest file. You get this:
[email protected]:~/Documents$ du -h | sort -hr | head -5 1.7G .
This is just telling us what you already know. The current directory, referred to as . , has 1.7G worth of files. That isn’t helpful if you’re trying to find single, unusually large files.
You need to add another flag to du for this task. Using option -a, you can get the output that we’re looking for. Let’s try it.
[email protected]:~/Documents$ du -ah | sort -hr | head -5 1.7G . 1.1G ./1gig-file.file 699M ./doc.tar 2.9M ./photo-of-woman-wearing-turtleneck-top-2777898.jpg 1.4M ./semi-opened-laptop-computer-turned-on-on-table-2047905.jpg
Did you enjoy this guide to finding large files in Linux? I hope all of these tips taught you something new.
If you like this guide, please share it on social media. If you have any comments or questions, leave them below.