LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-05-2009, 04:41 AM   #1
Heksie
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
cp multiple files to multiple directories


Hello

I thought I would find a solution easily but alas! The challenge: I need to copy *.cnv files from a number of folders (named Afr100, Afr101 etc and their sub-folders) to new folders that have the same name as the source. For example, from ~/data/Afr121/Processed/*.cnv to ~/newdata/Afr121. I don't want to have to mkdir for each folder (Afr100 to Afr132) and then copy the .cnv files one folder at a time. How difficult is this challenge?
 
Old 02-05-2009, 05:09 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can use rsync with the proper include and exclude options:
Code:
rsync -av --include='*/' --include='*.cnv' --exclude='*' data/* newdata
this will include all the directories, all the CNV files and exclude the rest. The creation of the directories is made by rsync automatically. A side effect is that all the directory structure is cloned into newdata, even if a directory is empty or does not contain any CNV file. If you can live with that, the trick is done!
 
Old 02-05-2009, 05:17 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
In case you cannot live with dummy directories... here is a simple alternative:
Code:
for file in $(find data -name \*.cnv)
do
  mkdir -p new$(dirname "$file")
  cp -a "$file" new$(dirname "$file")
done
 
  


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 multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 07:56 PM
tar multiple files in different directories... visitnag Linux - Desktop 1 04-13-2008 11:02 PM
List all files in multiple directories haiders Linux - Newbie 6 05-10-2007 12:05 PM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 08:16 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 05:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration