LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy files from multiple directories into one directory (http://www.linuxquestions.org/questions/linux-newbie-8/copy-files-from-multiple-directories-into-one-directory-162396/)

MadRabbit 03-25-2004 05:35 PM

Copy files from multiple directories into one directory
 
This is prolly a no-brainer but I can't seem to see the soultion ...

I have files scattered throughout numerous subdirectories, and I would like to collect them all into one single directory. What command (or script) can I run to do this? cp and cpio seem to try and make the full path under the new directory, but that's not what I want. I just want all the files in one directory.

AutOPSY 03-25-2004 06:42 PM

#!/bin/sh
echo "what files to collect:"
read collection
echo "Where do you want these:"
read destination
mv $collection $destination
exit




specifying full path-to-file names would probably work if not use a wildcard or modify the script.

or use cp.

and make sure the destination directory already exists.

if you need to specify multiple directories and files of a certain type, and it doesnt seem to work with this script for any reason you will need to cd to $directory and mv * $destination
get it.

Tinkster 03-25-2004 06:52 PM

Re: Copy files from multiple directories into one directory
 
Quote:

Originally posted by MadRabbit
This is prolly a no-brainer but I can't seem to see the soultion ...

I have files scattered throughout numerous subdirectories, and I would like to collect them all into one single directory. What command (or script) can I run to do this? cp and cpio seem to try and make the full path under the new directory, but that's not what I want. I just want all the files in one directory.

find may well be the right tool for the job,
depending on the criteria you want to apply
for the selection of files ...

find <start directory> -iname "<all my files type>" -exec cp {} <target_dir> \;

If you can tell me what the criteria are I
can make more specific examples ...


Cheers,
Tink

MadRabbit 03-26-2004 08:44 AM

Re: Re: Copy files from multiple directories into one directory
 
Quote:

Originally posted by Tinkster

find <start directory> -iname "<all my files type>" -exec cp {} <target_dir> \;



Thanks, Tink

That's just what I was looking for.

J

Tinkster 03-26-2004 03:07 PM

Re: Re: Re: Copy files from multiple directories into one directory
 
Quote:

Originally posted by MadRabbit
Thanks, Tink

That's just what I was looking for.

J
Sweet :} ...

One is glad to be of help!



Cheers,
Tink (discreetly pointing at the affero button) :}

Danicyber 01-07-2009 03:21 PM

Copy files from multiple directories into multiple directories
 
I found the solution looking at your suggestions! thank you all for saving me days of (repetitive) work!

Using the suggestions, I figure out how to copy directories from multiple directories, saving them into new multiple directories with part of the name maintained!

Ex: You have many directories with name d7q32. They are inside other directories with different names (ex: blabla/bleble/d7q32, bobo/bebe/d7q32 and so on). You got to find only the information about d7q32, also keeping part of their directory tree name. Linuxly speaking, find . -type d -name "d7q32". But now you must copy it to a new place, so you want to execute cp on top of this search keeping the parents' directory structure, saving this structure in a new directory called mynewdir. The final {} \ is just find/exec stuff, don't worry.

find . -type d -name "d7q32" -exec cp -rf --parents {} mynewdir \;

bustrofedico 11-29-2012 01:41 PM

Thank you so much. Exactly what I needed!

David the H. 11-30-2012 10:39 AM

Please think carefully before re-opening old threads like this. It's usually discouraged unless you have something substantial to add to that discussion (e.g. corrections or updated information).

Also please state clearly when you do so, so that others who read it don't accidentally reply to a poster who is no longer active.

Now to supply my own addition to the discussion, here are a couple of useful links on using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

slacker9876 02-07-2014 07:56 PM

Still works 10 years later on MacOSX too. Thank you brother!

Quote:

Originally Posted by Tinkster (Post 839367)
find may well be the right tool for the job,
depending on the criteria you want to apply
for the selection of files ...

find <start directory> -iname "<all my files type>" -exec cp {} <target_dir> \;

If you can tell me what the criteria are I
can make more specific examples ...


Cheers,
Tink



All times are GMT -5. The time now is 08:26 PM.