How to Rename File in Ubuntu 24.04

Renaming files in Ubuntu 24.04 can make them easier to manage as well as find. It helps organize your files better and can be important for scripts or programs that need specific file names. Renaming files can also improve security by avoiding default names that might reveal sensitive information. In addition, it keeps your file system consistent, which is beneficial for both professional as well as personal use. Overall, renaming files is a simple but effective way to optimize file organization.

This article will explain several possible ways to rename files in Ubuntu 24.04. The supported content is given below:

How to Rename File in Ubuntu 24.04?

Method 1: Using the Command Line

  1. mv Command
  2. rename Command
  3. mmv Command

Method 2: Using the Bash Script

Method 3: Using the GUI

Conclusion

How to Rename File in Ubuntu 24.04?

Renaming files can be done through several methods. Here are all the possible ways:

Method 1: Using the Command Line

On Ubuntu 24.04, renaming a file is straightforward through the command line. Here are a couple of commands to accomplish this task:

1. mv Command:

The mv command is commonly utilized to move files, but it can also rename them. The syntax is:

mv old_file new_file

In this syntax, old_file and new_file represent the original and replaced files, respectively.

Renaming Single File

For instance, to rename a file from linuxworld.txt to new_linuxworld.txt, utilize the mv command as below:

mv linuxworld.txt new_linuxworld.txt

Renaming Multiple Files

In Ubuntu 24.04, users can also rename several files via the mv command. Here’s a simple example to rename all .txt files by adding a prefix “new_” as below:

for file in *.txt; do mv "$file" "new_$file"; done

The above output verifies that file1.txt, file2.txt has been renamed with new_file1.txt and new_file2.txt, and so on.

2. rename Command:

In Ubuntu 24.04, the rename command can also be utilized for renaming the file name. The basic usage of rename command with the syntax is given below:

rename 's/oldname/newname/' filename

The above syntax uses sed to replace “oldname” with “newname” in the specified filename.However, the rename command is not pre-installed. Therefore, user need to install it via the apt command:

sudo apt install rename

Renaming Single File

In our Ubuntu system, the old_linuxworld.txt file is already located. Let’s rename the single old_linuxworld.txt file by removing the old string with the new one:

rename 's/old/new/' old_linuxworld.txt

Renaming Multiple Files

For example, to rename all .txt files by replacing old with new in their filenames, users can utilize the rename command:

rename 's/old/new/' *.txt

In the above output, users can verify that renames old_file1.txt to new_file1.txt, old_file2.txt to new_file2.txt, and so on.

3. mmv Command:

To deal with the several files at once, the mmv command is quite useful. For this, users can utilize the mmv command to rename single or multiple files. The syntax is given below:

mmv old_file new_file

In this syntax, old_file represents the original file and new_file identifies the replaced file.The mmv command is another tool for batch renaming. It also needs to be installed:

sudo apt install mmv

Renaming a Single File

For instance, to rename file.txt to renamed_file.txt, the user can run the below mmv command:

mmv file.txt renamed_file.txt

The above command renames the file.txt to renamed_file.txt in the current directory.

Renaming Multiple Files

Users can also rename all .txt files in the directory by adding prefixes. For instance, add a “new” prefix to all files in the same directory with the help of mmv command:

mmv "file*.txt" "newfile#1.txt"

This section assists users in renaming files in Ubuntu 24.04 using the command line.

Method 2: Using the Bash Script

Renaming files using scripts can be quite efficient. Here’s a simple instruction to assist users create a script for renaming files:

Renaming a Single File

Let’s write a simple bash script for renaming files. Here’s an example script to add a prefix new to the .txt file with mv command:

#!/bin/bash 
mv file.txt newfile.txt

After saving the file as rename_file.sh, users require permit for execution via the chmod utility and run it:

sudo chmod +x rename_file.sh

./rename_file.sh

Renaming Multiple Files

If users are required to rename multiple files, they can utilize a loop. For instance, rename all .txt files by adding new at the start of files:

#!/bin/bash 
for a in .txt; do
mv -- "$a" "new_${a}";
done

This script loops through all .txt files in the current directory and adds a new prefix.

After that, save this script as rename_file.sh, then, permit execution with the chmod command, and, run it:

sudo chmod +x rename_file.sh

./rename_file.sh

Method 3: Using the GUI

Renaming a file in Ubuntu 24.04 using the GUI is a simple process. Here’s how users can do it on the File Manager:

First, right-click on the file that users need to rename. Then, pick the “Rename” option from the menu as below:

Now, users need to type the name of file such as “replaced_file.txt”, and finally hit the “Rename” button:

Alternatively, users can also select the file and press F2 by typing the new file name.

Note: Remember, only the first part of the file name is selected by default, not the file extension.

Conclusion

Renaming a file in Ubuntu 24.04 is possible through the graphical interface or the terminal. Using the GUI, right-click on the file, pick the “Rename” option, enter the name, and hit Enter. To rename a file in Ubuntu 24.04 using the command line, use the mv, rename, mmv commands by mentioning the name of the old as well as new files. These commands effectively rename the file to a new name.