分页: 1 / 1

如何批量删除同名子目录?(己解决)

发表于 : 2011-08-24 5:07
lam2888
最近买了一本书《实战linux shell 编程与服务器管理》,根据书中提示,从http://bash.ols3.net/DL/shell-98-v1.tgz下载包文件解压,脚本文件为BIG5编码,转为UTF8:
find shell-98-v1 -type d -exec mkdir -p utf8/{} \;
find shell-98-v1 -type f -exec iconv -f BIG5 -t UTF-8 {} -o utf8/{} \;
mv utf8/* shell-98-v1
rm -fr utf8
find . 得结果
.
./shell-98-v1
./shell-98-v1/cha
./shell-98-v1/cha/samples
./shell-98-v1/cha/samples/setpars2.sh
./shell-98-v1/cha/samples/getip2.sh
./shell-98-v1/cha/samples/getline3.sh
./shell-98-v1/cha/samples/setpars.sh
./shell-98-v1/cha/samples/getline2.sh
./shell-98-v1/cha/samples/Factor.sh
./shell-98-v1/cha/samples/shift.sh
./shell-98-v1/cha/samples/loto.sh
./shell-98-v1/cha/samples/setpars3.sh
./shell-98-v1/cha/samples/getline4.sh
./shell-98-v1/cha/samples/myfun.sh
./shell-98-v1/cha/samples/mylib1.sh
./shell-98-v1/cha/samples/appendfile.sh
./shell-98-v1/cha/samples/getline1.sh
./shell-98-v1/ch7
./shell-98-v1/ch7/samples
./shell-98-v1/ch7/samples/test99.sh
./shell-98-v1/ch7/samples/fgrep.sh
./shell-98-v1/ch7/samples/getip1.sh
./shell-98-v1/chk
./shell-98-v1/chk/samples
./shell-98-v1/chk/samples/get_loading2.sh
./shell-98-v1/chk/samples/get_hostname.sh
./shell-98-v1/chk/samples/cr_acl.sh
./shell-98-v1/chk/samples/chkvol.sh
./shell-98-v1/chk/samples/my_fichecker.sh
./shell-98-v1/chk/samples/mailto2.sh
./shell-98-v1/chk/samples/cr_iptables.sh
./shell-98-v1/chk/samples/getip1.sh
./shell-98-v1/chk/samples/chk_dir_size.sh
./shell-98-v1/chk/samples/get_swap.sh
./shell-98-v1/chk/samples/mailattach.sh
./shell-98-v1/chk/samples/get_mem.sh
./shell-98-v1/chk/samples/get_loading.sh
./shell-98-v1/chk/samples/get_disk.sh
./shell-98-v1/chk/samples/get_hd_size.sh
./shell-98-v1/chk/samples/get_disk_free.sh
./shell-98-v1/chj
./shell-98-v1/chj/samples
./shell-98-v1/chj/samples/wget.sh
./shell-98-v1/chj/samples/autoftp2.sh
./shell-98-v1/chj/samples/autoscp.sh
./shell-98-v1/chj/samples/autoftp.sh
./shell-98-v1/chj/samples/dot.netrc
./shell-98-v1/ch9
./shell-98-v1/ch9/samples
./shell-98-v1/ch9/samples/forsum.sh
./shell-98-v1/ch9/samples/break1.sh
./shell-98-v1/ch9/samples/readfile3.sh
./shell-98-v1/ch9/samples/while1.sh
./shell-98-v1/ch9/samples/checkhost.sh
./shell-98-v1/ch9/samples/break2.sh
./shell-98-v1/ch9/samples/checkstr-notgood.sh
./shell-98-v1/ch9/samples/cvsfile
./shell-98-v1/ch9/samples/getcvs.sh
./shell-98-v1/ch9/samples/select2.sh
./shell-98-v1/ch9/samples/readfile2.sh
./shell-98-v1/ch9/samples/until.sh
./shell-98-v1/ch9/samples/dudir.sh
./shell-98-v1/ch9/samples/case2.sh
./shell-98-v1/ch9/samples/checkopts.sh
./shell-98-v1/ch9/samples/31r-3.sh
./shell-98-v1/ch9/samples/getdir.sh
./shell-98-v1/ch9/samples/case.sh
./shell-98-v1/ch9/samples/select.sh
./shell-98-v1/ch9/samples/continue1.sh
./shell-98-v1/ch9/samples/31r.sh
./shell-98-v1/ch9/samples/checkstr.sh
./shell-98-v1/ch9/samples/checkroot.sh
./shell-98-v1/ch9/samples/for-c.sh
./shell-98-v1/ch9/samples/prime.sh
./shell-98-v1/ch9/samples/checkint.sh
./shell-98-v1/ch9/samples/31r-2.sh
./shell-98-v1/ch9/samples/readfile.sh
./shell-98-v1/ch9/samples/checkstr2.sh
./shell-98-v1/che
./shell-98-v1/che/samples
./shell-98-v1/che/samples/sleep2.sh
./shell-98-v1/chc
./shell-98-v1/chc/samples
./shell-98-v1/chc/samples/getpid1.sh
./shell-98-v1/chc/samples/sleep.sh
./shell-98-v1/ch4
./shell-98-v1/ch4/samples
./shell-98-v1/ch4/samples/test.sh
./shell-98-v1/chd
./shell-98-v1/chd/samples
./shell-98-v1/chd/samples/dataf6.csv
./shell-98-v1/chd/samples/anonyftp.sh
./shell-98-v1/chd/samples/anonyftp1.sh
./shell-98-v1/chd/samples/dataf1
./shell-98-v1/chd/samples/chcsv24.sh
./shell-98-v1/chd/samples/dataf5
./shell-98-v1/chd/samples/dataf3
./shell-98-v1/chd/samples/vsftpd.conf
./shell-98-v1/ch8
./shell-98-v1/ch8/samples
./shell-98-v1/ch8/samples/netmask_2_bits.sh
./shell-98-v1/chg
./shell-98-v1/chg/samples
./shell-98-v1/chg/samples/inputbox1.sh
./shell-98-v1/chg/samples/radiolist2.sh
./shell-98-v1/chg/samples/menu1.sh
./shell-98-v1/chg/samples/progress1.sh
./shell-98-v1/chg/samples/yesnobox1.sh
./shell-98-v1/chg/samples/radiolist1.sh
./shell-98-v1/chg/samples/msgbox1.sh
./shell-98-v1/chg/samples/passwordbox2.sh
./shell-98-v1/chg/samples/checklist3.sh
./shell-98-v1/chg/samples/progress2.sh
./shell-98-v1/chg/samples/inputbox4.sh
./shell-98-v1/chg/samples/inputbox2.sh
./shell-98-v1/chg/samples/inputbox3.sh
./shell-98-v1/chg/samples/passwordbox1.sh
./shell-98-v1/chg/samples/checklist1.sh
./shell-98-v1/chg/samples/checklist2.sh
./shell-98-v1/ch6
./shell-98-v1/ch6/samples
./shell-98-v1/ch6/samples/heredoc.sh
./shell-98-v1/ch6/samples/lamp.sh
./shell-98-v1/ch6/samples/mustpreset2.sh
./shell-98-v1/ch6/samples/lamp2.sh
./shell-98-v1/ch6/samples/mustpreset.sh
./shell-98-v1/ch6/samples/create_prg.sh
./shell-98-v1/readme.html
./shell-98-v1/chl
./shell-98-v1/chl/samples
./shell-98-v1/chl/samples/wmultif.sh
./shell-98-v1/chl/samples/bk2.sh
./shell-98-v1/chl/samples/bk.sh
./shell-98-v1/chl/samples/mt-tape-funs.sh
./shell-98-v1/chl/samples/gen_rsync.sh
./shell-98-v1/ch5
./shell-98-v1/ch5/samples
./shell-98-v1/ch5/samples/lamp.sh
./shell-98-v1/ch5/samples/test.tgz
./shell-98-v1/ch5/samples/email.lst
./shell-98-v1/ch5/samples/dataf2
./shell-98-v1/ch5/samples/dirNAME.sh
./shell-98-v1/ch5/samples/dataf4
./shell-98-v1/ch5/samples/baseNAME.sh
./shell-98-v1/ch5/samples/dataf1
./shell-98-v1/ch5/samples/dataf5
./shell-98-v1/ch5/samples/dataf3
./shell-98-v1/ch5/samples/backupDIR.sh
./shell-98-v1/chh
./shell-98-v1/chh/samples
./shell-98-v1/chh/samples/readcfg.sh
./shell-98-v1/chh/samples/sapp.conf
./shell-98-v1/chh/samples/mksock.diff
./shell-98-v1/chh/samples/cp_progress.sh
./shell-98-v1/chh/samples/3.txt
./shell-98-v1/chh/samples/chkfile_fun.sh
./shell-98-v1/chh/samples/1.txt
./shell-98-v1/chh/samples/C
./shell-98-v1/chh/samples/C/mksock.c
./shell-98-v1/chh/samples/B
./shell-98-v1/chh/samples/B/mksock.c
./shell-98-v1/chh/samples/A
./shell-98-v1/chh/samples/A/mksock.c
./shell-98-v1/chh/samples/2.txt
./shell-98-v1/chh/samples/chkfile.sh
./shell-98-v1/chh/samples/listtoolong.sh
./shell-98-v1/chh/samples/qiconv.sh
./shell-98-v1/chh/samples/low2upper
./shell-98-v1/chh/samples/test_filequal
./shell-98-v1/chh/samples/mksock.c
./shell-98-v1/chh/samples/cp_progress2.sh
./shell-98-v1/chh/samples/mksock2.diff
./shell-98-v1/ch1
./shell-98-v1/ch1/samples
./shell-98-v1/ch1/samples/clearlog.sh
./shell-98-v1/ch1/samples/hello.sh
./shell-98-v1/chi
./shell-98-v1/chi/samples
./shell-98-v1/chi/samples/gen_dhcp.sh
./shell-98-v1/chi/samples/ca_by_rule.sh
./shell-98-v1/chi/samples/ca2.sh
./shell-98-v1/chi/samples/pa.sh
./shell-98-v1/chi/samples/gen_dns.sh
./shell-98-v1/chi/samples/delca.sh
./shell-98-v1/chi/samples/ca.sh
./shell-98-v1/chb
./shell-98-v1/chb/samples
./shell-98-v1/chb/samples/openf1.sh
./shell-98-v1/chb/samples/openf2.sh
./shell-98-v1/chb/samples/here1.sh
如何批量将samples目录下的脚本文件往上一目录移动,并删除samples目录? :em03 顺便问一下,为什么书中提到的bash 4.0新增通配符**(就是两个星星)没有效? :em03 书中说**可对比现行目录下所有的子目录和文件(以递归方式)。

Re: 如何批量删除同名子目录?

发表于 : 2011-08-24 8:24
我就是我2
问题一 若有文件重名,只保留一个

代码: 全选

for i in `find shell-98-v1 -type f`;do mv -v "$i" `echo "$i" | awk -F'samples' '{print $1}'`;done
rmdir -v shell-98-v1/*/samples 
问题二 关于通配符我只知道
在计算机(软件)技术中,通配符可用于代替字符。

