LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1567|回复: 10

求助,find命令和cp命令合起来

[复制链接]
发表于 2008-4-8 13:52:11 | 显示全部楼层 |阅读模式
我想搜索我的/media/hda7/My pacture/333文件夹内的.jpg图片,并复制到~/.fluxbox/background文件夹内,这个命令应该怎么写?
我写find /media/hda7/我的文档/My\ Pictures/3333/ -name -exec cp *.jpg ~/.fluxbox/background,却不能行,提示出错paths must precede expression,说路径必须在符号前面~~~不明白,而且我写find命令的时候,经常格式写对了,却提示出错~~~
发表于 2008-4-8 15:48:49 | 显示全部楼层
find . -name 'pattern' -exec cp "{}" dest-dir \;
回复 支持 反对

使用道具 举报

发表于 2008-4-8 23:55:57 | 显示全部楼层
find . -name *.jpg | xargs cp  dest-dir
回复 支持 反对

使用道具 举报

发表于 2008-4-9 09:27:24 | 显示全部楼层
man find
…………
-exec command ;
              Execute command; true if 0 status is  returned.   All  following
              arguments to find are taken to be arguments to the command until
              an argument consisting of ‘;’ is encountered.  The  string  ‘{}’
              is  replaced by the current file name being processed everywhere
              it occurs in the arguments to the command, not just in arguments
              where  it  is alone, as in some versions of find.  Both of these
              constructions might need to be escaped (with a ‘\’) or quoted to
              protect them from expansion by the shell.  See the EXAMPLES sec‐
              tion for examples of the use of the ‘-exec’ option.  The  speci‐
              fied  command is run once for each matched file.  The command is
              executed in the  starting  directory.    There  are  unavoidable
              security  problems  surrounding  use  of  the  -exec option; you
              should use the -execdir option instead.

       -exec command {} +
              This variant of the -exec option runs the specified  command  on
              the  selected  files, but the command line is built by appending
              each selected file name at the end; the total number of  invoca‐
              tions  of  the  command  will  be  much  less than the number of
              matched files.  The command line is built in much the  same  way
              that  xargs builds its command lines.  Only one instance of ’{}’
              is allowed within the command.  The command is executed  in  the
              starting directory.
…………
find /media/hda7/我的文档/My\ Pictures/3333 -name "*.jpg" -exec cp '{}' ~/.fluxbox/background \;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-4-9 09:30:41 | 显示全部楼层
谢谢~~~我原先打印了两本关于find命令的文档~~~但就是这个exec选项不会用~~~谢谢这次会用了
回复 支持 反对

使用道具 举报

发表于 2008-4-9 09:48:34 | 显示全部楼层
Post by showboy;1836238
find . -name *.jpg | xargs cp  dest-dir


好方法,受教了
回复 支持 反对

使用道具 举报

发表于 2008-4-9 23:54:26 | 显示全部楼层
Post by showboy;1836238
find . -name *.jpg | xargs cp  dest-dir


这种用法自己用用差不多,给新手用好像不太好。
因为如果别人的jpg文件名中有特殊字符,比如空格,就会有问题了。可能某些文件拷不到,甚至可能拷了错误的文件。

保险的做法是在find中用-exec。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-4-10 08:44:34 | 显示全部楼层
Post by showboy;1836238
find . -name *.jpg | xargs cp  dest-dir


这个方法老实说看不懂,不过幸好我打印的两本资料里有这个命令~~~好好学习一下
回复 支持 反对

使用道具 举报

发表于 2008-4-10 12:07:15 | 显示全部楼层
再提一个方案,不过,不过遇到文件名中有空格的还是会麻烦:

  1. cp `find . -name '[i]yourfile[/i]'` [i]dest-dir[/i]
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-4-11 09:56:58 | 显示全部楼层
谢谢各位~~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表