×

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

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.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

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

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

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.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

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

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.

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.

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

3 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

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

awk Command in Linux/Unix with Examples

The 'awk' 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...

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.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

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

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

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.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.