How To Rename Files and Directories In Linux?

Renaming files and directories are very popular task done by regular users or administrators. We can rename files and directories by using Graphical User Interface (GUI) or via the command line interface. But advanced users or administrators generally prefer the command line interface. Because the command line interface provides multiple commands and different options to rename files and directories.

Rename Files with mv Command

The mv command originally created to move files and directories. The name mv comes from the word move . As moving a file is the same thing as renaming the file. Simply the old name is replaced with the new name. The syntax of the mv command is like below.

mv OLD_NAME NEW_NAME
  • OLD_NAME is the old name that will be replaced with the new name.
  • NEW_NAME is the new name.

In the following example, we change the file named “old.txt” into “new.txt”.

$ mv old.txt new.txt

Rename Directories with mv Command

We can use the mv command in order to rename directories. Similar to the moving files we can rename directories with the mv command. The syntax of the mv command is like below.

mv OLD_NAME NEW_NAME
  • OLD_NAME is the old name that will be replaced with the new name.
  • NEW_NAME is the new name.

In the following example, we change the file named “old_directory” into “new_directory”.

$ mv old_directory new_directory

Rename Files with rename Command

There is also a native command to rename files. The rename command is specifically created to rename files in Linux. The rename command is a bit different from the mv command. The rename command is used with the Perl regular expression.

In the following example, we rename the HTML files with the *.html extension into the *.php extension.

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

Leave a Comment