2

I'm trying to copy directories of data sets that pertain to the a certain case but the data sets are currently stored in different directories and with different (non consecutive) numbered file.

e.g. I have a case file case_directory/006 and I want to copy and create case_directory/006/MRS from where that case's MRS directory is currently stored in data_directory/150/MRS. The next case would be case_directory/010 and for that I would want to copy data_directory/155/MRS - just to illustrate there is really no relation between the numbering systems. I have about 120 of such case directories so would be great to automate the copying process from the matching data directory! :-/

Can I automate this using the for and cat commands and text files containing lists where the matching numbers are on the same row in each text file? can you use more than 1 variable with for or do I need to use a more complicated script?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Justin
  • 21
  • 1
  • 1
    Use a `while`/`read` loop: `while read num1 num2; do cp -R case_directory/$num1/MRS data_directory/$num2; done < file_with_matching_nums` instead of a `for` loop. – muru Sep 09 '15 at 21:23

1 Answers1

2

Assuming your data file looks like this

006 150
010 155
...

You can use a simple loop to read the source and destination numbers

while read ncase ndata
do
    cp -a case_directory/$ncase/MRS data_directory/$ndata/
done < casedatafile
roaima
  • 107,089
  • 14
  • 139
  • 261
  • thank you so much @muru and @roaima - worked a treat! Will be using `while read` until I'm blue in the face now! – Justin Sep 10 '15 at 12:07
  • There are [many reasons](http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice) _not_ to use `while read` without serious pause for thought. Here, though, it's a sufficient solution. – roaima Sep 10 '15 at 12:15