In the last few decades the disks inside our PCs have gotten more and more spacious; for example now is starting to be common to have multiple terabytes. But still it doesn’t mean that they can hold infinite data. If your disk is getting full, you can find large files on Linux easily.
The following procedure to find large files on Linux rely on the terminal, as we are going to use the commands du (which stands for Disk Usage), sort (the name is self-explanatory) and head (showing only the first X results, 10 by default), so open it.
Find large files on Linux with DU
To find the 10 largest folders on the file system, you can use the following command:
du -hs */ | sort -hr | head
On the above screenshot the command is run on the home folder; if you want to run on a system folder, like the root /, you will need system privileges and therefore you have to use either
sudo or login as root.
To explain how it works,
du -hs analyze the space occupied by files in the folder and show them in a human readable format,
sort -hr put them in order (in this case reverse), and
head show only X results.
In the above example since
head has no parameters, it shows only the first 10 results as it is its default, but of course you can change the number of displayed folders by using the minus sign “-” followed by the desired number. For example:
du -hs */ | sort -hr | head -15
shows the 15 most heavy folders on the folder you are in.
Once you have found an especially heavy folder, you can use the following command to show the 10 largest files in it:
ls -lS | head
The use of the
head command is the same as seen above while
ls -lS simply list the files with the space occupied by each file.
Of course the above last presumes the large files are directly in the folder found above, but in many you may find just nested folders. In this case you can use the combination of the two commands to narrow down the folder with the large files.
Find large files on Linux with Find
Alternatively there the following command to find large files on Linux recursively in folders and subfolders:
find -type f | sort -k 7 -r -n | head
find will search from the list of subdirectories only the files and not folders (-type f),
sort will rearrange them in numerical order (n) based on the space occupied ( -k 7) in descending order (-r), and
head, as previously seen, will only show the first 10 results.
Not only this last command will give you directly the files, but if you notice closely you will find that it will expose files contained inside hidden folders (the ones that start with a .); something that the first command was not doing.