LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-25-2004, 05:35 PM   #1
MadRabbit
LQ Newbie
 
Registered: Mar 2003
Location: Canada
Distribution: Slackware 8.x, 9.x
Posts: 26

Rep: Reputation: 15
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.
 
Old 03-25-2004, 06:42 PM   #2
AutOPSY
Member
 
Registered: Mar 2004
Location: US
Distribution: Redhat 9 - Linux 2.6.3
Posts: 836

Rep: Reputation: 31
#!/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.

Last edited by AutOPSY; 03-25-2004 at 06:49 PM.
 
Old 03-25-2004, 06:52 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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
 
1 members found this post helpful.
Old 03-26-2004, 08:44 AM   #4
MadRabbit
LQ Newbie
 
Registered: Mar 2003
Location: Canada
Distribution: Slackware 8.x, 9.x
Posts: 26

Original Poster
Rep: Reputation: 15
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
 
Old 03-26-2004, 03:07 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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) :}
 
1 members found this post helpful.
Old 01-07-2009, 03:21 PM   #6
Danicyber
LQ Newbie
 
Registered: Jan 2009
Posts: 1

Rep: Reputation: 0
Lightbulb 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 \;
 
Old 11-29-2012, 01:41 PM   #7
bustrofedico
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
Thank you so much. Exactly what I needed!
 
Old 11-30-2012, 10:39 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
Old 02-07-2014, 07:56 PM   #9
slacker9876
Member
 
Registered: Apr 2004
Location: Colorado
Distribution: Ubuntu 6.10, SLED 10
Posts: 119

Rep: Reputation: 15
Still works 10 years later on MacOSX too. Thank you brother!

Quote:
Originally Posted by Tinkster View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 11:51 AM
Copy files from different directories fiomba Linux - Software 3 02-11-2005 03:01 AM
Copy one file to multiple directories cmfarley19 Linux - General 9 11-10-2004 12:57 PM
find and copy files into multiple directories avargas22 Linux - Newbie 2 04-01-2004 11:11 AM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 05:25 PM


All times are GMT -5. The time now is 09:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration