Home » Questions » Computers [ Ask a new question ]

Bizarre mv operation

Bizarre mv operation

The use of a renaming program severely screwed up some of my files. I'm trying to get them back in order.

Asked by: Guest | Views: 232
Total answers/comments: 1
Guest [Entry]

"Try this piece of shell magic:

for i in *.*; do
BASE=$(basename ""$i"" .ext2)
LIST=*/""${BASE}"".*
DIR=$(dirname ""${LIST}"")
mv -n ""$i"" ""${DIR}""
done

Or as an oneliner:

for i in *.*; do mv -n ""$i"" ""$(dirname */""$(basename ""$i"" .ext2)"".*)""; done

Before:

whitequark@forth:~/test$ ls *
test 1.ext2 test 2.ext2 test 3.ext2

f 1:
test 1.ext

f 2:
test 2.ext test 3.ext

After:

whitequark@forth:~/test$ ls *
f 1:
test 1.ext test 1.ext2

f 2:
test 2.ext test 2.ext2 test 3.ext test 3.ext2

The -n switch will protect you from overwriting anything."