1

I have multiple files in a directory like 1.jpg, 2.jpg, 15.jpg etc. I want to add a specific value to these names -for eg: 10, so that the files are renamed to 11.jpg, 12.jpg, 25.jpg etc. Is there any way to achieve it?

(It's not bulk renaming. It's to do renaming by adding a value to the current name)

  • 3
  • please note that the proposals int the mentioned duplicates will most probably result in images being overwritten (i.e. moving 1.jpg to 11.jpg will result in 11.jpg being overwritten) –  Sep 16 '15 at 09:20
  • @JacobVlijm, imho since this question has the uniqueness of source-target filename overlaps, it is sufficiently different than the other - and the main reason that precausion is not taken to avoid overwrites. I know that there is no other differences in the answer I gave and I will remove it and upvote your comment as soon as you make the changes. –  Sep 16 '15 at 09:44
  • @adonis waitwait, the question is different... misread the question, OP wants to add a number, calculated in the file's name... Will retratc my vote. – Jacob Vlijm Sep 16 '15 at 10:02
  • @A.B. I think the question is different, this is renaming, calculating the number in the file's name... – Jacob Vlijm Sep 16 '15 at 10:05
  • 1
    @JacobVlijm ok, retracted – A.B. Sep 16 '15 at 10:08
  • 1
    @JacobVlijm I was also about to close it when I noticed the thing. We discussed this on Meta already, but I think a good solution would be to have a canonical CW post (or to add an / some CW answer(s) to the question you linked before to address also this and other cases). This way also this "category" of question could be closed in favor of the question you linked. I'm starting to agree with you on the fact that there are too many. – kos Sep 16 '15 at 10:20
  • 2
    @JacobVlijm I personally enjoy answering them, but I think that some "general enough" cases such as this one could be addressed by using a good duplicate – kos Sep 16 '15 at 10:20
  • You're right, my answer had that (pretty blatant) flaw. I upvoted adonis' answer and deleted mine, since you have a working solution already. Fixing the flaw would probably take too much effort, and the solution probably wouldn't be as smooth as adonis' regardless. – kos Sep 16 '15 at 11:08

1 Answers1

4

You can use:

mkdir new
for f in *.jpg; do
    mv "$f" "new/$(( ${f%%.jpg} + 10)).jpg";done
mv new/* .
rmdir new

it will put the new files in a temporary new folder in order to avoid overwrites.

David Foerster
  • 36,264
  • 56
  • 94
  • 147