Home  >  Article  >  Operation and Maintenance  >  How to check which file is the largest in Linux

How to check which file is the largest in Linux

WBOY
WBOYOriginal
2022-07-13 10:17:0912552browse

Method: 1. Use the ls command, which can output file size information. The syntax is "ls -lSh specified folder | head -1"; 2. Use the find command, which can find the subdirectories of the directory. Directory, the syntax is "find specified folder -type f -printf ...|sort -n|tail -1"; 3. Use the du command, this naming can check the disk space usage, the syntax is "du -a /home| sort -n -r | head -n 1".

How to check which file is the largest in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to check which file is the largest in Linux

3 ways to find the largest file on Linux

The first one: ls

The easiest way is to use the ls command, because the output of the ls command itself contains file size information.

For example, if I want to list the 5 largest files in the /bin directory, I can:

ls -lSh /bin | head -5

Second: find

find itself It is a search command that can recursively search subdirectories of a directory, so it is natural to use it.

For example, to find the largest file in the / directory:

sudo find / -type f -printf “%s\t%p\n” | sort -n | tail -1

If you want to find the top 10 large files, you can do this:

$ find $HOME -type f -printf ‘%s %p\n’ | sort -nr | head -10

You can also use the -size option To search, the following command will display all files larger than 100MiB (note not 100MB, the difference between MiB and MB, emmm):

find / -size +100M -ls

You can also search for files between an interval size (such as 100MiB and 200MiB) :

find / -size +100M -size -200M -ls

Finally, the following command is also commonly used to find the five largest files in a directory:

find $DIRECTORY -type f -exec ls -s {} \; | sort -n | tail -n 5

The third type: du

## The #du command can check the usage of disk space. Naturally, it can also be used to check the files and folders that take up a lot of space on the disk.

For example, find the top 20 largest files under /home:

sudo du -a /home | sort -n -r | head -n 20

Find the 10 largest directories in the current folder:

sudo du -a | sort -n -r | head -n 10

If you want to display readable For KB, MB, and GB information, you can add the -h parameter:

du -hs * | sort -rh | head -n 10

Find the largest directory/file (including subfolders):

du -Sh | sort -rh | head -n 10

If you only look at all files with a size within the GB range file, you can use the du command and the grep command at the same time:

du -h -a /dir | grep “[0-9]G\b”

Recommended learning:

Linux video tutorial

The above is the detailed content of How to check which file is the largest in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn