免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 25605 | 回复: 6
打印 上一主题 下一主题

使用cp复制目录时,如何排除某些文件类型和文件夹 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-03 14:40 |只看该作者 |倒序浏览
使用cp复制文件夹时,希望排除某些后缀的文件以及指定的文件夹,比如希望排除 .txt、.obj等类型的文件,cp中没有exclude选项,如何实现?

论坛徽章:
0
2 [报告]
发表于 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

论坛徽章:
0
3 [报告]
发表于 2010-06-03 14:55 |只看该作者
find /your/src/dir ! -name "*.txt" -a ! -name "*.doc"  -print0 | xargs -0 -n1 cp {} /your/dest/dir

论坛徽章:
0
4 [报告]
发表于 2010-06-03 15:30 |只看该作者
回复 3# 渣渣鸟
运行的时候报错,比如有有个a.txt,运行脚本时报 cp: 目标 “a.txt” 不是目录Ex

论坛徽章:
0
5 [报告]
发表于 2010-06-03 15:45 |只看该作者
cp $(find /your/src/dir -type f ! -name "*.txt" -a ! -name "*.doc")  /your/dest/dir

论坛徽章:
0
6 [报告]
发表于 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,其他的全部复制,且保持原来目录的结构,怎么实现呢?

论坛徽章:
0
7 [报告]
发表于 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.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP