0

I want to rename all the files in a folder with numbers in their names, like

  • Solution_01.vtu
  • Solution_02.vtu
  • Solution_03.vtu ...

to be

  • Solution_201.vtu
  • Solution_202.vtu
  • Solution_203.vtu ...

where the additional constant (here 200) can be specified.

I've tried something along the lines of that suggested in Renaming a set of files to 001, 002, ... on Linux

i=200; temp=$(mktemp -p .); for file in solution_*.vtu 
do 
mv "$file" $temp; 
mv $temp $(printf "solution_%0.3d.vtu" $i) 
i = $((i+1)) 
done

but this doesn't work for me. Thanks for any help!

Community
  • 1
  • 1

2 Answers2

0

You can try with this command:

 find . -printf 'mv %f %f\n' | grep -v '\. \.' | sed 's/u Solution_/u Solution_2/g' | while read -r i ; do $i ; done
lpg
  • 4,897
  • 1
  • 16
  • 16
  • Thank you for your reply, but this isn't quite what I was looking for as this just appends an extra 2. I.e. if I have Solution_200.vtu Solution_201.vtu I will get Solution_2200.vtu Solution_2201.vtu instead of Solution_400.vtu, Solution_401.vtu which is what I really need. – Littlemouse Feb 10 '15 at 11:58
0

If you have ruby on your Linux machine :

#! /usr/bin/env ruby

require 'fileutils'

pattern = ARGV[0] || "*.vtu"
offset  = (ARGV[1] || 200).to_i

Dir[pattern].each do |file|
  id = file[/\d+/]
  if id then
    new_id = id.to_i + offset
    new_file = file.sub(/\d+/, new_id.to_s)
    puts "#{file} -> #{new_file}"
    ## UNCOMMENT THIS LINE IF YOU WANT TO MOVE FILES:
    # FileUtils.mv file, new_file
  end
end

You can use it like this :

./rename_with_integer_offset.rb "*.vtu" 200
# solution_201.vtu -> solution_401.vtu
# solution_202.vtu -> solution_402.vtu
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124