How to rename files & directories in Linux?


There are various ways in which you can rename the files or directories on a Linux system. If you are using a system with a graphical user interface then you can use the file manager to easily rename the files or directories this method is very much similar to renaming a file or folder on MS Windows.

Another way of renaming files or directories in Linux is by using the commands in the terminal. The two commands to rename a file or directory are mv and rename. In this article, we will discuss how to rename a file or directory using both commands in Linux.

How to rename a file or directory using the mv command

The mv command is generally used for moving files or directories. We can also use it for renaming a file or directory.

Renaming a file

If you want to rename a file then use the command as it is given below –

mv old_filename new_filename

For example, to rename a file file1.txt to testfile.txt use-

mv file1.txt testfile.txt

Renaming a directory

Similarly, if you want to rename a directory then use the mv command like this –

mv old_dirname new_dirname

For example, let’s say you want to change a directory dir to newdir then use –

mv dir newdir

How to rename a file or directory using the rename command

The rename is a command that can be used for some more advanced tasks while renaming a file or directory on a Linux system. It comes preinstalled in some distributions but if it is not in your system then use one of the following commands in your terminal.

To install in a Debian based distribution such as Ubuntu/Linux Mint/MX Linux, use –

sudo apt install rename

If you want to install in RHEL/CentOS then use –

sudo yum install rename

To install in fedora use –

sudo dnf install rename

To install in Arch Linux/ Manjaro use –

sudo pacman -Syu perl-rename

Press y and then enter if ask for confirmation.

Syntax of rename command

How to use the rename command in the Linux terminal is given below –

rename [Options] perlexpr file(s)

Where perlexpr is the argument supplied to rename command. The file name will be modified according to this argument. You can see more details of command & options that can be used with it on its man page.

Rename all files in the current working directory

To rename all the files with .txt extension to .html use the following command in your terminal.

rename 's/.txt/.html/' *.txt

Using option -n with rename command will display the list of files that is to be renamed.

rename -s 's/.txt/.html/' *.txt

How to convert all file names in uppercase

To convert to all files and directories names in the current working directory from lowercase to uppercase use –

rename 'y/a-z/A-Z/' *

How to convert all file names from upper to lowercase

Similarly, if you want to convert all the files and directories name from lower to uppercase then use –

rename 'y/a-z/A-Z/' *

For more usage of the rename command see its manual page by executing the following command in your terminal –

man rename

Conclusion

Ok, that’s all for now. Now If you have a query then leave it in the comments below.

Leave a Comment

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