How to Rename a File in Ruby

Oluwafisayo Oluwatayo Feb 02, 2024
  1. Why You Should Rename Files
  2. Rename Files in the Same Directory in Ruby
  3. Rename Files from Stating Directory in Ruby
  4. Rename Multiple Files in Ruby
How to Rename a File in Ruby

There is no bigger haystack than the computer system. Have you ever tried looking for one file on your system, and it ended up becoming an uphill task just because you didn’t name the file correctly or failed to name the file?

Why You Should Rename Files

For someone who’s used to downloading content on the internet, you will notice that you sometimes download content, and the filename is nothing like what you want it to be.

It’s best to rename such a file to a name that suits your style because you might end up searching for the file endlessly, bypassing the file, and you won’t even know.

Also, it makes it easy to search for that content using the file’s search engine when it is named correctly.

Renaming files correctly also helps you keep an organized file system in Ruby.

Renaming files in Ruby is pretty easy, and depending on what you want to achieve, various methods meet every need.

Rename Files in the Same Directory in Ruby

In a situation where you are in the exact directory where the file you want to rename is, you can easily use Ruby inside your terminal.

All you need to do is press down the Shift key on your keyboard, then right-click, you will see Open PowerShell window from here, click on that, and it will open a PowerShell terminal in that directory.

Then type in irb to activate Ruby, then you type in the snippet of code below and press Enter.

File.rename('out.txt', 'in.txt')

Once you do this, your file should be renamed.

rename from file directory

Rename Files from Stating Directory in Ruby

This particular method allows you to rename files once you know the file’s path; this can be done from the terminal or by using a code editor.

To do this, create a new file, name it new.rb and type in this snippet:

new.rb:

File.rename('C:/Users/HP/Downloads/csvv/outfile.txt', 'C:/Users/HP/Downloads/csvv/out.txt')

What we have done here is specify the path of the old file, then specify that same path with the new name we want to give the file.

rename files from stating directory

Aside from the fact that we can rename a file, we can also rename its extension. In this example, we can specify the new path and change the .txt extension to .doc, which will work fine.

rename file extension

But be mindful that you should change the extension to one that will still work with the file because it is only renaming and not converting the file, so if you try to change the extension to .pdf, the file will not open.

Rename Multiple Files in Ruby

What if you have a bunch of files you want to rename? Imagine the rigors of renaming each file one by one when you can do that with a snippet of Ruby codes.

Let us open a new file, name it new.rb, then type in these codes:

new.rb:

Dir.chdir('/Users/HP/Downloads/csvv') do
  unless Dir.glob('*.{txt}').empty?
    Dir.glob('*.txt', File::FNM_DOTMATCH).each_with_index do |_file, index|
      File.rename(Dir.glob('*.txt', File::FNM_DOTMATCH)[index], "new name#{index}.txt")
    end
  end
end

We want to specify the files we want to rename, so we target them using their extension. We run this code, and we will see each file renamed.

rename multiple files

Ruby surely offers every means to rename files, and its multiple-file renaming method is a lifesaver.

Oluwafisayo Oluwatayo avatar Oluwafisayo Oluwatayo avatar

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.

LinkedIn