在当前目录中执行shell脚本

时间:2012-02-04 01:40:32

标签: bash shell

我正在编写一个shell脚本来列出占用空间最多的五个文件夹。这就是我所拥有的:

$ du -m --max-depth 1 | sort -rn 5

这样可以正常工作,但如何让这段代码检查从中调用它的目录?

2 个答案:

答案 0 :(得分:0)

检查这是否回答了您的问题:

$dir=`pwd`

if [ $dir = "some_directory_you_are_expecting" ]
then
    du -m --max-depth 1 | sort -rn 5
else
    do_some_other_thing
fi

答案 1 :(得分:0)

您要将“5”作为要排序的文件,但是您想要对du的输出进行排序。也许你的种类不同。我来自http://www.gnu.org/software/coreutils/

这里:

$ echo 'du -sm * | sort -rn | head -n5 ' > ~/bin/top5.sh 
raptor: ~
$ chmod 755 ~/bin/top5.sh
raptor: ~
$ top
top      top5.sh  
raptor: ~
$ top5.sh 
606     src
532     svn
407     tmp
349     images
45      workspace
raptor: ~
$ cd /usr/
raptor: /usr
$ top5.sh 
du: cannot read directory `lib64/mozilla/extensions': Permission denied
du: cannot read directory `lost+found': Permission denied
du: cannot read directory `share/ggz/modules': Permission denied
1928    lib64
1842    share
1779    src
492     bin
457     lib32
相关问题