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.
-h
- The
-h
option 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)
-h --max-depth=1
-h
makes the sizes human-readable.--max-depth=1
ensures it only shows sizes for the current directory and its direct subdirectories.
Example 2: Show the largest directories sorted by size
-hs * | -rh | -5
-hs
displays the total size for each directory.sort -rh
sorts them in reverse order (largest first).head -5
shows only the top 5 largest directories.
Example 3: Find the largest files in the current directory
-Sh | -rh | -5
-Sh
lists file sizes, excluding subdirectories.sort -rh
sorts 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 - f - -Sh {} + | -rh | -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/ - f - -Sh {} + | -rh | -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/ - f - | -rn | -n 5
%s
prints the file size, and%p
prints 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
-Th
- The
-T
option adds filesystem type information, while-h
keeps the output readable.
Finding Large Files in Specific Directories
To search for files larger than 1 GB in the /var
directory:
find /var - f -size +1G
-type f
limits the search to files.-size +1G
looks for files larger than 1 GB.
Checking Inode Usage
Inodes are system structures used to store file metadata. To check inode usage:
-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:
-sh /var
- This gives you a quick summary of the total space used by the
/var
directory.