How to change the name and extension of a file. Or better: many at once

rename and extension a file

Sometimes we are presented with a file with a name that we want to change. At other times, what we want to change is its extension. This is something very simple in practically any operating system, but things already change if we want to do it many times. Which is the best option? Like many other types of tasks in Linux, the best (although not always) in these cases is to do it from the terminal or create a script to automate the process of change the name and extension of one or many files at the same time.

But before continuing I would like to explain something: changing the extension of a file is not the same as converting it. The only thing that we will be doing when changing the extension to a file will be something like "change the last name", but the file will remain the same. This means that, for example, if a photo is saved in PNG format, changing the extension to something .jpg will not make it JPG. We can check this by right clicking and looking at the file information. What can be used to change the extension of a file? There are cases in which it is a good idea.

Change the name and extension of a file. What is the best?

I'm not a great comic book reader, but I do have Dragon Ball comics. Some time ago I got the Dragon Ball and Dragon Ball Z files and in my research I realized something: CBR (Comic Book Reader) files are basically a ZIP with an extension that makes them open in software that is used to read comics. In other words, to a comic that comes compressed in ZIP and all the images are inside (without being inside a folder) we can change the extension to .cbr and it will directly be compatible with comic readers. This is a perfect example to understand what this post is about.

For a file: F2

If what we want is to change the name and extension of an isolated file, the easiest thing is to do right click on it and choose «Rename», "Rename" or whatever you put in your Linux distribution. In many operating systems, the option to rename is F2, but there are also many computers, especially laptops, where F2 is "caught" by some function (on my laptop it activates airplane mode). If this is your case, to rename a file you must use Fn + F2.

If we only change the name, it will not tell us anything, but if we change the extension, some operating systems will ask us if we want to change / add a different extension, to which we must say yes or accept.

With the mv command

The mv command comes from "move", but it also has the ability to rename. The command would look like this:

mv /ruta/al/archivo/origen.ext /ruta/al/archivo/destino.ext

From the above we have to:

  • mv is the order.
  • / path / to / file are the paths, the first the one of the original file and the second the one of the file that we will save with a new name and extension.
  • .Ext. is the example that I wanted to use to define the "extension".

We must bear in mind something important: there are directories in which we cannot make changes because they are protected. If we want to save a file in a protected directory we must use "sudo mv".

With the rename command

El rename command it is somewhat more powerful than the previous one because it has more options available. If your Linux distribution does not have it installed by default, it can be installed with the command sudo apt install rename. The option that interests us is the «s» (from «substitute», to substitute) and the command would look like this:

rename 's/nombreviejo/nuevonombre/' archivo1.ext archivo24.ext

This command also allows us to change the name and extension of many files, for which I recommend going into the directory where the files are from the terminal. For this we will write these commands, as long as the files are on the desktop:

cd /home/pablinux/Escritorio
sudo rename 's/.jpg/.png/' *

Before "'s" we can add the options:

  • -v: it will show us a list of the renamed files together with the new names.
  • -n: will do a simulation that will only show the files that will be changed, but does not touch them.
  • -f: will force the original files to be overwritten.

With Inviska Rename

Inviska Rename is a software specially designed for mass renaming of files and directories. It is cross-platform and available for Linux. Once open, we will do the following

Inviska Rename

  1. The first thing we will see is a view of our personal folder. I have opened my desktop folder and what you have in the previous screenshot appears.
  2. First we will choose the name, in the «Name» tab.
    • Replace name with X
    • Replace the text X with Y.
    • Insert the text X at position Y.
    • Insert X at the beginning (we can check the box on the right to add it to the end).
    • Trim at position X.
    • Trim N characters to the left (we can check the box on the right so that it is trimmed to the right).
  3. In the second tab (Extension) we can choose the extension. The options are the same as in the previous tab.
  4. And in the third, the numbering. If we don't want to add a number, we leave it by default. If we want to add a number to it, we can do it at the beginning, at the end or in a certain position. It would look like this:

Inviska Rename

  1. We click on «Rename».
  2. In the notice we click "Ok".
  3. If everything has gone correctly we will not see any message. Now it only remains to check that the files have been converted with the name that we have indicated and in the path that we have configured.

Have you tested if your operating system does it by default?

This question seems important to me. For example, Kubuntu does not, but Ubuntu does allow many files to be renamed at the same time. To do this, simply select several files, right-click and choose the "Rename" option. Something similar to what Inviska Rename offers us will appear, but simpler. In any case, I have used it and it has served me well.

It seems important to me to remember again that everything we have mentioned in this post it's about "renaming", not "converting." To convert a file or several to other formats (another extension), you would have to use a system for each type of file. For example, to convert all JPG images in a directory to PNG with the ImageMagick tool we would write the following, something that we have in more detail on our sister blog ubunlog:

for file in *.png; do convert $file -resize 830 primera-$file; done

Do you already know how to change the name and extension of a file with your Linux PC?


A comment, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   eric said

    Let's say I'm doing a job and I'm adding pages or changing the order of some ... With what program can I see the image of the document to quickly identify it and at the same time change the name (or number)?
    Prové combining "rename" and gimp but it is too tedious.