Identifying large files on your PC can help you quickly reclaim some much needed space. For desktop Linux users, hunting down large unnecessary files might be optional. But when it comes to server space, it costs money, and you have to pay for that excess space every month. Here’s how you can locate big files in Linux to quickly get rid of them.
Also read: How to Use Chroot in Linux and Fix Your Broken System
Use the ls Command
Generally, the ls
command is used to list all of the directories and files in the Linux terminal. However, it can do much more – for instance, classify directory contents and display file sizes.
- To see more information about files and directories including their permissions, you can use the
-l
flag.
ls -l
- To print their size along with all of the information, use the
-s
flag along with the previous-l
flag.
ls -ls
- By default, the
ls
command will only list the directories. To see the files inside the directories recursively, use the-R
flag.
ls -lsR
- To sort files, you can use two methods. One takes advantage of the
-S
flag inside thels
command, while the other employs the power of thesort
command. To sort the files according to their file size in descending order, run the following command:
ls -lsRS
- To reverse the order, you can use the
-r
flag.
ls -lsRSr
- Alternatively, after the
ls
command returns, you can pipe the result into thesort
command. This will sort the list as per their numerical file sizes in ascending order. You can also reverse the order by using-r
flag in conjunction with thesort
command.
ls -ls | sort -nr
- So far, we have analyzed and found the largest files inside our current working directory. To identify the largest file in your whole filesystem, you can add the location after the
ls
command.
sudo ls / -S
Also read: What Is XDP (Express Data Path) in Linux
Use the find Command
The find
command can be used to search any files inside a Linux filesystem. In this case, we can employ it to list files according to their file sizes. We can also filter the results by minimum file size.
- To find the largest files inside your current working directory, type the following:
find . -type f
- It’s possible to also add a filter with the minimum size of 100MB.
find . -type f -size +100M
- To specify a certain directory, add the relative path after the
find
command.
find ./test -type f -size +100M
- Doing so will find all the files inside the “test” directory which has a size greater than 100MB.
- Sometimes you need to search the entire filesystem to find the largest file. For that, just add a
/
after the find command.
sudo find / -xdev -type f -size +100M
Note: the -xdev
flag instructs not to descend directories on other filesystems. In simple words, it won’t scan other mounted filesystems in your Linux system.
- To remove this behavior and to scan all the mounted drives along with your primary filesystem, just remove the
-xdev
command as follows:
sudo find / -type f -size +100M
Now you should be able to view the list of the largest files in your entire device along with your mounted drives.
Also read: How to Install Safari on Linux
Use the du Command
The du
command is primarily designed to estimate file sizes in Linux. Here’s how to find large files with it.
- You can use the
-a
flag to list the files along with directory sizes recursively.
du -a
- The file sizes listed here appear as very long number strings, so they are very hard to estimate. To make it readable, use the
-h
flag in conjunction with other flags.
du -ah
- To make the file size uniform, use the blocksize operator
-B
along with a unit of your choice. If you want to convert the sizes in MB, use unitM
as a block size.
du -aBM
- To find the largest files among them, use the
sort
command to sort the files in descending order.
du -aBM | sort -nr
- To list only the first five results, pipe the sorted list into the head command by using
-n 5
.
du -aBM | sort -nr | head -n 5
- So far we’ve been displaying file sizes only in our current working directory. To list the largest files of a specific directory, append the directory name after the
du
command. The following command will list the five largest files in your home directory.
du /home -aBM | sort -nr | head -n 5
- To find the largest 10 files in your entire filesystem, just add the
/
after thedu
command.
du / -aBM | sort -nr | head -n 10
Also read: How to Use Rm Command in Linux
Find Large Unused Files
Getting the list of unused files is very useful, as you can readily delete them to save space on your hard disk. Do this with the help of the -mtime
flag and the find
command discussed earlier. The following instructions will list out the top 10 files, which have not been modified for more than 30 days and have a file size of at least 100MB.
find / -xdev -mtime +30 -type f -size +100M
GUI Apps to Find the Largest Files in Linux
If you are running Linux on your desktop, make use of these GUI apps to find the largest files in your system.
Disk Usage Analyzer
One of the best GUI apps to analyze file sizes in Linux is Disk Usage Analyzer. It comes preinstalled in your Gnome desktop environment.
- If this application is not installed on your machine, you can easily install the disk usage analyzer application like so:
sudo apt update sudo apt install baobab
For Fedora or other red-hat-based distributions, install Disk Usage Analyzer by using the following command:
sudo dnf install baobab
- Open the app and select the filesystem you want to scan. Disk Usage Analyzer will list the attached filesystem in your machine.
- You’ll have to wait a few seconds until the scan completes.
- Once the process is done, you’ll be able to see the list of files and folders sorted according to their sizes (larger ones at the top, smaller towards the bottom).
- You can navigate through the folders using double clicks.
Some additional GUI apps that you can use to find large files on your Linux system include:
Remove the Largest Files
After finding the largest files, you need to remove them and can easily do so with the help of therm
command in Linux.
- Copy the absolute or relative file of the file you want to delete, then append the file path after the
rm
command.
rm "your file path goes here"
- To remove any non-empty directory, use the
-rf
after therm
command.-r
represents recursively deleting the files inside the directory, and-f
implies force to delete that directory. For example, if you want to delete your “Downloads” folder along with all the files inside it, run the following command.
rm -rf ~/Downloads
Also read: How to Resize and Optimize Images From the Linux Terminal
Frequently Asked Questions
Why I am getting a "permission denied" error?
If you want to run a command beyond your home directory, you should have root permission. Otherwise, you will get the permission denied error. Try using sudo
before the command to elevate your permission as a root user. You can successfully run any command on your system files. Try to be careful when using sudo
with commands.
How do I find files larger than 1GB?
To find files larger than 1GB, use the find command with its -size
flag. The command will look like this: sudo find / -xdev -type f -size +1G
.
How can I view the size of a particular folder?
To see the size of a folder, use any of the tools mentioned in this article. You can also use your File Explorer to see the size of a folder. Just right-click on the folder and select the properties option. There you can easily find the listed folder size.
Image source: Unsplash All screenshots Hrishikesh Pathak
Our latest tutorials delivered straight to your inbox