Chinaunix首页 | 论坛 | 博客
  • 博客访问: 590009
  • 博文数量: 40
  • 博客积分: 7274
  • 博客等级: 少将
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-20 15:00
个人简介

Expired

文章分类
文章存档

2011年(1)

2008年(3)

2007年(17)

2006年(10)

2005年(9)

分类:

2007-10-02 22:31:48

使用 du -sh * 可以显示指定目录下各文件/目录的大小情况,但是输出结果不够人性化(human-readable),以 /usr/share/目录为例

du -sh /usr/share/* 会输出如下信息

654K    /usr/share/aclocal
141K    /usr/share/aclocal-1.9
318K    /usr/share/alsa
145K    /usr/share/applications
1.9M    /usr/share/audacious
170K    /usr/share/audacious-plugins
24K /usr/share/aumix
1.2M    /usr/share/autoconf
926K    /usr/share/automake-1.9

如果要非常直观地按大到小显示个文件目录的大小,我们可以使用脚本来完成,对 du 后的信息进行处理后再按照要求输出,比如只输出大小在多少M以上的目录信息

这是很久以前的一个脚本,这几天正好有时间,就整理了下。


#!/usr/bin/perl

## filename: dfdir.pl
## script for getting file list of specified directory,
## then output them in order by size.

## Powered by Muddyboot, last modified: 2005-04-26
## only accept one parameter

$basename=substr($0,rindex($0,'/')+1);
sub usage() {
    print "\nUsage: $basename DIRECTORY [MIN-SIZE]\n\n";
    print "Get file list of DIRECTORY, then output them in order by size.\n";
    print "If MIN-SIZE is specified, only print those greater than MIN-SIZE.\n";
    print "MIN-SIZE is non-zero integer optionally followed by a k/K/m/M.\n\n";
    exit;
}

sub print_err () {
    die "\n\e[1;31m$_[0]\e[0;39m\n\n";
}
    
die &usage if (@ARGV) < 1 ;
if ($ARGV[0] eq '-h' or $ARGV[0] eq '-help' or $ARGV[0] eq '--help') {
    &usage;
}

## delete the last slash in dirname
($dir=$ARGV[0]) =~ s@/$@@;

## check if exists
&print_err("ERROR: destination: $dir does not exist.") if ! -e $dir;

## check if is a directory
&print_err("ERROR: destination: $dir is not an directory.") if ! -d $dir;

## get the filelist length and count the max length
opendir DH,"$dir" or die "\nCannot open directory: $dir\n\n";
$maxlen=0;

foreach $file (readdir DH) {
    $curlen=length($file)+length($dir)+1;
    if ($curlen > $maxlen) {
        $maxlen=$curlen;
    }
}
closedir DH;

## min size parameter control

if ($ARGV[1] ne "" ){
    $msize=$ARGV[1];
    if ($msize !~ /^[0-9]+$/ and $msize !~ /^[0-9]+[kKMm]$/ or $msize == 0 ) {
        &usage;
    } elsif ($msize =~ /[mM]$/) {
        $msize =~ s/[mM]$//;
        $msize *= 1024;
    }
}

## get dir usage information
open INFO,"du -s $dir/*|sort -rn|" or &print_err("ERROR: Cannot open $dir.");

## process information and output
while (<INFO>) {
    chomp;
    my($size,$file)=/(\S+)\s+(.*)/;
    if ($size > 1024*1024 and $size > $msize) {
        printf "%5.1fG\t%s\n",$size/1024/1024,$file;
    }
    elsif ($size > 1024 and $size > $msize) {
        printf "%5.1fM\t%s\n",$size/1024,$file;
    }
    elsif ($size > $msize) {
        printf "%5dK\t%s\n",$size,$file;
    } else {
        last;
    }
}


运行方式:dfdir.pl <目录名> [整数][k/m]

例子1:排序显示 /usr/share 下所有文件目录大小信息
dfdir.pl /usr/share

例子2:显示 /usr/share 下大于 5M 的文件或目录信息
dfdir.pl /usr/share 5m

输出结果如下:
113.6M  /usr/share/fonts
 50.1M  /usr/share/man
 32.5M  /usr/share/gtk-doc
 18.9M  /usr/share/info
 17.9M  /usr/share/themes
 17.4M  /usr/share/vim
 12.6M  /usr/share/xml
  8.9M  /usr/share/icons
阅读(5166) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~