|
|
发表于 2004-12-3 19:25:21
|
显示全部楼层
列出目录树
2004-04-23 15:18 pm
作者:作者
来自:Linux知识宝库
现载:http://www.linuxhero.com/docs
地址:无名
下面的短小的shell程序可以列出目录树, 充分利用了sed强大的模式匹配能力.
目录树形式如下:
.
`----shellp
`----updates
`----wu-ftpd-2.4
| `----doc
| | `----examples
| `----src
| | `----config
| | `----makefiles
| `----support
| | `----makefiles
| | `----man
| `----util
脚本如下:
#!/bin/sh
# dtree: Usage: dtree [any directory]
dir=${1:-.}
(cd $dir; pwd)
find $dir -type d -print | sort -f | sed -e "s,^$1,," -e "/^$/d" -e "s,[^/]*/([^/]*)$,`----1," -e "s,[^/]*/,| ,g"
看看这个帖子:
http://www.linuxsir.cn/forum.php ... 6%C4%BF%C2%BC%CA%F7
- #!/bin/bash
- redir ()
- {
- #tab是真正的步长计算器
- tab=$tab$singletab
- line=${tab%"$singletab"}"|-------"
- #local比较关键,它规定了count是当前的参数列表值
- local count=$#
- for file in "$@"; do
- thisfile=${thisfile:-$PWD}/$file
- #判断当前文件是否为目录,如果是就开始递归
- if [ -d "$thisfile" ]; then
-
- #如果当前目录是分枝列表的最底层,则需进行特殊处理。
- if [ $count -eq 1 ]; then
- echo -e $line$file/
- #将前一个|符号去掉,看看目录树就知道为什么了。
- tab=${tab%"$singletab"}"\t"
- redir $(ls $thisfile)
- else
- echo -e $line$file/
- redir $(ls $thisfile)
- fi
-
- else
- echo -e $line$file
- fi
-
- thisfile=${thisfile%/*}
- let count=count-1
- done
-
- #这一步比较有意思,因为从递归出来的tab结尾可能是TAB也可能是$singletab,所以分成两步来去掉。
- tab=${tab%"\t"}
- tab=${tab%"|"}
- line=${tab%"$singletab"}"|-------"
- }
- singletab="|\t"
- userinput="$@"
- if ls $userinput; then
- for file in ${userinput:-.}; do
- echo $file
- echo '|'
- if [ -d "$file" ]; then
- cd $file
- redir $(ls)
- cd ..
- fi
- done
- else
- echo "$userinput is wrong"
- fi
复制代码
http://www.linuxsir.cn/forum.php ... 6%C4%BF%C2%BC%CA%F7 |
|