Find Largest Files and Directories in Linux
To find the largest files and directories in Linux, use the du command with sort and head, or the find command with -type f and -printf. This article explains the exact commands, how they work, and how to interpret their output.
Why You Need This
Disk space fills up faster than expected, especially on servers or long-running desktops. Log files, caches, virtual machine images, and database backups can silently consume gigabytes. Knowing which files and directories are largest helps you clean up safely and avoid downtime from a full disk. The commands below work on any Linux distribution with the standard GNU coreutils installed.
The Core Commands
Two commands form the backbone of disk usage analysis: du (disk usage) and find. du scans directories and reports the total size of each subdirectory, while find can locate individual files by size. Combine them with sort and head to get a ranked list.
Largest Directories
To see the top 10 largest directories in the current folder, run:
du -h --max-depth=1 . | sort -hr | head -10
Breakdown of the flags:
-hprints sizes in human-readable format (K, M, G).--max-depth=1limits the scan to immediate subdirectories, not everything inside them recursively.sort -hrsorts numerically by human-readable size, descending.head -10keeps only the top 10 lines.
For the entire filesystem, start from the root and increase the depth if needed:
sudo du -h --max-depth=1 / | sort -hr | head -20
Using sudo ensures you can read all directories, including those owned by other users. Without it, you may get “Permission denied� errors and miss large files.
Largest Files
To find the 10 largest files in the current directory and its subdirectories:
find . -type f -printf '%s %p\n' | sort -nr | head -10
Here:
-type frestricts to regular files.-printf '%s %p\n'prints the size in bytes followed by the path.sort -nrsorts numerically in reverse order.head -10takes the first 10 lines.
To get human-readable sizes, replace %s with %k (kilobytes) and add -h to sort:
find . -type f -printf '%k KB %p\n' | sort -hr | head -10
Understanding du vs. find Output
du reports the actual disk space used by a directory, including all files and subdirectories. It counts blocks allocated, which may differ from the file size if the filesystem uses compression or sparse files. find with -printf reports the logical file size, which is what ls -l shows. For most purposes the difference is negligible, but for compressed or sparse files the two numbers can diverge significantly.
Common Pitfalls and How to Avoid Them
- Permission errors: Run
sudowhen scanning system directories like/varor/hometo avoid incomplete results. - Sorting without
-h: If you usedu -hbut sort with plainsort -r, you get alphabetical order, not numeric. Always usesort -hrwith human-readable sizes. - Forgetting
--max-depth: Without it,duprints every subdirectory recursively, making the output huge and slow. - Scanning the whole disk repeatedly: For large filesystems, the scan can take minutes. Use
--max-depth=1first to identify the biggest branches, then drill down. - Hard links:
ducounts hard-linked files only once, whilefindlists each link separately. This can cause a file to appear twice in the file list.
Advanced Techniques
For a more interactive view, use ncdu (NCurses Disk Usage). It provides a text-based interface that lets you navigate directories and see sizes in real time. Install it with your package manager (e.g., apt install ncdu or dnf install ncdu), then run ncdu /.
To exclude certain directories (like /proc or /sys) from a system-wide scan, use --exclude:
sudo du -h --max-depth=1 / --exclude=/proc --exclude=/sys | sort -hr | head -20
To find files larger than a specific size, use find with the -size option:
find / -type f -size +1G -exec ls -lh {} \;
This lists all files larger than 1 GB. The -exec ls -lh shows the size in a readable format.
Interpreting the Results
When you get the list, focus on the top few items. For directories, check if the size is dominated by a single subdirectory or file. For files, consider whether the file is still needed. Common large files include:
- Log files in
/var/log(e.g.,syslog,kern.log) - Package manager caches (
/var/cache/apton Debian/Ubuntu) - Database files (
*.sqlite,*.ibd) - Virtual machine disk images (
*.qcow2,*.vdi) - Old backups (
*.tar.gz,*.zip)
Before deleting anything, verify the file’s purpose with ls -lh and check if it’s in use by a process (lsof). Deleting a file that is still open by a running process can cause the process to crash or behave unpredictably.
Frequently Asked Questions
Why does du show a different size than ls -l?
du reports disk usage, which counts allocated blocks. ls -l shows the logical file size. For most files they match, but sparse files (with holes) or files on compressed filesystems can use fewer blocks than their logical size.
How do I find the largest files in a specific directory?
Replace the starting point in the find command. For example, to search /home/user, use find /home/user -type f -printf '%s %p\n' | sort -nr | head -10.
What is the fastest way to scan a huge disk?
Use du --max-depth=1 to get an overview first, then drill down only into the largest subdirectories. Avoid scanning the entire disk repeatedly. ncdu is also efficient because it reads the directory tree once.
Can I find files by size without find?
Yes, you can use ls -lS to list files in the current directory sorted by size, but it does not recurse into subdirectories. For recursive searches, find is the standard tool.
Why do I get “Permission denied� when running du?
You lack read permission on certain directories. Use sudo to run the command with root privileges, which can read all files and directories.
How do I exclude a directory from the scan?
Use the --exclude option with du, or the -prune option with find. For example, find / -path /proc -prune -o -type f -printf '%s %p\n' skips /proc.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.