LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cp multiple files to multiple directories (https://www.linuxquestions.org/questions/linux-newbie-8/cp-multiple-files-to-multiple-directories-702445/)

Heksie 02-05-2009 04:41 AM

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?

colucix 02-05-2009 05:09 AM

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! :)

colucix 02-05-2009 05:17 AM

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



All times are GMT -5. The time now is 05:13 PM.