rename file in Linux

Rename a File in Linux: Basics Every User Should Know

Knowing how to rename a file in Linux is something you should know as a Linux user.

When do you need it?

Let’s say you create a file with the touch command:

touch report.txt

And then you decide that you want to include the month in the name of the report.

We could copy the file to a new file called report_february.txt:

cp report.txt report_february.txt

And if we use the ls command we would get:

ls -ltr
total 0
-rw-r--r--  1 my-user  my-group  0  1 Mar 00:11 report.txt
-rw-r--r--  1 my-user  my-group  0  1 Mar 00:11 report_february.txt

So we still have the original report.txt file that we can delete with the following command:

rm report.txt

And, what if I want to do all this in one step?

The Command to Rename a File in Linux

I can use that mv command to simply rename the file report.txt.

Below you can see the syntax of the mv command:

mv original_filename new_filename

For instance, in this case:

ls -ltr
total 0
-rw-r--r--  1 my-user  my-group  0  1 Mar 00:11 report.txt 

mv report.txt report_february.txt

ls -ltr
total 0
-rw-r--r--  1 my-user  my-group  0  1 Mar 00:11 report_february.txt

We can only see report_february.txt because we have renamed the original file, we haven’t copied it to a new file with the new name the way we have done before.

Relative and Absolute Path

In this example we have assumed that we are in the directory where the report is when we run the mv command.

What changes if we are not in the same directory where the report is?

We use the absolute path of the file. So if the report is in /opt/reports/ the following command:

mv report.txt report_february.txt

Becomes:

mv /opt/reports/report.txt /opt/reports/report_february.txt

The name of the new file can also include a completely different directory from the one where the original file is.

In that case the mv command not only renames the file but also moves the file to the new target directory.

Renaming directories

How does this apply to directories?

It’s very similar…

That’s because the mv can also be used to rename directories.

Create a directory called test_dir in the current directory using the mkdir command:

mkdir test_dir 
ls -ltr
total 0
drwxr-xr-x  2 my-user  my-group  64  1 Mar 00:28 test_dir 

Then we rename it to test_dir2:

mv test_dir test_dir2

ls -ltr
total 0
drwxr-xr-x  2 my-user  my-group  64  1 Mar 00:28 test_dir2 

So the process of renaming a directory is pretty much identical to the one of renaming a file.

Conclusion

You now know:

  • How the Linux mv command is used to rename a file.
  • What is the difference between mv and cp.
  • Using Relative or Absolute path when renaming a file.
  • How to rename directories with the command mv.

Makes sense?

And to learn more basics about files and directories in Linux have a look at this article.

Share knowledge with your friends!

One comment

Leave a Reply

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