How to Find Large Files in Linux
We’ve all gotten to that point on some system where we start to run out of storage space. Are we buying more storage, perhaps one of the best SSDs, or are we searching and finding the largest files quickly? In this guide, we’ll look at some simple approaches to help us maintain and manage our file systems.
All of the commands in this article will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through the terminal. If you’re not already at the command line, you can open a terminal window on most Linux machines by pressing ctrl, alt and t.
List files in order of size using ls command in Linux
The ls The command is used to list the contents of a directory in Linux. By adding the -lS argument, we can order the returned results based on file size. We copied a collection of files into a test directory to display this command, but it can be run in any directory of your choice.
To list the contents of the directory in descending order of file size, use the ls order with the -IS dispute. You will see the larger files at the top of the list descending to the smaller files at the bottom.
Although this command is useful to see, it lacks the actual size of files, so how can we identify the largest files in Linux and display their size?
Identifying files larger than a specified size in Linux
In another article we explained how to find files in linux using the to find command to search based on a filename or part of a filename. We can also use the to find order in combination with -Cut argument specifying a size threshold where any file larger than specified will be returned.
1. Use to find to search for any file larger than 100 MB in the current directory. We work within our test directory and the “.” indicates to search in the current directory. The -type f The argument specifies the files returned as results. Finally the +100M The argument specifies that the command will only return files larger than 100MB. We only have one file in our test folder Baby_Yoda.obj greater than 100 MB.
find . -type f -size +100M
2. Use the same command, but this time specify a path to search. We can run the same command as in the previous section but replacing the “.” with a specified path. This means that we can search for the test directory of residence phone book.
cd find ./test -type f -size +100M
Searching for large files throughout the Linux file system
It is sometimes useful to search the whole linux file system for large files. We may have hidden files in our home directory that need to be deleted. To search the entire filesystem, we will need to use the command with sudo. We might also want to limit the search to the current filesystem, which can be achieved via the -xdev argument, for example when we suspect that the files we are looking for are in our current main filesystem or we can choose not to add the argument -xdev argument which will then include results from other mounted filesystems, for example an attached USB drive.
1. Open a terminal.
2. Check the current file system for files larger than 100 MB. As we invoke root privileges using sudo we will need to enter our password. Note that we use / to set the command to search the entire file system from the file system root.
sudo find / -xdev -type f -size +100M
3. Search all file systems for files larger than 100MB. For this example, connect a USB stick containing a collection of files, including some larger than 100MB. You should be able to scroll through the returned results and see that the largest files from the USB stick have been included in the results.
sudo find / -type f -size +100M
Find the 10 largest Linux files on your disk
What are the top ten files or directories on our machine? How big are they and where are they located? Using a bit of Linux command line magic, we can target these files with a single line of commands.
1. Open a terminal.
2. Use the from to search for all files, then use two pipes to format the returned data.
du -aBM will search for all files and directories, returning their sizes in megabytes.
/ is the root directory, the starting point of the search.
2>/dev/null will send all errors to /dev/null ensuring that no errors are displayed on the screen.
| sort -nr is a pipe that sends the output of from command to be the input of to sort which are then ranked in reverse order.
| head -n 10 will list the first ten files/directories returned by the search.
sudo du -aBm / 2>/dev/null | sort -nr | head -n 10
3. Press Enter to run the command. It will take some time to run because it has to check every directory in the file system. When finished, it will return the ten largest files/directories, their sizes and locations.
With this collection of commands, you have several ways to identify and locate large files in Linux. It’s extremely useful to be able to do this when you need to quickly select large files for deletion to free up your precious system resources. As always, be careful when digging through your filesystem to make sure you don’t delete anything critical!