mylesagray
mylesagray

Reputation: 8869

RegEx move and change filename

I'm trying to move a lot of files in this directory and format:

/1.12.268/1.12.268_Hi_3D.jpg

To this directory and format:

/1.12.0268/1.12.0268_Hi_3D.jpg

I have managed to run a RegEx that created all the new folders for me:

ls 1* | find . -type d | awk '{print("mkdir "$1)}' | sed 's/[0-9][0-9][0-9]*/0&/2'

But I can't work out what regex to use to create a mv statement that will move and rename all the files to their new folders?

This is as close as I have come but the folders are reversed and the files are not getting renamed.

ls 1* | find . -type f | awk '{print("mv "$1" "$1)}' | sed 's/[0-9][0-9][0-9]*/0&/2'

I get this output:

mv ./1.12.0269/1.12.269_Low_Tech.gif ./1.12.269/1.12.269_Low_Tech.gif
mv ./1.14.0410/1.14.410_hi_3d.jpg ./1.14.410/1.14.410_hi_3d.jpg
mv ./1.14.0410/1.14.410_hi_tech.jpg ./1.14.410/1.14.410_hi_tech.jpg
mv ./1.14.0410/1.14.410_low_3d.png ./1.14.410/1.14.410_low_3d.png
mv ./1.14.0410/1.14.410_low_tech.png ./1.14.410/1.14.410_low_tech.png
mv ./1.14.0845/1.14.845_hi_3d.jpg ./1.14.845/1.14.845_hi_3d.jpg
mv ./1.14.0845/1.14.845_hi_tech.jpg ./1.14.845/1.14.845_hi_tech.jpg
mv ./1.14.0845/1.14.845_low_3d.png ./1.14.845/1.14.845_low_3d.png
mv ./1.14.0845/1.14.845_low_tech.png ./1.14.845/1.14.845_low_tech.png

When I in fact want this:

mv ./1.12.269/1.12.269_Low_Tech.gif ./1.12.0269/1.12.0269_Low_Tech.gif
mv ./1.14.410/1.14.410_hi_3d.jpg ./1.14.0410/1.14.0410_hi_3d.jpg
mv ./1.14.410/1.14.410_hi_tech.jpg ./1.14.0410/1.14.0410_hi_tech.jpg
mv ./1.14.410/1.14.410_low_3d.png ./1.14.0410/1.14.0410_low_3d.png
mv ./1.14.410/1.14.410_low_tech.png ./1.14.0410/1.14.0410_low_tech.png
mv ./1.14.845/1.14.845_hi_3d.jpg ./1.14.0845/1.14.0845_hi_3d.jpg
mv ./1.14.845/1.14.845_hi_tech.jpg ./1.14.0845/1.14.0845_hi_tech.jpg
mv ./1.14.845/1.14.845_low_3d.png ./1.14.0845/1.14.0845_low_3d.png
mv ./1.14.845/1.14.845_low_tech.png ./1.14.0845/1.14.0845_low_tech.png

Any help greatly appreciated!

Upvotes: 3

Views: 1021

Answers (2)

Satish
Satish

Reputation: 17437

I have dirty way to do this. Give it a try.

ls 1* | find . -type f | awk '{print("mv "$1" "$1)}' | sed 's/[0-9][0-9][0-9]*/0&/6' | sed 's/[0-9][0-9][0-9]*/0&/8'

Upvotes: 1

rwos
rwos

Reputation: 1841

Is using rename an option? It's basically made for this kind of stuff.

If so, rename -n 's/[0-9][0-9][0-9]/0$&/g' should work for you. (The -n is just for debugging, remove it for the actual renaming).

Upvotes: 1

Related Questions