67

I would like to find all the pdf files in a folder. It contains pdf files inside and more directories that contain more as well. The folder is located on a remote server I have ssh access to. I am using the mac terminal but I believe the server I am connecting to is Centos.

I need to find all the pdfs and copy them all to one directory on the remote server. I've tried about 10 variations with no luck. Both mine and the remote systems do not seem to recognise -exec as a command though exec is fine so thats a problem.

Im not sure what the problem is here but the command does not fail it just sits there and stalls forever so I do not have any useful errors to post.

cp $(find -name "*.pdf" -type f; exec ./pdfsfolder {} \; | sed 1q)

find: ./tcs/u25: Permission denied
find: ./tcs/u68: Permission denied
-bash: /var/www/html/tcs_dev/sites/default/files/pdfsfolder: is a directory
-bash: exec: /var/www/html/tcs_dev/sites/default/files/pdfsfolder: cannot execute: Success
cp: target `./runaways_parents_guide_2013_final.pdf' is not a directory

This is the last one I tried, I think I can ignore the permission denied errors for now but im not sure about the rest.

3 Answers 3

154

Try this:

find . -name "*.pdf" -type f -exec cp {} ./pdfsfolder \;
5
  • 1
    Thanks, it sat there for a while but i think that might have been to do with the permissions errors.
    – lorless
    Commented Aug 30, 2013 at 8:25
  • 2
    @user3728501 it terminates the exec expression Commented Sep 26, 2016 at 14:21
  • 4
    I would default to -iname instead of -name, since file extensions might have capital letters.
    – Martin R.
    Commented Nov 16, 2016 at 14:39
  • 1
    Can you explain this command in details? I need to modify it, and I don't understand that syntax with {} after cp.
    – Koli
    Commented Feb 12 at 14:40
  • yes what is the purpose of the backslash at the end? Commented Apr 30 at 7:52
8

Paul Dardeau answer is perfect, the only thing is, what if all the files inside those folders are not PDF files and you want to grab it all no matter the extension. Well just change it to

find . -name "*.*" -type f -exec cp {} ./pdfsfolder \;

Just to sum up!

5

Something like this should work.

ssh [email protected] 'find -type f -name "*.pdf" -exec cp {} ./pdfsfolder \;'

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.