Chinaunix

标题: 使用cp复制目录时,如何排除某些文件类型和文件夹 [打印本页]

作者: hanwangabc    时间: 2010-06-03 14:40
标题: 使用cp复制目录时,如何排除某些文件类型和文件夹
使用cp复制文件夹时,希望排除某些后缀的文件以及指定的文件夹,比如希望排除 .txt、.obj等类型的文件,cp中没有exclude选项,如何实现?
作者: whoiswhoz    时间: 2010-06-03 14:54
想到个比较麻烦的办法
[root@localhost cp]# ll
total 0
-rw-r--r--  1 root root 0 Jun  3 07:57 aaa
-rw-r--r--  1 root root 0 Jun  3 07:57 bbb
[root@localhost cp]# ls |sed 's/bbb//g' >/root/ffff
[root@localhost cp]# cat /root/ffff
aaa
[root@localhost cp]# cp $(cat /root/ffff) /cd/v1
[root@localhost cp]# ll /cd/v1
total 0
-rw-r--r--  1 root root 0 Jun  3 08:00 aaa
作者: 渣渣鸟    时间: 2010-06-03 14:55
find /your/src/dir ! -name "*.txt" -a ! -name "*.doc"  -print0 | xargs -0 -n1 cp {} /your/dest/dir
作者: hanwangabc    时间: 2010-06-03 15:30
回复 3# 渣渣鸟
运行的时候报错,比如有有个a.txt,运行脚本时报 cp: 目标 “a.txt” 不是目录Ex
作者: 渣渣鸟    时间: 2010-06-03 15:45
cp $(find /your/src/dir -type f ! -name "*.txt" -a ! -name "*.doc")  /your/dest/dir
作者: hanwangabc    时间: 2010-06-03 15:49
cp $(find /your/src/dir -type f ! -name "*.txt" -a ! -name "*.doc")  /your/dest/dir
渣渣鸟 发表于 2010-06-03 15:45



谢谢,这样只是拷贝文件,如果我希望保存原来目录的结构,比如原来目录A下有很多子目录(a,b,c,d,...层数有多层),我需要排除所有的.txt和.bat,其他的全部复制,且保持原来目录的结构,怎么实现呢?
作者: 渣渣鸟    时间: 2010-06-03 15:58
本帖最后由 渣渣鸟 于 2010-06-03 16:01 编辑

mkdir ~/tmp

cp /your/src/dir ~/tmp -rf

find ~/tmp/src_dir_name  -name "*.txt" -o -name "*.bat" | xargs -n1 rm -f

the new directory ~/tmp/src_dir_name is what you need.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2