通常地,星号“*”匹配0个或以上的字符,问号“?”匹配1个字符。在SQL中,通配符可用于“LIKE”表达式中,其百分号“%”匹配0个或以上的字符。
http://zh.wikipedia.org/wiki/%E9%80%9A% ... D%E7%AC%A6
若要对比目录,论坛里有推荐工具. 自己搜索一下.

Re: 如何批量删除同名子目录?

发表于 : 2011-08-24 8:42
tusooa
**似乎是zsh的功能

Re: 如何批量删除同名子目录?

发表于 : 2011-08-24 23:35
sk1418
移动文件另一方法:

代码: 全选

find /path -regex ".*/samples/.+" | awk -F'samples' '{print "mv "$0" "$1}' |sh
移动文件又一方法:

代码: 全选

 find /path -name "*samples" -a -type d|awk '{("dirname "$0|getline a); print "mv "$0"/* "a}'|sh
是不是有点无聊了。。。 :(

Re: 如何批量删除同名子目录?

发表于 : 2011-08-25 0:57
lam2888
多谢了。看来我不是一般的菜。我想到的是怎么用find 的 -exec的选项,没想到用循环和管道,awk。 :em06

Re: 如何批量删除同名子目录?(己解决)

发表于 : 2011-08-27 20:34
我就是我2
如何批量将samples目录下的脚本文件往上一目录移动,并删除samples目录? :em03 顺便问一下,为什么书中提到的bash 4.0新增通配符**(就是两个星星)没有效? :em03 书中说**可对比现行目录下所有的子目录和文件(以递归方式)。
**似乎是zsh的功能
bash和zsh都能用**
后面的搞不定了
find ./shell-98-v1/**/samples/ -type f -exec ls {} \;
2 down vote accepted


It says to go into all the subdirectories below tmp, as well as just the content of tmp.

e.g. I have the following:

$ find tmp
tmp
tmp/a
tmp/a/b
tmp/a/b/file1
tmp/b
tmp/b/c
tmp/b/c/file2

matched output:

$ echo tmp/*
tmp/a tmp/b

matched output:

$ echo tmp/**/*
tmp/a tmp/a/b tmp/a/b/file1 tmp/b tmp/b/c tmp/b/c/file2

It is a default feature of zsh, to get it to work in bash 4, you perform:

shopt globstar
http://stackoverflow.com/questions/3529 ... -asterisks