Showing posts with label rename. Show all posts
Showing posts with label rename. Show all posts

Saturday, July 5, 2008

How to rename a set of files using ruby

While switching to Ubuntu from Debian, in order to use the same home directory partition, I had to rename old configurations files to a new one so that ubuntu can freely create its own new configurations. At the same I was not willing to delete the old configurations files.

This is how it was done using ruby

  1. #!/usr/bin/ruby
  2. dir = "/mnt/home/suresh"
  3. files = Dir.entries(dir)
  4. files.each do |f|
  5. next if f == "." or f == ".."
  6. if File.fnmatch('.*',f) then
  7. puts f
  8. newname = f + ".olddeb"
  9. File.rename(f,newname)
  10. end
  11. end
Comments:
Line 5: To ignore the current and parent directories
LIne 6. I wanted to rename only files/directories starting with . (hidden files in unix)
Line 7. prints the current files - just for information
Line 8. the new name is old one with a .olddeb extension
Line 9. Does actual renaming