|
发表于 2007-3-20 23:54:36
|
显示全部楼层
第一,如果可以用rm -rf的话,就无需做递归了。
第二,如果不允许,那么如下:
- #!/bin/bash
- for direc in `ls $1/`
- do
- echo $direc
- if test -d $direc
- then
- cd $direc
- `basename $0` .
- cd ..
- rmdir $direc
- else
- rm $1/$direc
- fi
- done;
复制代码
结果:
- peter@Thinkpad:~/temp$ tree .
- .
- |-- 1.txt
- |-- 2.txt
- |-- 3.txt
- |-- test1
- | |-- 4.txt
- | `-- test3
- | `-- 5.txt
- `-- test2
- `-- 6.txt
- 3 directories, 6 files
- peter@Thinkpad:~/temp$ mysh .
- 1.txt
- 2.txt
- 3.txt
- test1
- 4.txt
- test3
- 5.txt
- test2
- 6.txt
- peter@Thinkpad:~/temp$ ls
- peter@Thinkpad:~/temp$
复制代码 |
|