How to Rename Files and Directories Using the Linux Terminal
We can rename files and directories using the rename
and commands
in Linux terminal . The command can rename only one file at a time, but the command can rename multiple files at the same time.mv
mv
rename
mv
Rename files and directories
using the command
mv
The command can rename files and directories. It is also used to move files and directories from one location to another.
mv
Command Syntax
mv [OPTIONS] source destination
source
Can be one or more files or directories, destination
always one file or directory.
If we have multiple files or directories as source, the destination is always a directory. In this case, all source files and directories will be moved to the destination directory.
If we put a file as the source file and a directory as the destination directory, the source file is moved to the destination directory.
mv
To rename a file
using , mv
both the source and destination arguments in the command must be files.
mv helloworld.py main.py
It helloworld.py
renames the file to main.py
.
We can also rename directories in the same way.
mv Programs Python-Programs
It Programs
renames the directory to Python-Programs
.
mv
Rename multiple files
with the command
As we know, mv
the command can only rename one file at a time. We can use for
or while
loop to rename multiple files.
for f in *.png; do
mv -- "$f" "${f%.png}.jpg"
done
.png
It renames
all files in the current directory with the extension .jpg
.
rename
Rename files and directories
using the command
rename
The command is slightly more advanced than mv
the command and can rename multiple files in one step.
To install the version of
in Ubuntu
and , use the command.Debian
Perl
rename
sudo apt install rename
To install the version of the command
in CentOS
and , use the command.Fedora
Perl
rename
sudo yum install prename
To install the version of rename command in Arch Linux Perl
, use this command.
yay perl-rename ## or yaourt -S perl-rename
rename
Command Syntax
rename [options] 's/old/new/' files
Example: rename
Rename a file using the command
rename 's/.png/.jpg/' *.png
.png
It renames
all files in the current directory .jpg
.
To print the renamed file names, we use the -h option rename
in the -p command -v
.
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
.jpg
It renames
all files in the current directory to .png
, and prints each renamed file in the terminal.
Example: Convert file names to lowercase
rename 'y/A-Z/a-z/' *
It converts all uppercase file names in the current directory to lowercase.
Example: Convert file names to uppercase
rename 'y/a-z/A-Z/' *
It converts all lowercase letters of file names in the current directory to uppercase letters.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Renaming a Database in MongoDB
Publish Date:2025/04/10 Views:199 Category:MongoDB
-
With the help of this MongoDB tutorial article, you will learn how to rename a MongoDB database. You will achieve this through two methods. The following method renames a database in MongoDB. Rename a MongoDB database using the MongoDB GUI
How to Move Files and Directories in Linux Using mv Command
Publish Date:2025/04/05 Views:178 Category:OPERATING SYSTEM
-
mv We can move files and directories through the Linux terminal using the command with various parameters . Use mv the command to move files and directories mv(move) The command can move files and directories from one location to another. I
Rename local and remote Git branches
Publish Date:2025/03/30 Views:197 Category:Git
-
While working on a project, if you feel that the branch name is not suitable for the branch you are working on and want to rename the branch, there are several ways to rename the branch depending on the scenario you are in. In this article,
How to rename a local branch in Git
Publish Date:2025/03/26 Views:66 Category:Git
-
Git is a widely known, in-demand, and popular version control system (VCS) that is commonly used by software developers to collaborate on code development in small or large teams. It helps us coordinate our work and helps us track the chang
Rename files and directories in a Git repository
Publish Date:2025/03/26 Views:199 Category:Git
-
In this article, we will discuss the rename process in git. We use Git Rename to change the name of files and folders in the working directory. The renaming process involves the git mv command. This command accepts two parameters: target an
Renaming an image in Docker
Publish Date:2025/03/25 Views:99 Category:Docker
-
In Docker, we can use an easy way to rename an image without rebuilding it from scratch. This article will discuss how to rename an image using Docker. Renaming an image in Docker We can rename an image or change the repository name by chan
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way
在 Java 中重命名文件
Publish Date:2023/08/14 Views:147 Category:Java
-
本文介绍了如何在 Java 中重命名文件,并列出了一些示例代码,以便你进一步了解该主题。在 Java 中重命名文件非常容易,因为 Java 在 java.io 包中提供了几个内置方法。