免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: kuangzhangshi
打印 上一主题 下一主题

如何实现按文件夹大小排序(大小里面包含G、M、K符号) [复制链接]

论坛徽章:
0
21 [报告]
发表于 2009-04-08 19:16 |只看该作者
和我以前问的一样



  1. du -sk */ | sort -n | cut -f2 | xargs -d '\n' du -sh
复制代码


据说迟此sort可能会出一个带有这种排序的参数

论坛徽章:
0
22 [报告]
发表于 2009-04-08 21:18 |只看该作者

回复 #21 haimming 的帖子

[root@erpdataserver ~]# du -sk */ | sort -n | cut -f2 | xargs -d '\n' du -sh
xargs: invalid option -- d
Usage: xargs [-0prtx] [-E eof-str] [-e[eof-str]] [-I replace-str]
       [-i[replace-str]] [-L max-lines] [-l[max-lines]] [-n max-args]
       [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]
       [--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]
       [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]
       [--max-args=max-args] [--no-run-if-empty] [--version] [--help]
       [command [initial-arguments]]

Report bugs to <bug-findutils@gnu.org>.

论坛徽章:
0
23 [报告]
发表于 2009-04-08 21:19 |只看该作者

回复 #20 ywlscpl 的帖子

你这个也不错,就是没排除文件。

论坛徽章:
0
24 [报告]
发表于 2009-04-08 21:32 |只看该作者
du -sk */ | sort -n | cut -f2 | xargs du -sh

论坛徽章:
0
25 [报告]
发表于 2009-04-08 21:40 |只看该作者
[root@erpdataserver local]# du -sh */ |sed -r 's/([0-9.]*)/\1 /' | awk '{if ($2~/K/){$2="N";print $0} else print $0}' |sort -k 2,2 -k 1nr,1|awk '{if ($2~/N/){$2="K"; print $0} else print $0}'
6.2 G   jboss-4.0.4.GA/
144 M   jdk1.5.0_01/
20 M    apache-tomcat-5.5.16/
14 M    zh_man/
9.9 M   erptask/
2.8 M   bin/
104 K share/
8.0 K etc/
8.0 K games/
8.0 K include/
8.0 K lib/
8.0 K libexec/
8.0 K sbin/
8.0 K src/

论坛徽章:
0
26 [报告]
发表于 2009-04-08 22:01 |只看该作者
只统计目录,目录名带空格不支持
(若统计所有,把!system("[ -d "$NF" ]")改成1)
  1. du -sh * |awk '{v=substr($1,length($1),1)}v=="G"{$0="1G "$0}v=="M"{$0="2M "$0}v=="K"{$0="3K "$0}v=="0"{$0="4Z "$0}!system("[ -d "$NF" ]")' |sort -k1,1n -k2,2nr |awk '{print substr($0,4)}'
复制代码


[root@Mylinux var]# pwd
/var
[root@Mylinux var]# du -sh * |awk '{v=substr($1,length($1),1)}v=="G"{$0="1G "$0}v=="M"{$0="2M "$0}v=="K"{$0="3K "$0}v=="0"{$0="4Z "$0}!system("[ -d "$NF" ]")' |sort -k1,1n -k2,2nr |awk '{print substr($0,4)}'
34M     lib
3.4M    log
1.6M    cache
852K    tmp
214K    arpwatch
42K     run
25K     spool
4.0K    lock
4.0K    state
3.0K    empty
3.0K    named
2.0K    db
1.0K    cvs
1.0K    ftp
1.0K    local
1.0K    nis
1.0K    opt
1.0K    preserve
1.0K    yp
0       mail

[ 本帖最后由 ywlscpl 于 2009-4-8 22:07 编辑 ]

论坛徽章:
1
寅虎
日期:2014-11-30 21:25:54
27 [报告]
发表于 2009-04-09 00:07 |只看该作者
du -sk ./* | sort -nr | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

网上看到一个 不是我写的 。。。

论坛徽章:
0
28 [报告]
发表于 2009-04-09 00:29 |只看该作者

回复 #22 我是DBA 的帖子

版本问题吧
man xargs
   --delimiter=delim
   -d delim
          Input items are terminated by the specified  character.   Quotes
          and  backslash  are not special; every character in the input is
          taken literally.  Disables  the  end-of-file  string,  which  is
          treated  like any other argument.  This can be used when the in-
          put consists of simply newline-separated items, although  it  is
          almost  always better to design your program to use --null where
          this is possible.  The specified delimiter may be a single char-
          acter,  a  C-style  character  escape such as \n, or an octal or
          hexadecimal escape code.  Octal and hexadecimal escape codes are
          understood as for the printf command.   Multibyte characters are
          not supported.

xargs (GNU findutils) 4.4.0
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b

论坛徽章:
0
29 [报告]
发表于 2009-04-09 09:19 |只看该作者

回复 #28 haimming 的帖子

可能是,我这个版本上没有

论坛徽章:
0
30 [报告]
发表于 2009-04-09 09:29 |只看该作者
原帖由 我是DBA 于 2009-4-8 21:19 发表
你这个也不错,就是没排除文件。

还是DBA知道我的想法,不要列出文件,只要目录
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP