find -exec cp

How to move or copy files listed by 'find' command in unix?

find / -name core-* -exec cp {} /mnt/sda/OTHER/5.1.4bld1.0_log/aim_test/ \;

 find . -name *.pdf | xargs -i cp {} ../docbook_pdf/

的写法的含义解释:

  • find . -name *.pdf :递归式的找出当前文件夹及其子文件下的所有.pdf文件
  • xargs -i xxx :其中xxx==cp {} ../docbook_pdf/,表示将输入的内容,用{}替换
    • cp {} ../docbook_pdf/ :对于每一个find出来的文件,拷贝到对应的目标文件夹中。

I have a list of certain files that I see using the command below, but how can I copy those files listed into another folder, say ~/test?

find . -mtime 1 -exec du -hc {} +
shareimprove this question
 
3  
find . -mtime 1 -exec cp {} ~/test/ \; –  user529758 Jun 28 '13 at 15:58
1  
Also, consider piping to xargs. That way, you can copy the files in batches. –  Eric Jablow Jun 28 '13 at 16:01
    
hey thanks a lot both of you –  L P Jun 28 '13 at 16:07

3 Answers

up vote 10 down vote accepted

Adding to Eric Jablow's answer, here is a possible solution (it worked for me - linux mint 14 /nadia)

find /path/to/search/ -type f -name "regular-expression-to-find-files" | xargs cp -t /target/path/

You can refer to "How can I use xargs to copy files that have spaces and quotes in their names?" as well.

shareimprove this answer
 
1  
-name takes a glob match, not a regular expression. An important distinction... –  sanmiguel Dec 18 '13 at 12:45
    
Please edit my answer in that case :) –  Ankur Kumar Dec 20 '13 at 11:14

Actually, you can process the find command output in a copy command in two ways:

  1. If the find command's output doesn't contain any space, i.e if the filename doesn't contain a space in it, then you can use:

    Syntax:
        find <Path> <Conditions> | xargs cp -t <copy file path>
    Example:
        find -mtime -1 -type f | xargs cp -t inner/
    
  2. But our production data files might contain spaces, so most of time this command is effective:

    Syntax:
       find <path> <condition> -exec cp '{}' <copy path> \;
    
    Example 
       find -mtime -1 -type f -exec cp '{}' inner/ \;
    

In the second example, the last part, the semi-colon is also considered as part of the find command, and should be escaped before pressing Enter. Otherwise you will get an error something like:

find: missing argument to `-exec'
shareimprove this answer
 

 

find /PATH/TO/YOUR/FILES -name NAME.EXT -exec cp -rfp {} /DST_DIR \;
shareimprove this answer
 
posted @ 2015-07-02 18:02  alxe_yu  阅读(847)  评论(0编辑  收藏  举报