Use the shell to find the largest file in a directory

Source: Internet
Author: User

This is a very interesting question, because as a shell rookie, I have no idea for the first time. I wonder why this kind of operation Linux actually does not have a direct command to implement such a query.

Naturally, the first feeling is to use awk to do it, because the rookie I see awk looks silly:

Ls-l | awk '/^[^d]/{print $5,$9} ' | Sort-nr | Head-1

Obviously LS view the complete file information, filter out the D-beginning of the East (directory), take out the file size of $ two, the name $9, sort, fetch the first

Naturally the problem becomes clear, and it is worth discussing how to implement the process of filtering out the file size and name before. The sort and head in the back are all the same, regardless of the method.

Baidu this question, found that a forum has the great God's answer. Mainly for the sub-directory can be queried. Because the great God used the Find command:

Find-type f-exec stat-c "%s%n" {} \; | Sort-nr | Head-1

The default path is the current directory, which executes the command stat, which is also the return size (in bytes) and the name, blah blah. Note that stat is not state, and a rookie says it's wrong every time.

Later in a basic textbook to see another way, with the du command, unusual worship:

Du-s * | Sort-nr | Head-1

Much simpler than the previous awk. The-s parameter returns the size of the file in the current directory, * Drill down to the size of each file, excluding the total size of the directory.

However, after a careful study, found that this method is the directory as a file to see. So the maximum file may be a subdirectory, not a file. The advantage is that du can set the units of size display, compared to the fool is directly with the-h parameter, the so-called human-readable form.

So in summary, or the previous two commands OK, the first is to query only the root of the largest file, the second is recursive to each subdirectory to find the largest file. As for du often involves the sum of subdirectories, so the function of the third command is: Query the current directory, the largest file or directory.

Transferred from: http://www.cnblogs.com/loveglory/archive/2012/11/29/2794790.html

Use the shell to find the largest file in a directory (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.