How To Find Large Files on Linux

Weโ€™ve all got to that point on a given system where we start to run out of storage space. Do we buy more storage, perhaps one of the best SSDs, or do we search and find the largest files quickly? In this how to we will look at a few simple approaches to help us maintain and manage our filesystems.

All the commands in this article will work on most Linux machines. Weโ€™ve used a Ubuntu 20.04 install but you could run this how-to on aย Raspberry Pi. All of the how-to is performed via 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.

Listing Files In Size Order Using the ls Command in Linux

 

Theย lsย command is used to list the contents of a directory in Linux. By adding theย -lSย argument we can ย order the returned results according to the file size. We have copied a collection of files into aย testย directory to show this command but it can be run in any directory you choose.

Identifying Files Larger Than a Specified Size in Linux

We can use the findย command in combination with theย -sizeย argument specifying a size threshold where any file larger than specified will be returned.

1.ย Useย findย to search for any file larger than 100MB in the current directory.ย We are working inside ourย testย directory and the โ€œ.โ€ indicates to search the current directory. Theย -type fย argument specifies returning files as results. Finally theย +100Mย argument specifies that the command will only return files larger than 100MB in size. We only have one file in our test folderย Baby_Yoda.objย that is larger than 100MB.


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 replace the โ€œ.โ€ with a specified path. This means we can search theย testย directory from theย homeย directory.


cd
find ./test -type f -size +100M

Searching the Whole Linux Filesystem For Large Files

Itโ€™s sometimes useful to search the wholeย Linux filesystemย for large files. We may have some files hidden away in our home directory that need removing. To search the entire filesystem, we will need to use the command withย sudo.ย We might also want to either limit the search to the current filesystem which can be achieved via theย -xdevย argument, for example when we suspect the files we seek are in our current main filesystem or we can choose not to add theย -xdevย argument which will then include results from other mounted filesystems, for example an attached USB drive.

1.ย Open a terminal.

2.ย Search the current filesystem for files larger than 100MB.ย As we are invoking root privileges usingย sudoย we will need to input our password. Note that we are usingย /ย to set the command to search the entire filesystem from theย root of the filesystem.


sudo find / -xdev -type f -size +100M

 

3.ย Search all filesystems for files larger than 100MB.ย For this example connect a USB drive with a collection of files on it including some that are over 100MB in size. You should be able to scroll through the returned results and see that the larger files on the pen drive have been included in the results.


sudo find / -type f -size +100M


Finding the 10 Largest Linux Files on Your Drive

What are the top ten files ย or directories on our machine? How large are they and where are they located? Using a little Linux command line magic we can target these files with only one line of commands.

1.ย Open a terminal.

2.ย Use theย duย command to search all files and then use two pipes to format the returned data.


du -aBMย will search all files and directories, returning their sizes in megabytes.


/ย is the root directory, the starting point for the search.


2>/dev/nullย will send any errors to /dev/null ensuring that no errors are printed to the screen.


| sort -nrย is a pipe that sends the output ofย duย command to be the input ofย sortย which is then listed in reverse order.


| head -n 10ย will list the top ten files/directories returned from the search.


sudo du -aBm / 2>/dev/null | sort -nr | head -n 10

 

3.ย Press Enter to run the command.ย It will take a little time to run as it needs to check every directory of the filesystem. Once complete it will return the top 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 big files for deletion to free up your precious system resources. As always, take care when poking around your filesystem to ensure you arenโ€™t deleting something critical!