Managing disk space is a crucial task for system administrators and Linux users, especially when handling large amounts of data. Knowing how to check disk usage and file sizes helps maintain system performance and prevent storage issues. In this guide, we will explore various Linux commands that allow you to check disk space, analyze directory sizes, and find large files quickly.
Checking Disk Space Usage with df
The df command provides an overview of the disk usage of your file systems.
df -h
- The
-hoption makes the output human-readable, displaying sizes in KB, MB, or GB.
This will give you an output similar to this:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 30G 40% /
Analyzing Disk Usage with du
The du command is one of the most powerful tools for analyzing directory sizes and specific files.
Example 1: Display the size of directories (up to one level depth)
du -h --max-depth=1
-hmakes the sizes human-readable.--max-depth=1ensures it only shows sizes for the current directory and its direct subdirectories.
Example 2: Show the largest directories sorted by size
du -hs * | sort -rh | head -5
-hsdisplays the total size for each directory.sort -rhsorts them in reverse order (largest first).head -5shows only the top 5 largest directories.
Example 3: Find the largest files in the current directory
du -Sh | sort -rh | head -5
-Shlists file sizes, excluding subdirectories.sort -rhsorts them in reverse order, showing the largest files first.
Finding the Largest Files Using find and du
Sometimes, you need to locate the largest files within a directory or system. The find command helps you search for files, while du assists in determining their sizes.
Example 1: Find and list the top 5 largest files
find -type f -exec du -Sh {} + | sort -rh | head -n 5
- This command searches for all files, then lists them by size, displaying the top 5 largest files.
Example 2: Find the largest files in a specific directory (e.g., /home/downloads/)
find /home/downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5
- The same approach, but limited to the
/home/downloads/directory.
Alternatively, you can use the -printf option to format the output:
find /home/downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5
%sprints the file size, and%pprints the file name.
Additional Commands for Disk Space Managemen
Checking Available Disk Space per File System
To see how much space is left on each
df -Th
- The
-Toption adds filesystem type information, while-hkeeps the output readable.
Finding Large Files in Specific Directories
To search for files larger than 1 GB in the /var directory:
find /var -type f -size +1G
-type flimits the search to files.-size +1Glooks for files larger than 1 GB.
Checking Inode Usage
Inodes are system structures used to store file metadata. To check inode usage:
df -i
- This will show the number of used and available inodes on each filesystem.
Display Disk Usage of a Specific Directory
To check the total disk usage of the /var directory:
du -sh /var- This gives you a quick summary of the total space used by the
/vardirectory.