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

How to check which file is the largest in Linux

WBOY
Release: 2022-07-13 10:17:09
Original
12809 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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

find / -size +100M -size -200M -ls
Copy after login

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
Copy after login

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
Copy after login

Find the 10 largest directories in the current folder:

sudo du -a | sort -n -r | head -n 10
Copy after login

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
Copy after login

Find the largest directory/file (including subfolders):

du -Sh | sort -rh | head -n 10
Copy after login

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”
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template