1

I am a beginner at shell scripting. I have 4 images:

 1.png, 2.png, 3.png, 4.png 

How do I rename these images to:

img1.png, img2.png, img3.png, img4
mklement0
  • 382,024
  • 64
  • 607
  • 775
RAHUL.S. KRISHNA
  • 221
  • 1
  • 3
  • 8
  • Did you try searching `rename file bash` ? Clearly you didn't because the first result tells you how to do this. –  May 12 '15 at 12:27

3 Answers3

2

Using perl based rename:

rename 's/^/img/' *.png
anishsane
  • 20,270
  • 5
  • 40
  • 73
1

Use this as a script with your file names as input. It is untested, but should give you a clue

#! /bin/bash    
for file in "$@"; do
     mv "$file" "img${file}"
done
NaN
  • 7,441
  • 6
  • 32
  • 51
  • You're welcome; on second thought, perhaps using `for file in *.png; do` is a more direct way of demonstrating this otherwise nice `bash` solution. – mklement0 May 12 '15 at 13:04
0

Have a look for the rename command, you can do something like

rename s/^/img/g *png

This substitutes (s/) the beginning of a file name (noted as the ^) with img for all files ending in png (*png)

.if you don't have it, you can get the command from here http://stackoverflow.org/wiki/Rename.pl

for instance

nrob
  • 861
  • 1
  • 8
  • 22
  • The Perl-based `rename` utility is a good tip, but you should link to the more robust and more powerful version at http://plasmasturm.org/code/rename/. The `g` option is pointless here, because the `^` by definition can only match _once_ per line; also, you want `*.png` to avoid false positives. – mklement0 May 12 '15 at 13:07