How to Rename Files and Directories Using Linux Terminal

Suraj Joshi Feb 02, 2024
  1. Rename Files and Directories Using the mv Command
  2. Rename Files and Directories Using the rename Command
How to Rename Files and Directories Using Linux Terminal

We can rename files and directories with rename and mv commands in the Linux Terminal. The mv command can only rename one file at a time, but the rename command can rename multiple files simultaneously.

Rename Files and Directories Using the mv Command

The mv command can rename files and directories. It is also used to move files and directories from one location to another.

Syntax of mv Command:

mv [OPTIONS] source destination

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

If we have multiple files or directories as the source, the destination is always directory. In this case, all the source files and directories are moved to the destination directory.

If we have a single file as a source and directory as a target, the source file is moved to the target directory.

To rename a file using mv, both the source and target parameters in the mv command must be files.

mv helloworld.py main.py

It renames the file helloworld.py to main.py.

We can also similarly rename the directory.

mv Programs Python-Programs

It renames the directory Programs to Python-Programs.

Rename Multiple Files With mv Command

As we know, the mv command can only rename one file at a time. We could use for or while loops to rename multiple files.

for f in *.png; do 
    mv -- "$f" "${f%.png}.jpg"
done

It renames all the files in the current directory with an extension of .png to .jpg.

Rename Files and Directories Using the rename Command

The rename command is slightly advanced than the mv command and can be used to rename multiple files in a single step.

To install the Perl version of the rename command in Ubuntu and Debian, use the command:

sudo apt install rename

To install the Perl version of the rename command in CentOS and Fedora, use the command:

sudo yum install prename

To install the Perl version of the rename command in Arch Linux, use the command:

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

Syntax of rename Command:

rename [options] 's/old/new/' files

Example: Rename Files With rename Command

rename 's/.png/.jpg/' *.png

It renames all the .png files in the current directory to .jpg.

To print names of renamed files, we use the -v option in the rename command.

rename -v 's/.jpg/.png/' *.jpg

Output:

1.jpg renamed as 1.png
bubbleheads.jpg renamed as bubbleheads.png
demo.jpg renamed as demo.png
hiss.jpg renamed as hiss.png
invoice.jpg renamed as invoice.png

It renames all the .jpg files in the current directory to .png, and prints each renamed file in the terminal.

Example: Convert Filenames to Lowercase

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

It converts all the alphabets of the filename in uppercase in the current directory to lowercase.

Example: Convert Filenames to Uppercase

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

It converts all the alphabets of the filename in lowercase in the current directory to uppercase.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux Files and Directories