Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find files recursively by file type and copy them to a directory?

Tags:

linux

bash

centos

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.

like image 879
lorless Avatar asked Aug 20 '13 14:08

lorless


People also ask

How do I copy a recursive file?

In order to copy the content of a directory recursively, you have to use the “cp” command with the “-R” option and specify the source directory followed by a wildcard character.

Which command is used to copy file or directory is recursively?

4. -r or -R: Copying directory structure. With this option cp command shows its recursive behavior by copying the entire directory structure recursively.

What command can we use to recursively find all files with an INI extension?

Using Glob() function to find files recursively We can use the function glob.

How can you copy a directory recursively with all of its contents including sub directories )?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.


1 Answers

Try this:

find . -name "*.pdf" -type f -exec cp {} ./pdfsfolder \; 
like image 189
Paul Dardeau Avatar answered Sep 25 '22 14:09

Paul Dardeau