4

I have the following folder structure

root folder
 |-al2
   |- GER.zip
   |- ENG.zip
 |-ww
   |- GER.zip
   |- ENG.zip

and so on. The folder names are random strings.

I want to sort the files in the following way:

root folder
 |-ENG
   |-al2
     |- ENG.zip
   |-ww
     |- ENG.zip
 |-GER
   |-al2
     |- GER.zip
   |-ww
     |- GER.zip

How can I automate this?

UPDATE: The folder names do not follow any kind of system. They are random strings.

2
  • 1
    Do you mean you want to alter the directory structure or simply display it differently? Your question is a bit ambiguous on that point. – jw013 Aug 22 '12 at 14:18
  • I want to alter the structure. I'm sorry if that was unclear. – pgrosslicht Aug 22 '12 at 14:22
3

Here is a simple bash script:

#!/bin/bash
for file in ./*/* ; do
    lang=${file%.zip}            # Remove the extension.
    lang=${lang#./*/}            # Remove the folder.
    mkdir -p "$lang/${file%/*}"  # Create the target folders.
    mv "$file" "$lang/$file"
done
2
  • Thank you, almost perfect. But I've meant the Folder1, Folder2 only as an example. Actually I have folder names that don't follow any kind of system. al2, ww, zen, some have numbers, some are 2 characters long, some are 3 characters long. – pgrosslicht Aug 22 '12 at 14:09
  • @pdg I've updated the answer for you - try now. I've also added some missing quotes. This method of iterating over globs with a for loop and using proper quoting is much safer than the piping find output approach. You should prefer this one as long as it works. – jw013 Aug 22 '12 at 14: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.