Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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. –  pdg Aug 22 '12 at 14:22

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
    
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. –  pdg 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
    
Works perfectly now, thank you! –  pdg Aug 22 '12 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.