×

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

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.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

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

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

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.

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.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes 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.

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.

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.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute 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.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.