Linux

Linux Rename File Example

Hello readers, in this tutorial, we will learn how to rename a file in the Linux operating system. For this tutorial, we will rename a file either:

  • By using the mv command to rename the file
  • Using rename command

1. Introduction

Renaming files in Linux is not an advanced option, but it’s surely the interesting one. Developers know that Linux has a powerful command-line interface which can be used to write this tutorial.

1.1 Renaming a file with ‘mv’ Command

The simplest way of renaming the files or folders in the Linux operating system is with the mv command. Its basic purpose is to move the files or folders, but it also renames the files as the act of renaming the file is considered by the file-system as moving it from one name to another. This most basic way has the following prototype form:

Syntax 1

mv [options] old_file_name new_file_name

The Syntax 1 will only work if the file is in the current working directory. If not, developers can specify the full path and can use:

Syntax 1(a)

mv [options] /<path_to_file>/old_file_name /<path_to_file>/new_file_name

Do remember:

  • The mv command should have a write permission for the folder containing the files
  • If developers want to confirm the file-name before renaming, they can use the -i option in conjugation with the mv command
  • If developers want to display all the changes made by the mv command, they can use the -v option in conjugation with the mv command

1.2 Renaming a file with ‘rename’ Command

The rename command in Linux operating system is used to rename multiple files using the Perl expressions. This command is more advanced than the mv because it supports the regular expressions, renames files to lowercase or uppercase and overwrites files using the Perl. This command also accepts the wildcard entries and has the following prototype form:

Syntax 2

rename 's/old-name/new-name/' file_name1.extn file_name2.extn

The letter s is the important part of the regular expression and it stands for “substitute“. The rename command comes with few optional arguments i.e.

  • -v: Prints the list of successfully renamed files along with the old names
  • -f: Forcefully overwrites the existing file
  • -n: Determine what changes would take place but not done for real

2. Practical usage

Let’s understand the usage of these commands with the help of the sample snippets.

2.2.1 Start Linux

Start a standalone Linux instance as shown below.

Fig. 1: Start Linux instance
Fig. 1: Start Linux instance

2.2.2 Create a Sample file

To start with this tutorial, we will need to create a dummy file using the vi editor in Linux. Developers can write a dummy content to this file and save it using the :wq! option. The following Linux snippet can be used.

Query 1

$ vi file1.txt

The command gives the following output.

Fig. 2: Sample file having the Dummy content
Fig. 2: Sample file having the Dummy content

2.2.3 Displaying the Content of Sample file

Once the file is created, developers can use the cat command to ensure that the file was successfully created at the given path. The following Linux snippet can be used.

Query 2

$ cat file1.txt

The command gives the following output.

Fig. 3: Displaying the file content
Fig. 3: Displaying the file content

2.2.4 Renaming the file using the ‘mv’ command

In short, to rename a file in Linux developer can use the following mv command. The following Linux snippet can be used.

Query 3

$ mv /<_path_to_the_file_>/file1.txt /<_path_to_the_file_>/welcome.txt

The command gives the following output.

Fig. 4: Renaming the file
Fig. 4: Renaming the file

2.2.5 Renaming the file using the ‘rename’ command

Let’s say developers want to rename multiple files at once. To achieve this they can use the rename command in Linux. Here is the basic Linux snippet that can be used to rename the multiple files.

Query 4

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

As shown in Fig. 5, this command will rename all the .txt files to the .log in the working directory. Developers can verify this by doing the ls -ltr on the Linux terminal.

Fig. 5: Renaming the files
Fig. 5: Renaming the files

Make note, while doing the critical or the major tasks developer can verify the changes by running the rename command with -n argument.

3. pyRenamer

pyRenamer is a renaming tool written in Python language to support the renaming of files using a desktop application. This application allows the file name change with a simple mouse click. At this moment, the official website is down, but users can install it from the 3rd part repositories using the following command:

sudo apt-get install pyRenamer

That’s all for this post. Happy Learning!!

4. Conclusion

In this tutorial, developers learned how to rename a file or files in the Linux system using the mv or the rename command. I hope this article served you with whatever developers are looking for. You can download the sample commands in the Downloads section.

5. Download the Source Code

This was a tutorial of renaming the files in Linux.

Download
You can download the full source code of this example here: Linux_file_rename_cmds

Yatin Batra

Yatin has graduated in Electronics & Telecommunication. During his studies, he has been involved with a large number of projects ranging from programming and software engineering to telecommunications analysis. He works as a software developer in the information technology sector where he is mainly involved with projects based on Java and J2EE technologies platform.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button