Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Sunday, May 7, 2023

How to find big directories and files to free space in Linux? 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 environments 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 the largest directories recursively. From there, I can go further and delete which are occupying most of the space.

How to find large files and directories to free some disk 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 a hurry, here is what you need:

Linux command to find the biggest top 10 directories in your disk

Here is the command I used on daily basis to find the large directory on any host when the disk space ran out or reaching to 100% fill
$ 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 the ls command.

Linux command to Find the biggest file inside a directory

Here is another command which you can use to list the large files in any directory on your Linux host:

$ ls -lS | head

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

Btw, if you are not familiar with fundamental commands like head, less, find, and ls, I suggest you first go through these free Linux courses, instead of hopping through blog posts. That will give you all the information you need to better understand an article like this and solve your day-to-day Linux problems.

How to find biggest files in any directory recursively in Linux

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

Btw, If you have trouble understanding the above command even after my explanation like you don't understand pipe to combine two Linux commands or feeding the output of one command to other then I suggest you go through Linux Command Line Interface (CLI) Fundamentals course on Pluralsight to learn fundamentals first.

How find, du, sort, and head command works together?

There is not a single command in Linux to help us with this task, but we will use a combination of the find, du, sort, and head commands to recursively find and delete the 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, the sort command is used to sort the output of the du command and print it 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.

As I have said before, if you are not familiar with these commands I suggest you first go through the Linux Command Line Basics course on Udemy, which you can get in just $10 on their several flash sales they run every month or so.

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 the 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 the 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.

How to find big directories and files to free space in Linux? find + du + sort + head command Example

That's all about how to find the biggest files and directories on any host, partition and free up disk space in Linux. 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 it with your friends and colleagues. If you have any questions or feedback then please drop a note.

No comments :

Post a Comment