Find and copy files

LinuxCopyFind

Linux Problem Overview


Why does the following does not copy the 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'

Linux Solutions


Solution 1 - Linux

If your intent is to copy the found files into /home/shantanu/tosend, you have the order of the arguments to cp reversed:

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

Please, note: the find command use {} as placeholder for matched file.

Solution 2 - Linux

i faced an issue something like this...

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

  1. If find command's output doesn't contain any space i.e if file name doesn't contain space in it then you can use below mentioned command:

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

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

  2. But most of the time our production data files might contain space in it. So most of time below mentioned command is safer:

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

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

In the second example, last part i.e semi-colon is also considered as part of find command, that should be escaped before press the enter button. Otherwise you will get an error something like this

find: missing argument to `-exec'

In your case, copy command syntax is wrong in order to copy find file into /home/shantanu/tosend. The following command will work:

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

Solution 3 - Linux

You need to use cp -t /home/shantanu/tosend in order to tell it that the argument is the target directory and not a source. You can then change it to -exec ... + in order to get cp to copy as many files as possible at once.

Solution 4 - Linux

for i in $(ls); do cp -r "$i" "$i"_dev; done;

Solution 5 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshantanuoView Question on Stackoverflow
Solution 1 - LinuxmalcolmpdxView Answer on Stackoverflow
Solution 2 - LinuxThiyagu ATRView Answer on Stackoverflow
Solution 3 - LinuxIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 4 - LinuxRobert AView Answer on Stackoverflow
Solution 5 - LinuxSebin JohnView Answer on Stackoverflow