196

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'
Nikos Alexandris
  • 708
  • 2
  • 22
  • 36
shantanuo
  • 31,689
  • 78
  • 245
  • 403

5 Answers5

392

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 uses {} as placeholder for the matched file.

Fred Sobotka
  • 5,252
  • 22
  • 32
malcolmpdx
  • 4,132
  • 1
  • 16
  • 7
47

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 \;
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Thiyagu ATR
  • 2,224
  • 7
  • 30
  • 44
  • 1
    3. If you have GNU `find`/`xargs`, `find -print0 | xargs -0 -r cp -t `. Example: `find -mtime -1 -type f -print0 | xargs -r0 cp -t inner/` – Toby Speight May 03 '16 at 11:55
9

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 2
    In linux had to specify the target directory like so: `find /home/shantanu/processed/ -name '*2011*.xml' -exec cp --target-directory=/home/shantanu/tosend {} \;` – GuruM Aug 14 '12 at 10:17
  • 3
    This answer could be improved and shortened if the complete command was provided like in the top answer. – Engineer2021 Apr 14 '14 at 14:58
-3
for i in $(ls); do cp -r "$i" "$i"_dev; done;
Robert A
  • 337
  • 4
  • 3
-4

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

  • 3
    Hi, as question is already answered (from a while back) this isn't really a relevant answer, it also should be a comment (when you have some more rep), unless you wish to expand and explain it. Just a heads up :). – W1ll1amvl Dec 08 '14 at 00:57