How to Rename Files and Directories in Linux Version (*)

  • Share this:

How to Rename Files and Directories in Linux

Renaming files is one of the most basic tasks you often need to perform on a Linux system. You can rename files using a GUI file manager or via the command-line terminal. Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users who are new to Linux.

What Is the mv Command

The mv command has a purpose in life, and that is to move files. It is a happy side effect that it can be used to move an existing file into a new file, with a new name. The net effect is to rename the file, so we get what we want. But mv is not a dedicated file renaming tool.

Renaming a Single File With mv

mv [OPTIONS] source destination

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

sudo mv old-file-name.html new-file-name.php

You can use sudo ls to check the file has been renamed.

Renaming multiple files at a time with the mv Command

The mv command can rename only one file at a time, but it can be used in conjunction with other commands such as find or inside bash for or while loop to rename multiple files.

The following example shows how to use the Bash for loop to rename all (any).html files in the current directory by changing the (any).html extension to (any).php.

for f in *.html; do
    mv -- "$f" "${f%.html}.php"
About author
Here’s My little description of your attention on Me and My blog. I am here to help you with PHP programming.
View all posts (53)