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

后端 未结 5 1773
一向
一向 2020-11-30 23:28

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 -ex         


        
相关标签:
5条回答
  • 2020-12-01 00:05
    find /PATH/TO/YOUR/FILES -name NAME.EXT -exec cp -rfp {} /DST_DIR \;
    
    0 讨论(0)
  • 2020-12-01 00:12

    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 "glob-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.

    0 讨论(0)
  • 2020-12-01 00:15

    If you're using GNU find,

    find . -mtime 1 -exec cp -t ~/test/ {} +
    

    This works as well as piping the output into xargs while avoiding the pitfalls of doing so (it handles embedded spaces and newlines without having to use find ... -print0 | xargs -0 ...).

    0 讨论(0)
  • 2020-12-01 00:17

    This is the best way for me:

    cat filename.tsv  |
        while read FILENAME
        do
        sudo find /PATH_FROM/  -name "$FILENAME" -maxdepth 4 -exec cp '{}' /PATH_TO/ \; ;
        done
    
    0 讨论(0)
  • 2020-12-01 00:19

    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'
    
    0 讨论(0)
提交回复
热议问题