Rename file linux

Table of Contents

Introduction

To rename a file from the Linux command line you can use the mv command.

You have surely happened to be working on a Linux system and need to rename a file using the command line.
In this post we will see how you can use the Linux mv command to rename a file.

The nice thing is that we don’t have to install any additional packages, in fact the mv command is present in all distros natively.

How to rename file in Linux

As already anticipated in the introduction, to rename a file using the Linux command line, use the mv command.
This command has the following syntax:

mv [options] SOURCE DESTINATION

The one labeled SOURCE represents the file whose name you want to change, while DESTINATION represents the new name.

Don’t forget to be in the same working directory before running this command, otherwise the file will not be found.
If you don’t know how to change directories, I recommend reading this post about the cd command.

However, it is possible to use SOURCE and DESTINATION as the complete path to the file, not necessarily just the name.

The command provides various options and in this chapter we will see how to use the main ones.
Don’t worry, as always each command will be explained in detail and with many examples.

As final suggestions, I remind you that if you want to check which files are present in your working directory you can use the ls command.
If, on the other hand, you want to create files to do some tests, just follow this tutorial.

Rename file using mv Linux command

Let’s suppose that in our working directory we have a file called file_1.txt and we want to rename it to file_2.txt.
Well, to do this, just run this command:

mv file_1.txt file_2.txt

After running this command you will see that file_1.txt has become file_2.txt.

Rename file using mv Linux command if new name already exists

In the previous section we saw how you can rename a file using the Linux mv command.
But what if the new name we want to give is already there.
Let me explain better, suppose that in your working directory you have the following files: file_1.txt and file_2.txt.

Just like before we want to rename file_1.txt to file_2.txt.

mv file_1.txt file_2.txt


Since file_2.txt already exists, file_1.txt will replace file_2.txt losing important information.

For this reason I recommend using the -i/–interactive option.

mv file_1.txt file_2.txt --interactive

In fact, by launching this command, if the destination file already exists, we will be asked if we want to replace the file or not.
Just answer y for yes and n for no.

mv command option –force and –no-clobber

In this last section we see two options of the mv command that may be useful to you.
Suppose that also in this case we have two files in the same working directory called file_1.txt and file_2.txt.

We want to rename file_1.txt to file_2.txt but file_2.txt already exists.
If we use the -f/–force command we will always overwrite the file while if we use the -n/–no-clobber command we will NEVER overwrite the file.

Here’s how to use them:

mv file_1.txt file_2.txt --force
mv file_1.txt file_2.txt --no-clobber

Rename a file using Linux command ‘rename’

In the previous sections we have seen how to rename a file using the mv command.
However, you can also use another Linux command to rename a file whose name is rename.

By default this command is not present in all Linux distributions therefore it is necessary to install it.
The command varies according to the version of Linux installed:

  • Ubuntu and Debian: sudo apt install rename
  • CentOS and Fedora: sudo yum install prename
  • Arch Linux: sudo pacman -S rename

Substitute expressions replace part of the filename with another string.
Here the syntax of the rename command:

rename [options] 's/[filename item]/[replace]/' [filename]

Using this command, wew are going to rename the file substituting the first occurrence of the filename element with the replacement.

The rename command has several options that can be used and here you can find a list with a short description:

Option nameDescription
-v, –verboseIncrease the verbosity of the output
-n, –nonoOnly shows the output without doing anything
-f, –forceOverwrites existing file if present
-d, –filename, –nopath, –nofullpathDon’t rename directory but just filename component of path
Rename command options with descriptions

Let’s see here a couple of example on how to use this command.

If you want to change the extension of a file you can use the following command:

rename -v 's/.txt/.pdf/' *.txt

Otherwise, if you need to change the lowercase characters into uppercase you can use this command:

rename -v 'y/A-Z/a-z/' *.TXT

Conclusion

And here we are at the end of this post!
As usual I hope now you know how best to use the Linux mv command to rename files.
If you have any doubts, do not hesitate to contact me or leave me a comment below, I will try to reply as soon as possible.

Last but not least, I invite you to take a look at my other blog posts!

Leave a Comment

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