How to rename a file in Linux using the mv command?

Published on Aug. 22, 2023, 12:17 p.m.

To rename a file in Linux using the mv command, you can use the following syntax:

mv oldfilename newfilename

Where oldfilename is the current name of the file you want to rename, and newfilename is the new name you want to give the file. For example, let’s say we have a file named oldfile.txt that we want to rename to newfile.txt. We can use the following command:

mv oldfile.txt newfile.txt

This will rename oldfile.txt to newfile.txt.

You can also use mv to move a file to a different directory while renaming it at the same time. For example, to move the file oldfile.txt from the current directory to a directory named newdir, and rename it to newfile.txt, you can use the following command:

mv oldfile.txt newdir/newfile.txt

Note that if the file you are renaming or moving has spaces in its name, you should enclose the filename in quotes to avoid errors. For example:

mv "old file.txt" "new file.txt"

This will rename old file.txt to new file.txt.

Tags: