To find the largest 10 files in a directory recursively on Ubuntu, use a combination of the
find, du, sort, and head commands.Command to find the 10 largest files (human-readable format)
Run the following command in your terminal. Replace
. with the specific directory path if you want to search a different location:bash
find . -type f -exec du -Sh {} + | sort -rh | head -n 10
Explanation of the Command
Here is a breakdown of the commands and options used:
find .: Starts the search from the current directory (.) and traverses all subdirectories recursively.-type f: Restricts the search to regular files only, ignoring directories, links, etc..-exec du -Sh {} +: Executes theducommand on the found files.du: Estimates file space usage.-S: Reports the size of individual files, not including the size of subdirectories (important for listing individual files correctly).-h: Displays sizes in a human-readable format (e.g., 10MB, 2G).{}and+: These are used byfindto pass multiple filenames to a singleducommand efficiently.
|: Pipes the output of thefindcommand to thesortcommand.sort -rh: Sorts the output lines.-r: Reverses the sort order, placing the largest files at the top.-h: Compares human-readable numbers correctly (e.g., understands that 2G is larger than 10M).
| head -n 10: Displays only the first 10 lines of the sorted output (the top 10 largest files).
Alternative: Using Bytes for Precise Sorting
If
you need a very precise numeric sort (e.g., if the human-readable
sorting behaves unexpectedly on non-GNU systems), you can print sizes in
bytes:
bash
find . -type f -printf "%s %p\n" | sort -nr | head -n 10
This command prints the size in bytes (
%s) followed by the file path (%p), sorts numerically (-n) in reverse order (-r), and shows the top 10 results.Interactive Method (GUI/TUI)
For an interactive, user-friendly experience, you can use the
ncdu tool, which is available in the Ubuntu package manager.- Install
ncdu:sudo apt install ncdu. - Run in the directory:
ncdu .(or a specific path, e.g.,ncdu /var/log). This provides an interactive interface to navigate directories and see the largest files and folders.
No comments:
Post a Comment