Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Suppose I have a folder with thousands of photos named all randomly. How can one rename them as photo1, photo2,...,photo1000 from the command-line/terminal?

share|improve this question
    
If it's for Photos ( and not other kind of files ), then ExifTool is the tool. –  Vardhan May 24 at 3:10
add comment

2 Answers 2

up vote 10 down vote accepted

I will assume that you want to keep a proper suffix on the filenames:

c=1; for f in *.jpg ; do mv "$f" "photo$c.jpg" ; c=$(($c+1)) ; done

Notes

  • c=1: This initalizes the counter. You can set it to any number you like.

  • for f in *.jpg ; do: This signifies the beginning of a shell for-loop. While much of shell-scripting can be difficult to make work when file names can contain spaces, newlines or other difficult characters, this construction is safe against even the most hostile file names.

  • mv "$f" "photo$c.jpg": This uses the counter c and does the actual renaming of files. The file name $f is in double-quotes to protect the name from the various possible shell expansions.

  • c=$(($c+1)): This increments the counter for the next loop

  • done: The signifies the end of the for loop.

share|improve this answer
1  
mv "$f" "$(printf 'photo%04d.jpg' $c)" will give all of the files a 4-digit, zero-padded number so that they will sort correctly. –  Sammitch May 24 at 0:50
add comment

You can use this code :

for i in *.jpg; do let j+=1 ; mv "$i" "photo$j.jpg" ; done
share|improve this answer
1  
You may want to put $i inside double-quotes to protect it from word splitting in case his file names have spaces or other difficult characters in them. –  John1024 May 23 at 19:08
    
@john1024 , that correct , thank you –  nux May 23 at 19:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.