Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have many files from b_in00 to b_in79, and I need to move these files to folders 00 to 79, with b_in00 going into folder 00, and so on. Could you guide me?

share|improve this question

2 Answers 2

In bash:

mkdir {0,1,2,3,4,5,6,7}{0,1,2,3,4,5,6,7,8,9} 80 
for i in `seq -w 0 80`; do mv b_in${i} $i; done

-w means apply padding to the left such that 1 becomes 01

Alternatively:

mkdir {00..80}
for i in {00..80}; do mv b_in$i $i; done

Or even

for i in {00..80}; do mkdir $i; mv b_$i $i;done
share|improve this answer
1  
{00..80} will work in both the commands that you used. –  zmode Nov 6 '12 at 21:37
    
I don't want to move 00..80 to all of 00..80, and afaik it's not possible to tell 00 to move to 00 only and 01 to 01 and so on using globs like that. ...ah You meant instead of seq and as in mkdir {00..80}. Nice. –  Ярослав Рахматуллин Nov 6 '12 at 21:41

There are many ways to do this. Personally, I would use a shell script.

In bash it would look like this:

i=0
while [[ $i -le 80 ]]
do number=$(printf %02i $i)
    mkdir $number &&
    mv b_in$number $number
    i=$((i+1))
done

or the single-line version

i=0; while [[ $i -le 80 ]]; do number=$(printf %02i $i); mkdir $number && mv b_in $number; i=$((i+1)); done

An alternative approach is to take all files, strip the leading b_in and use the remaining string as the directoryname:

for filename in b_in*
do
    dirname=${filename#b_in}
    mkdir $dirname &&
    mv $filename $dirname
done

Things that can go wrong and that I assume you check before you run this script:

  • There are no spaces or special characters in filenames
  • The directories can be created and don't conflict with existing files or directories
share|improve this answer

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.