1

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!

heemayl
  • 91,753
steve
  • 331
  • you say you copy amazon_gfdl is copied into 1, 2, 3, and 4. Do you mean all the files and subdirs of amazon_gfdl are copied 4 times? Or are there only files to copy? Can you just link with ln the files or do they need to be separate copies? – meuh Aug 02 '15 at 14:30
  • I would like to have the full folder e.g. "amazon_gfdl", copied as a separated copy in each "1" , "2", "3", "4" subfolders. Therefore "amazon_gfdl" shall be copied 4 times. – steve Aug 02 '15 at 14:35
  • when you say amazon_gfdl1 is moved to the new directory "1", do you mean the contents of amazon_gfdl1, or does amazon_gfdl1 become a subdirectory of "1"? – meuh Aug 02 '15 at 14:54
  • I mean that the subfolder "amazon_gfdl1" become a subdirectory of "1" – steve Aug 02 '15 at 15:17

2 Answers2

1

You can run this script, it does nothing but echo the commands. When it does what you want, replace the dont=echo by dont= ie nothing.

dont=echo
for dir in */*
do if ! [[ "$dir" =~ [1234]$ ]]
   then file=${dir/\//_}
        for i in 1 2 3 4
        do $dont mkdir $dir/$i
           $dont cp -r $dir/$file $dir/$i
           $dont mv $dir$i $dir/$i
        done
   fi
done
meuh
  • 3,211
  • after several adjustment considering root_directory; your answer does the job. Thank you so much! – steve Aug 02 '15 at 15:42
0

You should try writing something similar to this:

for dir in */; do
  echo "Moving into $dir"
  cd "$dir"
  for subDir in */; do
    echo "Moving into $subDir"
    cd "$subDir"
    for num in 1 2 3 4; do
      echo "Moving $dir_$sudDir$num to $num"
      mv -vRi "$dir_$sudDir$num" "$num"
    done
  done
done

I have not tested this, and don't recommend running it until you have tested it. Some mv flags won't work on Mac OSX because of the version of bash it runs - but it should work on Ubuntu.

Again - mess around with the above code until it does what you want - you might want to look into bash for loops in general, or possibly using a find command to execute a similar block of code in one line.

Related: How do I rename a directory via the command line?