Running out of disk space on your Linux system? Don’t panic! Tracking down those massive files hogging your storage is simpler than it seems. Here are five practical command-line examples to help you identify and manage large files efficiently.
Table of Contents
1. Using du and sort Together
A quick way to find the biggest files in a directory (and its subdirectories) is by combining the du and sort commands. Try this:
du -a . | sort -n -r | head -n 10
Here’s what’s happening:
• du -a . lists the size of every file in the current directory and its subdirectories.
• sort -n -r organizes the list by size, largest first.
• head -n 10 shows only the top 10 results.
This combo is simple, effective, and doesn’t require any extra tools.
2. Find Files by Size with find
The find command is like a Swiss Army knife for file searching. To find all files larger than 100MB in the /home directory, use:
find /home -type f -size +100M
Here’s the breakdown:
• /home: The directory to search (change this to your target directory).
• -type f: Limits the search to files (not directories).
• -size +100M: Finds files larger than 100MB.
Easy, right?
3. Visualize Disk Usage with ncdu
For a user-friendly, interactive option, check out ncdu. It’s not built-in, but installing it is quick:
sudo apt-get install ncdu
ncdu /
After running ncdu /, you’ll get a navigable display of disk usage starting at the root directory (/). Use the arrow keys to explore. Pro tip: Replace / with any directory path to focus your search.
4. Sort Files by Size with ls
If you’re working in a specific directory and want a fast summary, the ls command gets the job done:
ls -lSh | head
What this does:
• -lSh: Lists files in detailed view (-l), sorts them by size (-S), and makes sizes human-readable (-h).
• head: Limits the output to the first 10 entries.
This is perfect for a quick glance at which files are taking up space.
5. Get Advanced with awk and find
Want more control over the output? Combine awk with find:
find / -type f -size +50M -exec ls -lh {} \; | awk '{ printf "%s %s\n", $5, $9 }'
Here’s the magic:
• find locates files over 50MB.
• ls -lh gives a detailed, human-readable size.
• awk formats the output, showing only the file size and name.
FAQs
- How Do I Find Files Larger Than 100MB in Linux?
Use the find command to locate files exceeding 100MB:
find / -type f -size +100M
This starts from the root directory (/) and checks every subdirectory. To limit the search to a specific directory (like /home), replace / with the directory path:
find /home -type f -size +100M
2. How Do I Delete Large Files in Linux?
Once you’ve identified unnecessary large files, you can delete them with:
find /home -type f -size +100M -delete
Warning: Be careful! This deletes files immediately. Always double-check what’s being removed and back up critical data.
With these tools and commands, you can take charge of your disk space like a pro. Just remember to verify file importance before hitting delete—losing essential files can lead to bigger headaches than running out of storage.