4

I can successfully copy the file from the folder "objects" (the directory I'm in) to it's subfolder "access" with this code in Terminal.

find . -name *.pdf -exec cp {} ./access \;

But the code is also looking into the "access folder" for files to copy and I get this message

cp: ./access/cuid12368.pdf and ./access/cuid12368.pdf are identical (not copied).

Is there a way to not have it look into the "access" folder for files to copy?

4 Answers 4

1

The find command is not necessary for this operation. Use:

cp *.pdf access/.

This copies all PDF files to the access subfolder and is much prettier and simpler than the equivalent find command.

0

If you insist on using the find ... -exec cp ...\; (instead of the cp) command and don't want it to walk any subdirectory use the -maxdepth X argument.

... 0 limits the whole search to the command line arguments (=does nothing).
... 1 limits the whole search to the start directory itself (=no sub-directories).

find . -name *.pdf -maxdepth 1 -exec cp {} ./access \;

In 10.11 El Capitan the previous command failed (find: example01.pdf: unknown primary or operator) and I had to use:

find . -name "*.pdf" -maxdepth 1 -exec cp {} ./access \;
0

You can copy files from a folder into a subfolder by simply using the cp command. For example I have a directory folder1 with 10 PDF files and a folder named folder2. Copy or move the PDF files into folder2 by the following.

cd folder1
mv *.pdf folder2/

This will move the pdf files to the subfolder folder2.

1
  • The mv command above would work the same without the / after folder2. Use cp *.* folder2 to copy only all the files directly under folder1 to folder2 when inside folder1.
    – Alper
    Commented Apr 18, 2020 at 4:42
-1

Is there a way to not have it look into the "access" folder for files to copy?


Use the -path "pathname" arguments.

find . -name *.pdf -path "./access" -exec cp {} ./access \;
1
  • 1
    Wouldn't that tell it to search the folder "access" rather than not search it? Commented Apr 21, 2020 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .