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

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.

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

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!

Something like this should work.
ssh user#ip.addr 'find -type f -name "*.pdf" -exec cp {} ./pdfsfolder \;'

Related

Linux move files from dir to dir by name mask

I am trying to move all files with names starts with SML from directory to another.
Tried with
find /var/.../Images/ -name SML\* mv /var/.../Images/Small but doesnt work
try find /var/.../Images/ -name SML\* -exec mv {} /var/.../Images/Small/ \;
I guess you want something like this:
dir=/path/to/your/Images
mkdir -p "$dir/Small"
find "$dir" -name "SML*" -not -wholename "$dir/Small/*" -exec mv {} "$dir/Small/" \;
Since the directory you move the files to is a subdirectory of the one you seach in, you need to exclude the files already moved there. So I added -not -wholename "$dir/Small/*"
To execute a command for each found file, you need -exec .... The alternative would be to pipe your find results to a while read loop.
When using -exec, the found name can be referenced by {}.
See man find for a lot more information.

find and copy command not working in bash

This line works in the terminal, but not in a bash script:
cd /home/me/Downloads/Data/$currentYear/$currentMonth/$currentDay/
find . -name '*.wav' -exec cp {} $tempfolder \;
I'm trying to copy all the WAVE files from all the sub-directories to a temporary folder
So, I solved it. Turns out, that the
cd /home/me/Downloads/Data/$currentYear/$currentMonth/$currentDay/
was not actually changing the directory for the
find .
to work. The script was trying to find the files in its own directory. Once i wrote find "$absolutePath" -name '*.wav' -exec cp {} "$tempfolder" \; it worked

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?
Here is what I have so far:
find -type d -name "*.ABC" -exec {} /Desktop/NewFile \;
I get this as output:
find: './GAE/.ABC: PERMISSION DENIED
Please Help, Thanks!
Your error here above has nothing to do with file read permission. You're trying to execute the directories you find! Avoid running commands as root or sudo unless: (1) you really need it and (2) you really know what you're doing. Quite often people asking for root or sudo privileges are exactly the ones should not have it.
That said... there are several ways to copy a directory tree under *nix. This is just one possible approach:
$ find <start> -type d -name \*.ABC -exec cp -av {} <target> \;
Where:
<start> is a directory name. It's used to tell find where to start its search (for example /usr/local or $HOME)
<target> is another directory name to define the final destination of your copied directories
UPDATE
In case you want to search for multiple paths...
$ find <start> -type d \( -name \*.ABC -o -name \*.DEF \) -exec cp -av {} <target> \;
This should work:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
For more info, you can look up here.

Newbie-ish error: cp: omitting directory

Pulling my hair as I'm stuck with a basic error without understanding why:
find . -type f -exec cp del {} \;
We're in a "test" directory, in which I created one "del" subdirectory. The "test" directory contains a variety of files of various types.
The result is a series of lines (same number as the number of files present in the directory from where the command is ran) with:
cp: omitting directory `del'
Possibly useful details follow.
Debian Wheezy, standard shell interface.
As a prelude to more complex exclusion and exec patterns I wanted to start with this fundamental test... and had this.
I think I excluded the "del" directory with "type -f", so it's not as if I was asking Linux to move a directory within itself.
There are no other directories or subdirectories.
Permissions: everything belongs to the current user.
I made variations for the "cp del" part, putting it in simple or double quotes, or using ./del, no difference.
I also tried with -R
find . -type f -name '*script1*' -exec cp -R ./del {} \;
That gave:
cp: cannot overwrite non-directory `./script1' with directory `./del'
Same with -r
If what you're trying to do is to copy some files found by find command to the del directory, then you can do it like this:
find . -type f | xargs cp -T del/
Or like this:
find . -type f -exec cp {} del \;

Create file in Linux and replace content

I have a project in Linux. I want to create a file named index.html in all folders.
So I have used the following command:
find . -type d -exec touch {}/index.html \;
It's working! Now I'm trying to copy the existing file from a given location and it to be automatically replaced into all the folders of my project.
This should actually work exactly in the same way:
find . -type d -exec cp $sourcedir/index.html {}/index.html \;
If I understand your question correctly, what you want is to copy a given file in all the directories.
You can use a similar find command :
find . -type d -exec cp -f /tmp/index.html {} \;
where /tmp/index.html is path to the original file (replace it with your own path).
Also, you don't need to create the files if your final objective is to replace them with the original file.
tar -cvzf index.tar.gz `find . -type f -iname 'index.html'` && scp index.tar.gz USER#SERVER:/your/projec/root/on/SERVER && ssh USER#SERVER "tar -xvzf index.tar.gz"
Or if you're in the proper directory localhost, and rsync is available:
rsync -r --exclude='**' --include='**/index.html' . USER#SERVER:/your/projec/root/on/SERVER
HTH

Resources