Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Here is an example of a series of files with a numbering suffix in the file name.

  • screen1.png
  • screen2.png
  • screen3.png
  • screen4.png
  • screen5.png
  • screen6.png
  • screen7.png
  • screen8.png

Now, if I for whatever the reason, was to rename these files so that the numbering starts from 2, I would have to rename screen1.png to screen2.png, and screen2.png to screen3.png and so on. But this would fail already at the point where I try to rename screen1.png to screen2.png as there is already a file with that name.

This is how I like to name the files when taking a series of screenshots in Windows. So I might find myself in this type of situations sometimes. I might have skipped one screenshot, perhaps one that shows some setup process that, in time, occurs before screenshot 5 but after screenshot 4. That would have to be number 5, but to save it, I would first have to rename screen5.png to screen6.png and all the files that follow, to get them in chronological order.

Rename:

  • screen1.png
  • screen2.png
  • screen3.png
  • screen4.png
  • screen5.png > screen6.png
  • screen6.png > screen7.png
  • screen7.png > screen8.png
  • screen8.png > screen9.png

Result:

  • screen1.png
  • screen2.png
  • screen3.png
  • screen4.png
  • screen6.png
  • screen7.png
  • screen8.png
  • screen9.png

File added:

  • screen1.png
  • screen2.png
  • screen3.png
  • screen4.png
  • screen5.png+
  • screen6.png
  • screen7.png
  • screen8.png
  • screen9.png

Is there a script, or a tool, or a regular expression I can use that would make my life easier? I'm sure that most of you superusers out there have been in this type of situation more than once. Would you care to share your solution to the problem?

share|improve this question

2 Answers 2

$ perl -e 'for $i (reverse 5..8) { $j=$i+1; rename screen$i.png screen$j.png }

You can reassure yourself by running this first

$ perl -e 'for $i (reverse 5..8) { $j=$i+1; print "rename screen$i.png screen$j.png\n" }'
rename screen8.png screen9.png
rename screen7.png screen8.png
rename screen6.png screen7.png
rename screen5.png screen6.png

For Windows you'd have to use " in place of ' and qq(...) in place of "...".

share|improve this answer

All you need is a file manager with "Multiple file rename" feature (all name conflicts are resolved automatically). Most of such managers allow you to edit files in external editor, where editing numbers is much faster than in file manager. At least Total Commander and Unreal Commander have such feature.

Or you can write small one-line scripts to do that by means of shell.

You can also use a standalone script for multiple file renaming like vimv, there are more similar scripts. They can be configured to use other editor, though IMO Vim suits more than any other editor for that, since it has VisIncr plugin and builtin number increment/decrement commands (:help CTRL-A and :help CTRL-X).

Another variant is to use vifm, which has support for incrementing/decrementing numbers in names of selected files (same keys as in Vim). But it's a console application and mainly for Vim-addicted people.

share|improve this answer

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.