×

Rename Folder in Linux

Rename Folder in Linux

In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose of renaming and moving files.

Below we have listed some of the popular ways to rename any file and directory.

  • With the help of mv command
  • With the help of find command
  • With the help of rename command
  • With the help of bash script

Rename with the help of mv command

The actual working of the mv command is of moving files and directories. This command will stuff from one place to another, and we move our file in the same directory but with a new name. This process eventually leads to renaming the file or directory.

Example

In this example, we have to create a directory at the user's home directory and then rename that directory to a new name.

 #mkdir directory_created
#ls 
Rename Folder in Linux

Now, rename directory_created to directory_renamed.

#mv directory_created directory_renamed
Rename Folder in Linux

Hence the directory has been renamed successfully with the help of mv command.

Rename with the help of find command

The primary work of the find command is to search for files in the file system or directory hierarchy. Below we have explained the syntax of the find command to rename any directory.

Syntax  

#find . -depth -type d -name <InitialName> -execdir mv {} <FinalName> \

Example

Now, let us try the same example of directory_created and directory_renamed.

 #mkdir directory_created
#find . –depth –type d –name directory_created –execdir mv {} directory_renamed
#ls 
Rename Folder in Linux

Rename with the help of rename command

The rename command is not installed in the system binary, and you can install this using the below screenshot.

Rename Folder in Linux

The primary use case of the rename command is to rename multiple files at a time using the terminal. The rename command uses an expression to rename these multiple files.

Example

In this example, we have renamed all the files and the folders to the lower cases. You could even provide any specific file instead of the *, and the * is used to rename all the content present in the directory.

Rename Folder in Linux

Rename with the help of bash script

You can rename multiple files and directories by using a bash script. We are going to create a script that could find all the png files and rename them according to the serial numbers.

Example 1

First, we will create some .png files using the touch command. It is straightforward to use the touch command, and you can also create multiple files if assigned as arguments.

Rename Folder in Linux

Now, we have to rename these image files using a script. Let us code a script for this task. Open a text editor and name the script file as renameScript.sh.

 #!/bin/bash
i=1
for Old_name in *.png
do
mv $Old_name ${i}.png
echo $Old_name"----> "${i}”.png”
i=$(( i + 1 ))
done   

Now, execute the file and see the results.

Rename Folder in Linux

Hence all of the .png files have been renamed.

Example 2

We can also rename directories using the bash script, the approach will be the same, but some additional lines will be added.

First, we have created some demo directories using the mkdir command.

Rename Folder in Linux

Now, we have to create a script to rename these demo directories.

 #!/bin/bash
i=0
for Old_directory in *
do
          if [ -d "$Old_directory" ]
          then
          mv "${Old_directory}"  "d${i}"
          i=$(( i+1 ))
          fi
done 

The if condition is for confirming whether the variable is a directory or not.

Rename Folder in Linux

Related Topics

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.