0

I have several folders ("amazon", "niger", "rhine",...). Inside each of them I have several subfolders ("gfdl", "hadgem", "ipsl",...). Each subfolders is composed by 5 subfolders (e.g. in "amazon", the subfolder "gfdl" is composed by 5 subfolder 'amazon_gfdl', 'amazon_gfdl1', ..., 'amazon_gfdl5'); and the others subfolders follow the same structure (e.g. in "amazon" subfolder "ipsl" is composed by 5 subfolders 'amazon_ipsl', 'amazon_ipsl1', 'amazon_ipsl2',...until 'amazon_ipsl5'. I have a huge amount of folder following the same frame of organisation. Therefore my question is the following: How can I organise each folder and subfolder in such a way that in each subfolder ("gfdl", "hadgem","ipsl",...), 4 new directories are created ("1", "2", "3", "4"); and then that the folder e.g. "amazon_gfdl" (already present in "gfdl") is copied in each of those new directories and finally that "amazon_gfdl1" is moved to the new directory "1", "amazon_gfdl2" is moved to the new directory "2", and so on! I am currently using the command cp and move within each subfolders but it´s not really efficient and I might need an extra life to end this task like that! Therefor any helps or hint will be greatly appreciated. Thanks you very much!

1

You can write a tiny script to do that for you, along these lines (adjust as needed, I'm not sure I got the story right):

> cat tst.sh 
#!/bin/bash
for river in amazon niger rhine ; do
    for name in gfdl hadgem ipsl ; do
        for count in 1 2 3 4 ; do
            mkdir ${river}/${name}/${count}
            cp -a ${river}/${name}/${river}_${name} ${river}/${name}/${count}
            mv ${river}/${name}/${river}_${name}${count} ${river}/${name}/${count}
        done
    done
done

Before running it:

> find . | sort
.
./amazon
./amazon/gfdl
./amazon/gfdl/amazon_gfdl
./amazon/gfdl/amazon_gfdl1
./amazon/gfdl/amazon_gfdl2
./amazon/gfdl/amazon_gfdl3
./amazon/gfdl/amazon_gfdl4
./amazon/gfdl/amazon_gfdl5
...

Result:

> chmod u+x tst.sh
> ./tst.sh
> find . | sort
.
./amazon
./amazon/gfdl
./amazon/gfdl/1
./amazon/gfdl/1/amazon_gfdl
./amazon/gfdl/1/amazon_gfdl1
./amazon/gfdl/2
./amazon/gfdl/2/amazon_gfdl
./amazon/gfdl/2/amazon_gfdl2
./amazon/gfdl/3
./amazon/gfdl/3/amazon_gfdl
./amazon/gfdl/3/amazon_gfdl3
./amazon/gfdl/4
./amazon/gfdl/4/amazon_gfdl
./amazon/gfdl/4/amazon_gfdl4
./amazon/gfdl/amazon_gfdl
./amazon/gfdl/amazon_gfdl5
...
  • After several adjustment considering name and root directory; your answer just do the job perfectly. Thank you so much you make me learn something while not loosing time! – steve Aug 2 '15 at 15:40

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.