Hello! Today, we’re going to learn how to solve the ‘disk space shortage’ problem that anyone operating a Linux server will encounter at some point. A terrifying situation where the service suddenly stops or you can’t connect to the server! π¨ Most of the time, this problem occurs because the disk space is full.
In such cases, as a temporary measure, finding and deleting the largest files can help you put out the urgent fire. I’ll explain the method step-by-step now!

Step 1: Check Filesystem Usage π
First, you need to identify which disk is causing the problem. The `df` command allows you to see the overall filesystem usage of your current server at a glance.
- Connect to the server where the problem occurred. (Example: node01)
ssh node01
- Use the `-h` option (human-readable) to check usage in units that are easy for humans to read (GB, MB).[Example Output]Look closely at the results. The Use% column exceeding 90% is our target. In the example above, the root (`/`) filesystem usage is 98%.
- β οΈ Important! The path to be used in subsequent steps is the one in the ‘Mounted on’ column. In the example above, it would be `/`. Be sure to remember the path that matches your environment. For convenience, in this article, I will refer to it as `/path/to/mountpoint`.
`
df -h
`
Step 2: Find the Largest File (Don’t Delete Yet!) π
Now it’s time to find out which file is occupying the most space in the full filesystem (`/` or `/data`, etc.).
Deleting first is dangerous! π¨ It’s important to get into the habit of checking what kind of file it is first. The command below only shows the size and path of the largest file.
find /path/to/mountpoint -type f -printf '%s %p
' | sort -nr | head -n 1
The command looks a bit long and complicated, doesn’t it? Let me break it down for you.
- find /path/to/mountpoint -type f -printf ‘%s %p
‘:
- find: Searches for files in a specific path.
- -type f: Finds only ‘files’, not directories.
- -printf ‘%s %p
‘: Prints the information of the found file in a specific format. %s means file size, %p means file path. That is, it outputs in the format [file size] [file path].
- | (pipe): A connector that passes the output of the left command as input to the right command.
- sort -nr:
- sort: Sorts the received content.
- -n: Sorts numerically.
- -r: Reverse order, i.e., descending. The largest number will be at the top.
- head -n 1: Shows only the first line of the received content.
As a result, this command means: “Find the single largest file in the specified path and show its size along with its path!”
Step 3: Confirm and Delete the Largest File ποΈ
If you are confident that the file found in Step 2 is a safe file to delete, such as an unnecessary log file or backup file, then proceed with deletion.
The command below adds the deletion logic to the Step 2 command.
find /path/to/mountpoint -type f -printf '%s %p
' | sort -nr | head -n 1 | awk '{print $2}' | xargs rm
Let’s look at the newly added parts:
- awk ‘{print $2}’:
- awk: A powerful text processing tool.
- ‘{print $2}’: Means to print only the second column from the input. It extracts only the file path from input in the format [file size] [file path].
- xargs rm:
- xargs: Passes the result from the previous command (file path) as an argument to the `rm` command.
- rm: The command to delete files.
In other words, the process of finding the path of the largest file and deleting it with the `rm` command is automatically executed.
π₯ Caution! π₯ This command permanently deletes files without confirmation. Deleting files essential for system operation may prevent the server from booting. Always double-check what kind of file it is and if it’s safe to delete before proceeding!
Step 4: Confirm Space Has Been Freed β
If you’ve successfully deleted the file, you need to confirm that disk space has actually been freed, right? Let’s run the `df -h` command again, which we used in Step 1.
df -h /path/to/mountpoint
This time, instead of looking at the entire filesystem, it’s more convenient to check only the specific path we worked on.
[Example Output]
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 42G 7.5G 85% /
You can see that Use% has significantly dropped from 98% to 85%. Success! π The server has now regained space and can operate normally again.
Today, we learned step-by-step how to find and delete the largest files when a Linux server’s disk space is full. This method is very useful in an emergency, but it may not be a fundamental solution. It’s more important to establish long-term plans, such as regularly cleaning up unnecessary logs or increasing disk capacity! π
Leave a Reply