Question

I have a lot of pictures in a folder named like this:

foo.png
foo.png.~1~
foo.png.~2~
and so on

I want them to be named like

foo.png
foo1.png
foo2.png
and so on

How can I do this? I am using Ubuntu Server 13.04

Thanks!

-Yes, I searched before posting, but could not find anything that was helping me.

Was it helpful?

Solution

You loop over each file name with a bash script like this:

#!/bin/bash
for f in *
do
  name=(${f//./ })      # split the filename on period. Note - the space matters!
  number=(${f//\~/ })   # similar trick to find the number between tildes
  newName=${name[0]}${number[1]}${name[1]}       # construct the new name from array elements
  echo "now you can rename " $f " to " $newName  # print out the new name as a check
done

I deliberately left out the "rename" command, replacing it with an "echo" instead. See if this does what you wanted - then change the echo line to

mv $f $newName

You might want to copy the files to a new directory, rather than doing a bulk rename, until you are sure things are behaving. I would not want to be responsible for a bunch of files getting overwritten or otherwise mangled. Of course this would be something like

mv $f newDirectory/$newName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top