QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3946|回复: 9

[原创]列出指定目录及其子目录里的所有空目录

[复制链接]
发表于 2006-10-20 20:50:12 | 显示全部楼层 |阅读模式
[code:1]
#!/bin/bash
# filename: void_dirs_ls.sh
# author: skypp
# description: list void directories of specified directory and its sub directories
# usage: sh ./void_dirs_ls.sh [DIRECTORY]

dirs=($(ls $1 -R | grep : |sed s/://))
for((i=0;i<${#dirs[@]};i++))
do
((i<${#dirs[@]}-1)) && [[ ${dirs[$i+1]} != ${dirs[$i]}* ]] && [[ $(ls ${dirs[$i]}) == '' ]] && echo ${dirs[$i]} && continue || [[ $(ls ${dirs[$i]}) == '' ]] && echo ${dirs[$i]}
done
[/code:1]

同事想列出指定目录包括其子目录里的所有空目录,自个写了一个,不知道这里有没有更好的script
发表于 2006-10-21 00:45:11 | 显示全部楼层
  不错 不错
回复

使用道具 举报

 楼主| 发表于 2006-10-22 17:39:38 | 显示全部楼层
通常用du来实现好像速度更快一些
[code:1]
#!/bin/bash
# filename: void_dirs_du.sh
# author: skypp
# description: list void directories of specified directory and its sub directories
# usage: sh ./void_dirs_du.sh [DIRECTORY]

dirs=($(du $1 | grep 0 | awk '{print $2}'))
[[ $(ls ${dirs[0]}) == '' ]] && echo ${dirs[0]}
for((i=1;i<${#dirs[@]};i++))
do
[[ ${dirs[$i-1]} != ${dirs[$i]}* ]] && [[ $(ls ${dirs[$i]}) == '' ]] && echo ${dirs[$i]}
done
[/code:1]
突然感觉用awk一行就可以实现,请哪位赐教,小弟谢过
回复

使用道具 举报

 楼主| 发表于 2006-10-23 10:49:42 | 显示全部楼层
一不小心成精华贴了,呵呵
这次的是一句话的,效率可能低了点,不过也算是一个实现了
[code:1]
#!/bin/bash
# filename: void_dirs_awk.sh
# author: skypp
# description: list void directories of specified directory and its sub directories
# usage: sh ./void_dirs_awk.sh [DIRECTORY]

du $1 | grep 0 | awk '{system("[[ -z $(ls " $2 ") ]]&&echo "$2)}'
[/code:1]
回复

使用道具 举报

发表于 2006-10-23 11:56:02 | 显示全部楼层
回复

使用道具 举报

发表于 2006-10-28 17:27:39 | 显示全部楼层
du /root|awk ' ($1==4) {print $2}'

du $1|awk ' ($1==4) {print $2}'

这样不知怎么样啊!
那个grep 后面不应是0吧!应是4吧!
回复

使用道具 举报

 楼主| 发表于 2006-11-12 10:58:36 | 显示全部楼层
不好意思公司放长假,才回来呵呵
楼上用的是什么Linux啊,我在Cygwin下和SuSe 9.3 X64下查看空目录大小都是0K,不过在RHEL4 AS下看空目录大小是8K
大家看情况使用0、4或者8,可能还有其他大小,哪位大虾指点指点
回复

使用道具 举报

发表于 2006-11-16 16:42:25 | 显示全部楼层
在个新问题:
如在某目录中只有若干个字节为0的文件或目录,那此目录是否为空目录啊!
因此目录大小还是为0、4或者8.
回复

使用道具 举报

 楼主| 发表于 2006-11-18 15:54:23 | 显示全部楼层
所以后边还得加点代码用ls探测下有没有空文件,有的话就不算空目录了,看我上面的小程序
回复

使用道具 举报

 楼主| 发表于 2006-11-22 15:35:25 | 显示全部楼层
[quote:0968214d9b="seamonkey"]ls -ld /path/to/dir/**/*(/^F)

zsh [/quote]

一语惊醒梦中人,看来得好好学习下zsh了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-25 06:53 , Processed in 0.049753 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表