Search and copy files

Why doesn't the following copy files to the destination folder?

# find /home/shantanu/processed/ -name '*2011*.xml' -exec cp /home/shantanu/tosend {} \; cp: omitting directory `/home/shantanu/tosend' cp: omitting directory `/home/shantanu/tosend' cp: omitting directory `/home/shantanu/tosend' 
+111
linux find copy
Mar 09 '11 at 5:12
source share
5 answers

If you intend to copy the found files to / home / shantanu / tosend, you have the order of the arguments for cp:

 find /home/shantanu/processed/ -name '*2011*.xml' -exec cp {} /home/shantanu/tosend \; 

Note: the find command uses {} as a placeholder for the corresponding file

+246
Mar 09 '11 at 5:19
source share

I ran into a problem something like this ...

In fact, in two ways you can handle the output of the find in the copy command

  • If the output of the find does not contain a space. If the file name does not contain a space, you can use the command below:

    Syntax: find <Path> <Conditions> | xargs cp -t <copy file path> find <Path> <Conditions> | xargs cp -t <copy file path>

    Example: find -mtime -1 -type f | xargs cp -t inner/ find -mtime -1 -type f | xargs cp -t inner/

  • But most of the time, our production data files may contain spaces in it. Therefore, most of the time below the specified command is safer:

    Syntax: find <path> <condition> -exec cp '{}' <copy path> \;

    Example find -mtime -1 -type f -exec cp '{}' inner/ \;

In the second example, the last part of the ie half-colony is also considered as part of the find , which must be escaped before pressing the enter button. Otherwise, you will receive an error message

 find: missing argument to `-exec' 

In your case, the copy command syntax is incorrect to copy the search file to /home/shantanu/tosend . The following command will work:

 find /home/shantanu/processed/ -name '*2011*.xml' -exec cp {} /home/shantanu/tosend \; 
+29
Dec 07 '13 at 18:18
source share

You need to use cp -t /home/shantanu/tosend to say that the argument is the target directory, not the source. Then you can change it to -exec ... + to get cp to copy as many files at once.

+7
Mar 09 2018-11-11T00:
source share
 for i in $(ls); do cp -r "$i" "$i"_dev; done; 
0
Jul 07 '19 at 12:02
source share

The reason for this error is that you are trying to copy a folder that requires the -r option also for cp Thanks

-2
Dec 08 '14 at 0:30
source share



All Articles