Identifying and resolving disk space issues in a Linux system involves several steps. We will cover some of the mode needed and important methods to check and clean the linux disk space.
Step1: Find the dir/file where the disk space in too high.
i) Use the df
command to display disk space usage:
$ df -h
This command shows the disk space usage for all mounted file systems in human-readable format. We can identify the file system with higher disk usage.
ii) The command below is used to find the file size of the root directory. It displays all the file sizes in the root directory, providing insight into areas where the data is excessive.
$ du -sch /*NB: from the command "df -h" we will get the file system with high disk usage.
Use that file system path in this command. if the file system is "/dev":
$ du -sch /dev/*
Below command will show the largest files/directory.
$ du -Sh | sort -rh | head -5
du command: Measures file space usage.
-h : Print sizes in human-readable format.
-S : Do not include the size of subdirectories.
-s : Display only a total for each argument.
sort command : sort lines of text files.
-r : Reverse the result of comparisons.
-h : Compare human readable numbers
head : Output the first part of the files.
iii) To find largest file in the directory:
#shows the 20 biggest files in the /dir/ path
$ du -a /dir/ | sort -n -r | head -n 20
🐧We can also use the find module to find the largest file in linux system.
find /path/dir/ -type f -printf '%s %p\n' | sort -nr | head -20
🐧Using the command below, we can check the file size if we already know the directory where files are taking up too much space.
$ ls -sh
{OR}
$ ls -lh --> Human readable format
NB:Sometimes, disk space issues are related to a large number of small files. Use the df -i
command to check inode usage:
df -i
Step 2: Delete all unnecessary files
Once we identify the files consuming excessive disk space, we need to delete all unnecessary files.
#Delete this particular file.
$ rm -rf filename.txt#Delete multiple files in a single command
$ rm -rf file2.txt work.txt code.txt
#Delete all the txt files in that directory.
$ rm -rf *.txt
#Delete all files starting with name "file".
$ rm -rf file*
#Delete the directory "dev" along with all the files.
$ rm -rf /dev
#Delete all the files in the directory
$ rm -rf /dev/*
# Delete all files older than 7 days
$ find /path/to/search -type f -mtime +7 -exec rm {} \;
Preventive Measures:
- Regularly Monitor Disk Space: Set up regular monitoring to alert you when disk space usage is reaching critical levels.
- Implement Log Rotation: Configure log rotation to limit the size of log files and prevent them from consuming excessive disk space.
- Consider Disk Expansion: If your system frequently runs out of disk space, you may need to consider expanding the storage capacity by adding a new disk or resizing existing partitions.
🐧We can use the man command to get more information about the commands that we can used.
$ man du
$ man sort
$ man head
$ man find
Happy learning 🚀🤖🏁