How to find the largest files on Linux

Most of us have installed tens of applications in our machines. As time goes on, many files will get bigger and bigger and your disk space will start to shrink. It is therefore important to know the files or directories that are eating up your machine disk space.The good news is that you can very easily find out the files with the biggest sizes in a particular directory in your Ubuntu & sld (similar linux distros). In this article, you will learn different methods to find out the largest files in your system :

Command 1 : Find

The find command will start searching in the directory that you specify and then will proceed to all accessible subdirectories. More than one starting directory can be specified for search. The command below will help us look for large files .

First open up your terminal and type in the command :

sudo-i

Next, issue the commands below to see the largest files while skipping the directories on the computer type this :

sudo find / -type f -printf “%s\t%p\n” | sort -n | tail -1

sudo command with find

$ find $HOME -type f -printf ‘%s %p\n’ | sort -nr | head -10

There are further options you can use in order to find large files: the -size option. The next command for instance will display all files that are larger than 100MiB (this is not 100MB, see here if you are confused):

find / -size +100M -ls

command sudo find with size

If however you want to add a range of min and max sizes, you can just proceed as the following command which will find files between 100MiB and 200MiB:

find / -size +100M -size -200M -ls

This seems like the perfect application for find:

Finally the following command might also be very relevant here :

find $DIRECTORY -type f -exec ls -s {} \; | sort -n | tail -n 5

This will find all files in the folder $DIRECTORY which undergo ls -s. The result is then sorted numerically via the sort command and finally, the last five entries are displayed.
To summarize, the command above will then display the largest 5 files in the folder $DIRETORY.

As a final word about the find command, you can for instance look for files which are more recent : less or equal than n days (-ctime -n) or which belong to specific users (-user mrlinus).

Command 2 : du

The du command which stands for disk usage estimates file space usage. It can actually be used to fetch the files and folders which are eating up excessive amounts of space on your hard disk .

First type in the command below in order to switch to root :

sudo-i

And then issue the command below :

sudo du -a /home | sort -n -r | head -n 20

sudo du with sort and head

du calculates the files sizes in the home directory which will then go through a “sort” operation and finally the output will be limited thanks to the “head” argument which considers only the top 20 largest files.

In order to display the directories with the biggest sizes in the current working folder, just run:

sudo du -a | sort -n -r | head -n 10

sudo du -a | sort -n -r | head -n 10

Here is an explanation of the command above :

du command: calculates the file space usage.

a : shows all files and directories.

sort command : Sorts the input stream of text files (from the entry of the pipe).

-n : –numeric-sort : string numerical value comparison.

-r : –reverse : the result of comparisons is reversed.

head : Reads the provided list and prepares it for standard output.

-n : tells head to how many lines to return. (In our case, we limited the display to the first 10 lines).

If you want to use KB, GB , just issue the command as follows :

du -hs * | sort -rh | head -n 10

du -hs * | sort -rh | head -5

The above command will show the largest directories which are consuming excessive disk space. You can go ahead and delete them if you think they are useless in order to free up some space.

In order now to display the largest directories/files including sub-folders, run :

du -Sh | sort -rh | head -n 10

du -Sh | sort -rh | head -n 10

Here is a breakdown of the command above:

du: The du command

-h : The sizes will be shown in human readable format (.e.g. 3MB).

-S : The subdirectories size will not be included.

Sort : Sorts the input stream of text files (from the entry of the pipe).

-r : –reverse : the result of comparisons is reversed.

-h : human readable formatted numbers comparison enabled (e.g., 2K, 1G).

head : Reads the provided list and prepares it for standard output.

-n : tells head to how many lines to return. (In our case, we limited the display to the first 10 lines).

If you want to exclude for example error message of type “permission denied” , simply add teh parameter : 2>/dev/null like for instance:

du -a /* 2>/dev/null | sort -nr | head -n 50

In order to find all files with the size in the GB range for instance, you could use both du command and grep command :

du -h -a /dir | grep “[0-9]G\b”

Command 3 : ls

The ls command is used to display information about directories and files.

So, to list the 5 top largest files in the /bin directory , issue the command below :

ls -lSh /bin | head -5

You may be interested to read : How to keep Ubuntu clean?

Using Baobab (Disk Usage Analyzer)

Formerly known as Baobab, Disk Usage Analyzer, is a GNOME graphical utility for analyzing disk usage. Like a file explorer, Baobab offers an easy to read menu-driven graphical interface representation of the content of the disk drive. The user can scan a selection of specific parts of filesystem be it a single folder, the filesystem in its entirety and even remote directories.

In order to install Baobab, issue the command below :

sudo apt install baobab

sudo apt install baobab

You may be interested to read: How to install and uninstall applications in Ubuntu ? A Beginner’s guide

And then simply call baobab to launch it. You will see the window below :

disk usage analyzer

Now you can choose the filesystem to analyze. Below you can see the folder hierarchy along with information about the size, modification date…

On the right part of the snapshot, you will see a kind of chart map which reflects the tree information. You can use this to look at the sizes of the folders and their content as well.

disk usage analyzer 2

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
adminsitaram Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
sitaram
Guest
sitaram

How did you miss ncdu, arguably the most powerful text-based tool in this category?

Close Menu
Show Buttons
Hide Buttons