The first type: ls
The easiest way is to use the ls command, because the output of the ls command itself is with file size information.
For example, I want to list the 20 largest files in the /data/log/ directory, which can:
ls -lSh /data/log/ | head -20
The second type: find
find itself is a search command, which can recursively find a subdirectory of a directory, so it is natural to use it.
For example, find the largest 5 files in the /etc directory:
find /etc -type f -printf “%s\t%p\n” | sort -n | tail -5 | xargs ls -Slh
Find the largest 10 files under the current username
find $HOME -type f -printf “%s\t%p\n” | sort -nr | head -10 | xargs ls -Slh
Query files larger than 200M
find / -type f -size +200M | xargs ls -Slh
Query files between 100M and 200M
find / -type f -size +100M -size +200M | xargs ls -Slh
Query the largest 5 files in the root directory
find /root -type f -exec ls -s {} ; | sort -n | tail -n5 | xargs ls -Slh
Query/Directory 5 largest files 10 days ago
find / -type f -mtime +10 -printf “%s\t%p\n” | sort -n | tail -5 | xargs ls -Slh
The third type: du
The du command can view the usage of disk space, and naturally it can also be used to view files and folders that take up more space on the disk.
Find the 5 largest files under /root
du -ah /root | sort -nr | head -n5
Find the largest 5 directories in the current directory
du -ah | sort -nr | head -n5
Find the largest directory/file (including subfolders) in the root directory
du -Sh / | sort -rh | head -n10
Only look at all files with sizes in GB
du -ah / | grep “[0-9]G\b”
Command parameters involved
find:
- -exec<exec instruction>: Assuming that the return value of the find instruction is True, the instruction is executed;
-mtime n Find the file data in the system that was changed in the last n days + greater than - less than
- -type Find a file of a certain type
- b - Block device file;
- c - Character device file;
- d - Directory;
- p - Pipeline file;
- f - Normal file;
- l - Symbol link file;
- s -socket file;
-printf<output format>: Assuming the return value of the find instruction is Ture, list the file or directory name to the standard output. The format can be specified by yourself;
sort:
- -n Sort by the size of the numeric value;
- -r Sort in reverse order;
xargs: Pass parameters
ls -Slh
- -Sl Display details from large to small
- -Slr Show details from small to large
- -h humans is displayed in a human-readable way (normally displayed as bit, plus -h, the displayed KB MB GB TB, etc.)
du:
- -a Displays the size of all directories or files
- -h Improve information readability in units of K, M, G
- -S shows the size of the directory, but does not contain the size of the subdirectory
This is the end of this article about 3 ways to find the largest file on Linux. For more related Linux content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!