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?

  • 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 9 '15 at 21:23
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
  • 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 not to use while read without serious pause for thought. Here, though, it's a sufficient solution. – roaima Sep 10 '15 at 12:15

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.