JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

How to Rename Files and Directories Using the Linux Terminal

Author:JIYIK Last Updated:2025/04/05 Views:

We can rename files and directories using the renameand 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.mvmvrename

mvRename files and directories using the command

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

mvCommand Syntax

mv [OPTIONS] source destination

sourceCan be one or more files or directories, destinationalways 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.

mvTo rename a file using , mvboth the source and destination arguments in the command must be files.

mv helloworld.py main.py

It helloworld.pyrenames the file to main.py.

We can also rename directories in the same way.

mv Programs Python-Programs

It Programsrenames the directory to Python-Programs.

mvRename multiple files with the command

As we know, mvthe command can only rename one file at a time. We can use foror whileloop to rename multiple files.

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

.pngIt renames all files in the current directory with the extension .jpg.

renameRename files and directories using the command

renameThe command is slightly more advanced than mvthe command and can rename multiple files in one step.

To install the version of in Ubuntuand , use the command.DebianPerlrename

sudo apt install rename

To install the version of the command in CentOSand , use the command.FedoraPerlrename

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

renameCommand Syntax

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

Example: renameRename a file using the command

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

.pngIt renames all files in the current directory .jpg.

To print the renamed file names, we use the -h option renamein 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

.jpgIt 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.

Article URL:

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

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

Pandas 重命名多个列

Publish Date:2024/04/22 Views:200 Category:Python

本教程演示了如何使用 Pandas 重命名数据框中的多个列。

在 Java 中重命名文件

Publish Date:2023/08/14 Views:147 Category:Java

本文介绍了如何在 Java 中重命名文件,并列出了一些示例代码,以便你进一步了解该主题。在 Java 中重命名文件非常容易,因为 Java 在 java.io 包中提供了几个内置方法。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial