How to rename Files and Directories in Linux – Easy Steps

Renaming the files in Linux is one of the basic tasks that you often need to perform as a beginner because it is very normal in our daily life. Basically, there are two methods to rename your Linux files. The one is using a GUI file manager and the second one is via the command line terminal.

How to rename files and directories on linux

If you want to rename a single file then the steps are simple, but renaming multiple files at once can be difficult especially for new users.

So in this article, we have discussed How to use the mv and rename commands to rename files and directories.


Renaming Files with the mv Command

The mv command is used to rename or move files from one location to another. The syntax of the mv command is given below. 

mv [OPTIONS] source destination

The source can be one or more files, or the directory and the destination can be a single file or directory.

⇒ Suppose, If you specify a file as the source, and the destination target is an existing directory, the source file is moved to the specified directory.

⇒ Suppose, If you specify multiple files as the source, the destination must be a directory. In this case, the source files are moved to the target directory.

⇒ To rename a file, you need to specify the single file as a source and the single file as the destination target.

Suppose if you want to rename the file filename1.txt as filename2.txt then you have to run the command.

mv filename1.txt filename2.txt


Renaming multiple files with the mv Command

The mv command can only rename one file at once, But it can be used as a combination with other commands such as find or for loop to rename multiple files or inside the bash or while loop to rename multiple files.

The below example code shows how to use a Bash for loop to rename all .html files in the present directory by changing the .html extension to .php.

for f in *.html; do

    mv — “$f” “${f%.html}.php”

done

Let’s try to understand the code line by line.

⇒ The first line creates a for loop for the given extension and iterates through the list of all files edged with .html.

⇒ The second line is applied to each item in the list and moves the file to a new file, replacing .html with .php. and the ${file%.html} part is using shell parameter expansion to extract the .html part from the filename.

⇒ The third line’s Done gives the indication of the end of loop segment.


Here is a code using mv in conjunction with find to achieve the same as above:

find . -depth -name “*.html” -exec sh -c ‘f=”{}”; mv — “$f” “${f%.html}.php”‘ ;

It means the find command is passing all those files ending with .html in the present directory to mv one by once using the -exec option and the string {} is the only name of the file that is currently in process.


Renaming Files with the rename Command

The rename command is also used for renaming multiple files on the Linux system. This command needs basic knowledge about regular expressions.

In this article, we are using the Perl version of the rename command. You have to firstly install this version using the package manager on your PC or laptop, but if you are a new user then follow the below commands according to your Linux distribution.


Install rename on Ubuntu and Debian

sudo apt install rename


Install rename on CentOS and Fedora

sudo yum install prename


Install rename on Arch Linux

yay perl-rename ## or yaourt -S perl-rename


The syntax for the rename command is given below.

rename [OPTIONS] perlexpr files


The rename command will rename files according to the specified perlexpr regular expression.


The following codes will change all .html files to .php

rename ‘s/.html/.php/’ *.html


Use the -n option to print the names of the files without renaming them.

rename -n ‘s/.html/.php/’ *.html


The output will look similar to this, the .html extension file change into .php extension files.

rename(linuxfile.html, linuxfile.php)

rename(linuxfile1.html, linuxfile1.php)

rename(linuxfile2.html, linuxfile2.php)

rename(linuxfile3.html, linuxfile3.php)

rename(linuxfile4.html, linuxfile4.php)


Pass the -f option to allow over-writing existing files:

rename -f ‘s/.html/.php/’ *.html


Some more code about How to use the rename command.

Converting filenames to lowercase

rename ‘y/A-Z/a-z/’ *


Converting filename to uppercase

rename ‘y/a-z/A-Z/’ *


Replacing spaces in files with underscores

rename ‘y/ /_/’ *


Wrap Up

We have provided easy methods to rename the files in Linux, also there are some other commands like mmv to rename files in Linux.  But we have discussed mv and rename commands only because for beginners they are easy to remember.

How to rename Files and Directories in Linux – Easy Steps

Leave a Reply

Your email address will not be published.

Scroll to top