How to rename file in linux?

Renaming a file in Linux is a common task that can be easily accomplished using the command line. In this article, we will explain how to rename a file in the different operating systems of Linux, including Ubuntu, Debian, CentOS, and Fedora.

Before we get started, it’s important to point out that we understand that Linux is a multi-user operating system, as a result, you may need to use the “sudo” command to gain the necessary permissions to rename a file.

How do you rename a file in Ubuntu and Debian?

In Ubuntu and Debian, you can use the “mv” command to rename a file. The “mv” command stands for “move,” but it can also be used to rename a file. Here’s how to use it:

  1. Open a terminal window.
  2. Navigate to the directory that contains the file you want to rename. You can use the “cd” command to change directories. For example, if the file is in the “Documents” folder, you would use the following command:
cd Documents

Use the “ls” command to list the files in the current directory. This will allow you to verify the name of the file you want to rename.

To rename the file, use the “mv” command followed by the current file name and the new file name. Here’s an example:

mv old_file_name.txt new_file_name.txt

In this example, we are renaming a text file called “old_file_name.txt” to “new_file_name.txt”.

How to rename a file in CentOS and Fedora?

In CentOS and Fedora, you can use the “mv” command to rename a file, just like in Ubuntu and Debian. The process is the same, with just a few minor differences. Here’s how to rename a file in CentOS and Fedora:

  1. Open a terminal window.
  2. Navigate to the directory that contains the file you want to rename. You can use the “cd” command to change directories.
  3. Use the “ls” command to list the files in the current directory.
  4. To rename the file, use the “mv” command followed by the current file name and the new file name. Here’s an example:
sudo mv old_file_name.txt new_file_name.txt

In this example, we are using the “sudo” command to gain the necessary permissions to rename the file. We are also renaming a text file called “old_file_name.txt” to “new_file_name.txt”.

How to rename multiple files in Linux?

To rename multiple files in Linux, you can use the mv command in a terminal. For example, if you want to rename all files with the .txt extension to .doc, you could use the following command:

 mv *.txt *.doc

This command will rename all files with the .txt filename extension in the current directory to have the .doc filename extension instead.

Alternatively, you can use the rename command, which is a more powerful tool for renaming files. To install rename command in Linux follow this –

if you are using Ubuntu or another distribution that uses the apt package manager, you can use the following command to install the rename command:

sudo apt-get install rename

If you are using a different distribution, you will need to use the appropriate package manager and package name for your system. For example, on Fedora or CentOS, you can use the yum package manager and the prename package, like this:

sudo yum install prename

Now since it’s installed, you can use it to rename multiple files, like to rename all files in the current directory that have the .txt file extension to have the .text extension instead, you can use the following command:

rename 's/\.txt$/.text/' *.txt

 

The following command will rename all files with the .txt file extension to have the .doc file extension, but will also append the current date to the end of each filename:

rename 's/.txt$/.doc/' *

This command uses a regular expression to match the .txt filename extension at the end of each filename and replaces it with the .doc filename extension. The * character at the end of the command tells rename to apply the regular expression to all files in the current directory.

There are many other options and features available with the rename Linux command, so it is worth reading the documentation to learn more about its capabilities, for that you can use the -h or –help option with the command.

Summary
How to rename file in linux?
Article Name
How to rename file in linux?
Description
This simple tutorial will show you step-by-step how to use the rename command to easily change the names of files and directories in Linux.
Author

Leave a Comment