For Linux administrators, there are times when we need to identify the files that are consuming the most space on our Linux server, leading to low free space. This is crucial as low disk space can impact server performance and may cause critical processes, such as backup jobs, to fail. In this comprehensive guide, we will explore how you can identify large file sizes on Linux CentOS and RHEL.
For a deeper understanding of Linux servers, you can visit our pages on best web servers, Apache, Nginx, and LiteSpeed. If you’re interested in different types of hosting, we also have articles on dedicated server, VPS server, cloud hosting, and shared hosting.
Finding Large File Sizes on Linux
Displaying the Largest Directories in /var/log
To display the largest directories in /var/log in a human-readable format, you can use the following commands:
# cd /var/log # du -sh * | sort -rh | head -10
This will display the top 10 directories in terms of size.
# cd /var/log # du -sh * | sort -rh | head -10 78M nginx 14M audit 1012K dmesg.old 980K messages-20170109 700K messages-20160907 556K anaconda 288K messages-20161212 128K wtmp 120K httpd 96K messages-20160929
Displaying the Largest Folders Including Sub-directories
To display the largest folders including their sub-directories for /var/log, use the following commands:
# du -Sh /var/log | sort -rh | head -10
This will display the top 10 folders, including sub-directories, sorted by size.
# du -Sh /var/log | sort -rh | head -10 78M /var/log/nginx 14M /var/log/audit 3.6M /var/log 556K /var/log/anaconda 120K /var/log/httpd 68K /var/log/tuned 56K /var/log/mariadb 28K /var/log/php-fpm 4.0K /var/log/varnish 4.0K /var/log/ppp
or
# du -Sh /var/log | sort -n -r | head -n 10 556K /var/log/anaconda 120K /var/log/httpd 78M /var/log/nginx 68K /var/log/tuned 56K /var/log/mariadb 28K /var/log/php-fpm 14M /var/log/audit 4.0K /var/log/varnish 4.0K /var/log/ppp 4.0K /var/log/dirsrv
Displaying the Largest Files Including Sub-directories
To display the largest files including their sub-directories for /var/log, use the following commands:
# du -ah /var/log | sort -rh | head -10
This will display the top 10 files, including those within sub-directories, sorted by size.
# du -ah /var/log | sort -rh | head -10 96M /var/log 78M /var/log/nginx/ehowstuff.local.error.log-20160907 78M /var/log/nginx 14M /var/log/audit 6.1M /var/log/audit/audit.log.2 6.1M /var/log/audit/audit.log.1 1.8M /var/log/audit/audit.log 1012K /var/log/dmesg.old 980K /var/log/messages-20170109 700K /var/log/messages-20160907
Finding the Largest Files in a Specific Location
To find the largest files in a specific location or directory, for example /var/log, use the following commands:
# find /var/log -type f -exec du -Sh {} + | sort -rh | head -n 10
This will find and display the top 10 largest files in the specified location.
# find /var/log -type f -exec du -Sh {} + | sort -rh | head -n 10 78M /var/log/nginx/ehowstuff.local.error.log-20160907 6.1M /var/log/audit/audit.log.2 6.1M /var/log/audit/audit.log.1 1.8M /var/log/audit/audit.log 1012K /var/log/dmesg.old 980K /var/log/messages-20170109 700K /var/log/messages-20160907 288K /var/log/messages-20161212 184K /var/log/anaconda/anaconda.storage.log 152K /var/log/nginx/ehowstuff.local.access.log-20160907.gz
Finding the Largest Files (Top 10) in a Specific Location
To find the largest files (top 10) in a specific location or directory, for example /var/log, use the following commands:
# find /var/log -printf '%s %p\\n'|sort -nr|head
This will find and display the top 10 largest files in the specified location.
# find /var/log -printf '%s %p\n'|sort -nr|head 81440064 /var/log/nginx/ehowstuff.local.error.log-20160907 6291477 /var/log/audit/audit.log.2 6291467 /var/log/audit/audit.log.1 1823505 /var/log/audit/audit.log 1052691 /var/log/dmesg 1001793 /var/log/messages-20170109 709481 /var/log/messages-20160907 293738 /var/log/messages-20161212 292000 /var/log/lastlog 187757 /var/log/anaconda/anaconda.storage.log
Displaying the Largest Files (Top 20) Using ls Command
To display thelargest files (top 20) in a specific location or directory, for example /var/log, using the ls command, use the following commands:
# ls -lSh | head -n20
This will display the top 20 largest files in the specified location.
# ls -lSh | head -n20 total 3.7M -rw-r--r-- 1 root root 1.1M Jan 9 21:52 dmesg -rw------- 1 root root 979K Jan 9 15:12 messages-20170109 -rw------- 1 root root 693K Sep 7 03:21 messages-20160907 -rw------- 1 root root 287K Dec 12 10:01 messages-20161212 -rw-r--r--. 1 root root 286K Jan 9 22:03 lastlog -rw-rw-r--. 1 root utmp 125K Jan 9 22:03 wtmp -rw------- 1 root root 95K Sep 29 21:01 messages-20160929 -rw-r--r-- 1 root root 94K Jan 9 14:57 dmesg.old -rw------- 1 root root 81K Jan 9 22:07 messages -rw------- 1 root root 55K Sep 6 23:16 secure-20160907 -rw-r--r-- 1 root root 36K Sep 7 03:21 cron-20160907 -rw-------. 1 root root 26K Jan 4 22:12 yum.log-20170109 -rw-r--r-- 1 root root 23K Jan 9 21:53 vmware-vmsvc.log -rw-------. 1 root root 17K Dec 30 2015 yum.log-20160101 -rw------- 1 root root 9.3K Jan 9 14:59 secure-20170109 -rw-r--r-- 1 root root 9.2K Jan 9 21:52 boot.log -rw-------. 1 root root 8.0K Dec 20 22:39 grubby -rw------- 1 root root 7.3K Dec 12 10:17 maillog-20161212 -rw-r--r-- 1 root root 5.4K Jan 9 15:19 cron-20170109
Commands Mentioned
- cd /var/log – Changes the current directory to /var/log
- du -sh * – Estimates file and directory space usage
- sort -rh – Sorts the results in reverse order
- head -10 – Outputs the first 10 lines
- du -Sh /var/log – Estimates space usage including subdirectories
- du -ah /var/log – Estimates space usage for all files
- find /var/log -type f -exec du -Sh {} + – Finds files in a specific location
- find /var/log -printf ‘%s %p\\n’ – Finds files and prints their size and name
- ls -lSh – Lists files with their size in human-readable format
Conclusion
Understanding how to manage disk space is a vital skill for any Linux administrator. By learning how to identify large files and directories, you can ensure that your server maintains optimal performance and prevent critical processes from failing due to low disk space. This guide has provided you with several commands and techniques to find large files on Linux CentOS and RHEL.
Remember, it’s not just about finding large files; it’s about understanding your server’s file system and how it’s being used. By regularly checking and managing your disk usage, you can prevent potential issues and ensure your server runs smoothly. For more insights and guides on managing servers, check out our other articles on best web servers, Apache, Nginx, and LiteSpeed.
FAQ
-
What does the ‘du’ command do in Linux?
The ‘du’ command, short for ‘disk usage’, is used in Linux to estimate file and directory space usage. It can be used with various options to display the sizes of files and directories in different formats and orders.
-
What is the purpose of the ‘sort’ command in Linux?
The ‘sort’ command in Linux is used to sort the lines of text in a file. It can be used with various options to sort in reverse order (-r), sort numerically (-n), or sort by human-readable sizes (-h), among others.
-
What does the ‘head’ command do in Linux?
The ‘head’ command in Linux is used to output the first part of files. By default, it outputs the first 10 lines of each file to standard output. With more than one file, it precedes each set of output with a header identifying the file name.
-
What is the purpose of the ‘find’ command in Linux?
The ‘find’ command in Linux is a powerful tool that allows you to search for files and directories in a directory hierarchy based on a user-given expression and performs action on each matched file. It can search for files by name, owner, group, type, permissions, date, and other criteria.
-
What does the ‘ls’ command do in Linux?
The ‘ls’ command in Linux is used to list the contents of a directory. It can be used with various options to display the list in different formats and orders. For instance, ‘ls -lSh’ lists files with their size in a human-readable format, sorted by size.