How to Find Large Files in Linux

We all get to a point in this system where we start running out of storage space. Are we buying more storage space, perhaps one of the best SSDs, or are we quickly searching and finding the biggest files? In this guide, we’ll take a look at some simple approaches to help us maintain and manage our file systems.

All commands in this article will work on most Linux machines. We used an Ubuntu 20.04 install, but you could run this tutorial on a Raspberry Pi. All instructions are executed through the terminal. If you’re not already at the command line, you can open a terminal window on most Linux computers by pressing ctrl, alt, etc.

List files in order of size using ls command in linux

How to find large files 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 size of the file. We’ve copied a collection of files into a test directory to show this command, but you can run it in any directory you choose.

To list the contents of a directory in descending order of file size, use the ls command along with the -THIS argument. You will see the largest files at the top of the list descending to the smallest files at the bottom.

ls -lS

While this command is useful for viewing, it lacks the actual size of the files, so how can we determine the largest files in Linux and display their size?

Identify files larger than specified size on Linux

How to find large files in Linux

In another article, we explained how to find files in Linux using the find command to search by filename or part of a filename. We can also use the find command in combination with the -size argument, which sets a size threshold that will return any file larger than the specified size.

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

find . -type f -size +100M

2. Use the same command, but this time specify the search path. We can run the same command as in the previous section, but replace the “.” to the specified path. This means that we can search the test directory from the home directory.

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

How to find large files in Linux

Sometimes it is useful to search the entire Linux filesystem for large files. Perhaps there are some files hidden in our home directory that need to be deleted. To search the entire file system, we will need to use the command with sudo. We may also want to either limit the search to the current file system, which can be achieved with the -xdev argument, such as when we suspect the files we are looking for are on our current main file system, or we can choose not to add the argument – xdev, which will then include results from other mounted filesystems, such as an example of a mounted USB drive.

1. Open a terminal.

2. Search the current file system for files larger than 100 MB. Since we’re invoking superuser privileges with sudo, we’ll need to enter our password. Note that we use / to give 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. In this example, plug in a USB drive with a bunch of files on it, including some that are larger than 100 MB. You should be able to scroll through the returned results and see that the larger files on the flash drive were included in the results.

sudo find / -type f -size +100M

Find the 10 largest Linux files on your drive

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

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 the du command as sort input, which is then listed in reverse order.

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

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

3. Press the Enter key to execute the command. It will take a little time to run because it needs to check every directory in the file system. When completed, it will return the ten largest files/directories, their sizes and locations.

How to find large files in Linux

With this set of commands, you have several ways to identify and search for large files in Linux. This is extremely useful when you need to quickly select large files to delete in order to free up your precious system resources. As always, be careful when digging through your file system to make sure you don’t delete anything critical!

@top-advice.ru

Rate article
TOP ADVICE
Leave a Reply