Home » Linux » How to Find Biggest Files and Directories in Linux to Free some Disk Space Quickly – find & du & sort & head command Example

About Javin Paul

Javin Paul

How to Find Biggest Files and Directories in Linux to Free some Disk Space Quickly – find & du & sort & head command Example

Hello guys, 100% full disk space is a common problem and as a programmer or Linux user, you often need to free disk space by deleting biggest files and directories. In order to do that, you need to know some Linux find command examples to list and delete some biggest files and directories and that’s what you will learn in this tutorial. As a
Java developer, you also need to also support your application, most of which mostly run on Linux machines. Though production is monitored by the dedicated support teams and they receive regular alerts on the status of file system space, they often come running to you or the developer responsible about what to do when disk space is almost full or reaching 100%.

This is even more common on UAT and QA/TEST environment where no one really monitors and then people start coming screaming to you that things are not working, Java processes are not up and systems are not able to connect to each other, only to find that there is no space left in the machine.

In order to fix those problems you need to free up some space and when you are running out of both disk space and time, you need to concentrate on the biggest files and folders on your disk or partition, so that you can get space quickly by deleting just a couple of files and directories.

The best way is to list the top 10 directories by size and then go inside some of them, and find files you can delete to free space some space e.g. old log files, cache files, and heap dumps.

My preferred approach is to go into the root directory of your application and then run the find or du command to find largest directories recursively. From there, I can go further and delete which are occupying most of the space.

Since you may come here while searching for a quick Linux command to free some disk space, I am first listing commands you can use, I’ll explain it in the next paragraph.

Do hang on if you have some time to understand how this Linux find command works but if you are in hurry, here is what you need:

Linux command to find biggest top 10 directories in your disk

$ du -hs */ | sort -nr | head

This will give you the biggest directories in the disk, then you can go inside one of them, preferably the biggest ones and find files to delete using ls command.

Linux command to Find the biggest file inside a directory

$ ls -lS | head

That’s it.  You now have the biggest files in that folder. If it is something you can delete, just delete. If you need all files just remove the head command.

Find biggest files in any directory recursively

We’ll now use find command, to search only files, and then sort to find out top 10 or top 5 biggest files in all directories from the current directory. This will print the full path of the file which you can just copy and delete to free disk space.

$ find -type f -ls | sort -k 7 -r -n | head -5

find command only list files and not directories, hence you can sort them using the column 7 (the column with the file size).

We are sorting using sort -n option for numeric order and -r reverse order (from biggest to smallest i.e. descending order), and finally only the first 5 files in the current directory and sub-directory.

If you want to find the top 5 biggest files in your machine from the root partition then you can use it like this:

$ find / -type f -ls | sort -k 7 -r -n | head -5

If you want to list the top 10 biggest files from the current directory then you can use it like this:


$ find . -type f -ls | sort -k 7 -r -n | head -10

Commands Used and Explanation

There is not a single command in Linux to help us with this task, but we will use a combination of find, du, sort, and head command to recursively find and delete largest files and directories fast.

If you don’t know the du command stands for disk usage and print size of each file and recursively for directories. This is good if your file system has a lot of directories and sub-directories.

On the other hand, sort command is used to sort the output of du command and print into the console. If you are interested in the biggest file then you need to sort in descending order and you also need to do numeric sort instead of lexicographic sort. For that, you need to use the sort -nr command, where -n is for numeric sort and r is for reverse sort i.e. sorting in descending order.

Now, we are not interested in all the files as there could be hundreds of files and that’s why we are using the head command, which can print top 10 or top 5 files depending upon your choice. For example head 10 will print 10 lines. For example head -n 10 will print the first 10 lines.

If you have already sorted your output in decreasing order then you can use head -n 10 to print 10 largest directories or files.

Now, the find command which can help you to search file based upon size in all the directories recursively. Just issue the find command with -size parameter and start it from current directory if you are already at the start of your application directory or partition.

It will then go on and find all the files which are larger than the size you have specified.

Also, here is a nice picture to remember useful find command options from Julia Evans:

full disk space

That’s all about how to find the biggest files and directories on any partition and free up some disk space. These Linux find commands are very handy and I always write them up in my notes for quick reference, I know it’s difficult to write Linux command by yourself sometimes and we often prefer a tried and tested command.

Though you should be careful while copy pasting commands on Linux because if you copy a new line which is very much possible the command will start running and you may accidentally block or delete something. In short, never run a Linux command by copy-pasting in a production machine.

Related UNIX Command Tutorials

  • 5 Free Courses to Learn Linux for Programmers (courses)
  • 10 examples of date command in Linux (examples)
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • 10 examples of the XARGS command in Linux (examples)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 5 examples of sort command in Linux (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 examples of chmod command in UNIX (examples)
  • 10 examples of lsof command in Linux (examples)
  • 10 examples of curl command in Linux (examples)
  • 10 examples of cut command in Linux (examples)
  • 10 Books every Linux Power user should read (books)
  • 5 Courses to Learn Shell Scripting in Linux (courses)


Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

Published on System Code Geeks with permission by Javin Paul, partner at our SCG program. See the original article here: How to Find Biggest Files and Directories in Linux to Free some Disk Space Quickly – find + du + sort + head command Example

Opinions expressed by System Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Phani Raj Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Phani Raj
Guest
Phani Raj

Thanks for the commands.
I would suggest to use “sort -hr” instead of “sort -nr” to sort the results properly with human readable numbers like ’25M or 5G’