0

I am trying to rename my file adding a letter at the end of the filename This is what I have so far

suffix=x
for f in *.tif
do
    mv "$f" "$f$suffix.tif"
done

when it runs I get 001.tifx.tif

How do I get the script to run and output 001x.tif ?

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • Does this answer your question? [Remove a fixed prefix/suffix from a string in Bash](https://stackoverflow.com/questions/16623835/remove-a-fixed-prefix-suffix-from-a-string-in-bash) – Han-Kwang Nienhuys Jun 25 '20 at 19:22
  • that would make it as easy as `for f in *.tif; do mv $f ${f%\.*}$suffix.${f#*\.}; done` – lab9 Jun 25 '20 at 21:57
  • Thank you this worked. Now how can you add a sequential number to filename – user1555314 Jun 27 '20 at 13:06

1 Answers1

0

Like this (perl rename command) in a shell:

$ suffix=x
$ rename -n "s/\.tif$/$suffix$&/" *.tif

Remove -n switch when the result looks good.

Output

rename(001.tif, 001x.tif)
rename(002.tif, 002x.tif)
rename(003.tif, 003x.tif)
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223