0

We have wrong file names like file1@x2.png , it should be like file1@2x~ipad.png.

Not sure if this is proper place for me to raise this question. How to write the simple bash shell script to convert such file name from the wrong to the expected.

Forrest
  • 122,703
  • 20
  • 73
  • 107
  • are you saying that you need to rename a bunch of files to include `~ipad` before the extension if that part is not there ? – c00kiemon5ter Apr 20 '12 at 10:05
  • 1
    possible duplicate of [How to do a mass rename?](http://stackoverflow.com/questions/417916/how-to-do-a-mass-rename). There are also over 3000 other questions found with the search `[shell] rename file`. – Jonathan Leffler Apr 20 '12 at 10:10

4 Answers4

3

using bash, although this can be translated to sh/POSIX easily

for file in *; do
    [[ "$file" =~ @2x~ipad\.png$ ]] || mv "$file" "${file%@*}@2x~ipad.png"
done

if files are not just pngs then use this (extension agnostic), assuming a 3 character extension.

for file in *; do
    [[ "$file" =~ @2x~ipad\.[[:alpha:]]{3}$ ]] || mv "$file" "${file%@*}@2x~ipad.${file##*.}"
done

if those files are not grouped under some dir then try to find them recursively under a specified root dir

while read -r file; do
    [[ "$file" =~ @2x~ipad\.[[:alpha:]]{3}$ ]] || mv "$file" "${file%@*}@2x~ipad.${file##*.}"
done < <(find /path/to/root/dir/to/look/under -type -f -name "*.png")
c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
  • Good answer. He also wanted `x2` converted to `2x` so something like `${file%x2.*}2x~ipad.${file##*.}` will be requred. – bdecaf Apr 20 '12 at 10:57
  • 1
    good catch, didn't see that in the question. the file form should be cleared up though. I cant keep guessing ;) – c00kiemon5ter Apr 20 '12 at 10:58
1

You don't need a script; try mmv:

mmv "*.png" "#1~ipad.png"
Andreas Florath
  • 4,418
  • 22
  • 32
0

mv file1@x2.png file1@2x~ipad.png You can use man mv for help.

brian_wang
  • 401
  • 4
  • 16
0

You can use prename (comes with perl) to do this easily for lots of files:

prename 's/x2\.png/2x~ipad.png/' *.png
Idelic
  • 14,976
  • 5
  • 35
  • 40