top of page

How to Find Large Files and Directories with Size in Linux?

One of the common problems while working in Linux is to find large files to free some space. Suppose, your file system is full and you are receiving an alert to remove spaces or if your host is run out of space and your server is not starting up, the first thing you do is find the top 10 largest files and see if you can delete them. Usually, old files, large Java heap dumps are good candidates for removal and freeing up some space. If you are running Java applications like core Java-based programs or web applications running on Tomcat then you can remove those heap dump files and free some space, but the big question is how do you find those? How do you know the size of the biggest file in your file system, especially if you don't know which directory it is? We'll try to find answers to some of those questions in this article. The find command which let you search sub-directories for large files as shown below:

$ find . -size +1G

This command will print all the files which are greater than 1GB from the current directory and any subdirectory. The only problem with this one is that it doesn't print the exact size. The problem was solved by using the -printf option, which allows you to specify a format String much like Java's printf() method.

How to find large files with their size in Linux


You can use the find command and du command to find out all the large files and directories which are hogging disk space. If you are file system is 100 % full or close to 100% then you will need to find these big files and directories so that you can delete them if not needed. Generally, old log files and core dump files are good candidates to free disk space.

1. Finding Big files using the find command in Linux

You can further tweak the command to find files up-to a certain size like the below command will find all files. Here is the modified UNIX command to find large files with size :

$ find . -size +1G -printf '%s %p\n'

here is %s is for size and %p are for the path. Alternatively, You can also use -exec option to run ls on each file the find command return to print its size as shown below:

$ find . -size +100M -exec ls -sh {} \;

This is good enough, you can just see which files you can delete and free some space, but the problem is that you will not find any file which is larger than 1GB, hence I always use this command with some hypothetical large number like 10GB, etc, but, those are just workaround, not the proper fix. Let's see what we can do next.


2. Finding large file using the du command in Linux

Btw, you can also use the du (disk usage) command to find large directories and their size, as shown below :

$ du -a . | sort -n -r | head -n 10
16095096 . 
13785288 ./logs 
6095380 ./logs/app 
2125252 ./temp 
2125244 ./temp/data 
2125240 ./temp/data/app

This is the right command, it will list both directories and files. I have also combined the output of the du command with the sort command to print the top 10 largest files and directories. That's all about how to find the large files and directories in Linux. As I said, earlier I used to search large files by using the find command with -size option but that is more or less guesswork because you never know the size of the largest file in a machine, but by using a reasonable high size, you can possibly find all big files in your filesystem


Source: Java67


The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page