How to Rename a File on Ubuntu 22.04


Renaming files is one of the key activities of file management in Ubuntu. Starting from organizational consistency and security to ease for users, renaming files has a significant contribution to overall system management.

Ubuntu provides extensive support to rename the files, i.e., commands, GUI, and TUI. Today, we will explore the possible methods to rename files on Ubuntu 22.04:

How to Rename a File on Ubuntu 22.04

Ubuntu has the mv and the rename command to rename the file on server/desktop support. Whereas the Ubuntu Desktop does offer the GUI method to rename the file. Moreover, you can also get a terminal file manager to rename files on Ubuntu 22.04. Let’s start with the first method:

mv Command

Primarily, the “mv” command is used to move the files/directories. However, if the file already exists at the target then it would be overwritten/renamed. This section enlists the basic working of the “mv” command with possible examples to rename a file.

Syntax:

mv <Options> <Source> <Target>

The “Source” represents the file to be renamed and the “Target” where the renamed file will be placed.

Options:

  • -i: Confirm before overwriting an existing file.
  • -b: Create a backup of the target file.
  • -u: Moves only if the source is updated/newer than the target or the target is empty.
  • -n: Do not overwrite the existing target.

Tip: Keep the source and target the same if you want to rename a file in the current directory of the source file.

Example 1: Rename a Single File

The example command below renames the file in the current location of the file (as the source and target are the same):

mv -i ~/genie.txt ~/updatedgenie.txt

Example 2: Rename Multiple Files

The mv command can be used in a looping manner to rename multiple files. The for loops match the specific condition and keep on renaming the filenames until the condition is no longer true in the specific case. Here is an example of the for loop and mv command to rename multiple files:

for x in *.jpg; do mv -- "$x" "genie_${x}"; done
  • The “x” stores each filename of the file matching the “*.jpg”.
  • The “do” part of the command executes the mv command and renames the current/matched file name(s) with the new file name (genie_x{x}).

The “ls” command is used before and after the rename command to check/verify whether files have been renamed or not.

Note: If you have to rename multiple files as routine work, you can use the for loop in the scripts.

rename Command

The rename command renames single as well as multiple files. The rename command uses the expressions to make the search and renaming more effective. However, this command does not come by default. Before using it, let’s install it:

sudo apt install rename

Syntax:

rename <Options> <Expression> <File(s)>

Options:

The supported options can be listed:

Example 1: Rename a Single File

The substitute expression finds the specific text in the filename and replaces it with the other. Here, the “updated” text is traced in the filename which will be replaced with the “linux” word. The target file is “updatedgenie.txt”:

$ rename 's/updated/linux/' updatedgenie.txt

If you want to rename the file name character by character then use “y” in place of the “s” in the above command. Here’s an example of such a scenario where the “l”, “i”, and “n” will be replaced with the “u”, “b”, and “u” characters respectively:

rename 'y/lin/ubu/' linuxgenie.txt

Example 2: Rename Multiple Files

The mv command can be used directly to rename multiple files having the same extension or matching a specific case in their names. Here are a few use cases of renaming multiple files using the rename command:

  • Use Case 1: Replace a Specific String

You can match the set of characters (string) and replace it with any string. For instance, the following command replaces the string “genie” with “expert” in all the text file names:

rename 's/genie/expert/' *.txt

  • Use Case 2: Change the File Extension

The command below changes the extension of all the pdf files to txt:

rename 's/\.jpg$/.pdf/' *.jpg

  • Use Case 3: Change the Letter Case

The renaming can also be done by changing the letter case of the characters. For instance, the below command changes all the characters of the lower case to upper case:

rename 'y/a-z/A-Z/' *

That’s how you can rename single/multiple files using the rename command.

File Manager | GUI

By default, Ubuntu offers the Nautilus File Manager which is effective enough to rename single/multiple files(s). Here’s the process:

  1. Rename Single File

Navigate to the location of the file and right-click on it. Select “Rename”:

Choose the name and the file will be renamed:

Note: You can use the shortcut key “F2” to get the “Rename File” prompt.

  1. Rename Multiple Files

Select multiple files, right-click, and then click “Rename” (or press “F2” after selecting). Here, you have two options:

  • Rename using a template

This option lets you choose/type any text/name in the text field and all the selected file names will be replaced with that selected text.

As an example, we selected two files and chose the text “errors”. The filenames before and after renaming also appear, as shown below.

  • Find and replace text

This option allows you to find the specific text in the file name(s) and replace it with some other. For instance, we selected two files and searched for “genie” text in their names and finally replaced it with “rename”. The file names before and after the renaming processing also appear:

That’s how you can rename files using the file manager.

Additional Method: Rename a File Using the Terminal File Manager

Ubuntu supports various Terminal User Interface (TUI) utilities to manage the operations, such as, nmtui to manage the Network Manager using a TUI. Similarly, “vifm” is the VIM-inspired TUI-based file manager. It allows you to rename the files effectively. It is not installed by default. Thus, first install it using the command:

sudo apt install vifm

Note: Use the up and down keys to navigate inside the directory. If you want to get into the directory use the “right-arrow” key and to get back to the previous directory, use the “left-arrow” key.

Example

Once installed, use the “vifm” command to invoke a TUI-based file manager as seen below:

To rename any file, navigate to its location and press the “c” key. Upon pressing the key, a new set of options appears on the right side, as seen below. Now, look for the key that refers to the “Renaming Files”, i.e., “w”:

When you press “w”, a rename prompt appears at the footer of the window. Choose the name you want to set, i.e., we changed “Genie.jpg” to “LinuxGenie.pdf”.

Hit enter to save the changes and the changed name will appear:

Type the shortcut “:wq” to exit VIM and save the changes. So, that’s how you can rename files interactively from the terminal.

Bottom Line

Ubuntu being a diverse distribution of Linux offers multiple ways to rename files. You can use the terminal (mv, rename, or vifm) commands to rename the files. Moreover, you can also rename the files using File Manager.

The “rename” and “vifm” utilities are not available by default. So, you need to install them prior to their usage. All these methods (alongside the prerequisites) are elaborated briefly to rename files on Ubuntu 22.04.

Print Friendly, PDF & Email
Categories