creating directory from filename and move bash

There's no whitespace allowed around the = in an assignment.

dir="${file%%.*}"

Conversely, whitespace is required in a test.

if [ -e $dir ]; then
    ^       ^

As far as stylistic improvements, it doesn't hurt to do an unnecessary mkdir -p, so you can get rid of the if statement.

Quotes aren't required in an assignment, so you can remove them from the dir= line. Quoting is a good idea everywhere else though, so don't delete the other quotes.

It might be good to add an extra .* to the for loop. That way if you run the script more than once it won't try to move those newly-created sub-directories. And a neat trick (though not necessarily an improvement) is to shorten BR*.* W0*.* to {BR,W0}*.*.

for file in {BR,W0}*.*; do
    dir=${file%%.*}
    mkdir -p "$dir"
    mv "$file" "$dir"
done

Tags:

Linux

Bash