Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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
{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:

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

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*
    mkdir $dirname &&
    mv $filename $dirname

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .