Linux disk use command du improved

  

We know that under Linux, if you want to know the size of each file or subdirectory in the current directory, you can use the du command to complete this action. Such as:

$ du -sh * This command can display the size of each file and subdirectory in K, M, G mode. We call this method, human-readable, which is the way people can read, as follows:

8.4G Desktop2.6G Documents12K keys12M Pictures536K scripts However, it is a pity that our du There is no related sorting function, so if under human-readable, that is, the -h parameter, it is difficult to sort using the sort command. Because that becomes a string sort, the decimal point, the number of digits, and the units K, M, G will make the sorting confusing. So, how can we have the human-readble function and sorting? We have to borrow some scripting language to handle it.

The following is the use of Perl to achieve this:

du -sk * |  Sort -n |  # Sort perl -ne ' in K-bytes # #Use Perl to handle KM and G units ($s,$f)=split(m{\\t}); #separate size/file names for (qw(KMG) )) { # Cycles in size units if($s<1024) { #if size<1024 then printf("%.1f",$s); #display file size print "$_\\t$ f"; #display file name last #换行}; $s=$s/1024 #除1024 and then enter the next size unit}

Copyright © Windows knowledge All Rights Reserved