Can anyone help me to define a script which would copy multiple files one by one to multiple folders in other destination based on their shared template names?

For example

I have several folders named like 15apr01d_c_000??gr_000??sq_v0?_000??hl_0000?ex.frames (the question marks are representing different numbers)

my files are in other folder named 15apr01d_c_000??gr_000??sq_v0?_000??hl_0000?ex.frames.region_000.sum-all_000-0??.box

So what I want is to copy each respective box file to each respective folder as they share the template name 15apr01d_c_000??gr_000??sq_v0?_0000?hl_0000?ex.frames

  • Are there any serial numbers to those ??? or just random numbers? – Raphael Jan 23 '16 at 19:58
  • just random numbers – Gabe Jan 23 '16 at 19:59
  • Can you give an example to a file going to a particular folder. I mean exactly and not by using ?? – Raphael Jan 23 '16 at 20:00
  • I want to copy 15apr01d_c_00042gr_00081sq_v02_00003hl_00002ex.frames.region_000.sum-all_000-056.box which is located in /media/first4t/boxes to respective folder 15apr01d_c_00042gr_00081sq_v02_00003hl_00002ex.frames located in /media/first4t/frames – Gabe Jan 23 '16 at 20:02
  • How many files/folders are there? Do you know? – Raphael Jan 23 '16 at 20:11

The bash script you are looking for is:


#for loop

for n in {1..3500}


cp -r `find /media/first4t/boxes/ -type f -iname $(cd /media/first4t/frames && ls -d 15apr01d_c_000[0-9][0-9]gr_000[0-9][0-9]sq_v0[0-9]_000[0-9][0-9]hl_0000[0-9]ex.frames | head -$n | awk 'END{print}')* | awk 'END{print}'` /media/first4t/frames/`cd /media/first4t/frames && ls -d 15apr01d_c_000[0-9][0-9]gr_000[0-9][0-9]sq_v0[0-9]_000[0-9][0-9]hl_0000[0-9]ex.frames | head -$n | awk 'END{print}'`/


Save it as /path/to/anyname.sh & to run it do:

bash /path/to/anyname.sh
  • Ok now I am able to run the script, but instead of copying the boxes to respective folders, the script is making another copy of the frame folders with the the respective box file, I want to avoid this to make actually duplicates of frame folders – Gabe Jan 24 '16 at 15:49
  • Did you edit the script to make it run, if so specify your changes – Raphael Jan 24 '16 at 15:51
  • I played with it and it works now fine. Thank you so much for help. – Gabe Jan 24 '16 at 16:29
  • What did you edit? Could you please specigy so that I may edit my answer and then you may accept it as an answer. – Raphael Jan 24 '16 at 16:53
  • I basically commented the second half of the script which was /media/first4t/frames/cd /media/first4t/frames && ls -d 15apr01d_c_000[0-9][0-9]gr_000[0-9][0-9]sq_v0[0-9]_000[0-9][0-9]hl_0000[0-9]ex.frames | head -$n | awk 'END{print}'/ because this part was responsible to copy additionally also the frames folders in duplicate – Gabe Jan 26 '16 at 2:35

A bit of bash script can so this job quite well. A bit to write the scripts now.

Are you aware of bash script ?

Use rsync !!

  • How can I use rsync for this purpose? – Gabe Jan 23 '16 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.