Thursday, May 2, 2024
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 555  / 2 Years ago, wed, august 17, 2022, 4:42:00

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?


More From » command-line

 Answers
5

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.



[#25047] Thursday, August 18, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ousear

Total Points: 395
Total Questions: 114
Total Answers: 89

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
;