Renaming Files in Bash
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 you can easily rename all files in a directory in the correct order.
In this article, we will present a method by which you can easily rename all files in a directory. In addition, we will see the necessary examples and explanations to make the topic easier to understand.
Rename Files Using mv Command in Bash
Bash scripting contains a built-in command called mv. The mv command is mainly used to move directories and files from one location to another.
It can also be used to rename files and directories. One important thing to know about this command is that if you do not specify a new name for the file, the file name will be the same in its new location.
The general syntax of this command is $ mv [OPTIONS] SOURCE DESTINATION
. The available options for this command are:
- mv -f - This flag will force the move by overwriting the destination file without prompting.
- mv -i - This flag is used for an interactive prompt before overwriting.
- mv -u - This flag is for updating. This will only move the file if it is the new source and not the destination.
- mv -v - This flag will print all files of both the source and destination.
- man mv - This flag will open the help manual.
Suppose we have a list of files with the following order.
1_file.txt
2_file.txt
3_file.txt
4_file.txt
5_file.txt
6_file.txt
Now, take a look at the following code example.
for file in *.txt
do
mv "$file" "${file/_file.txt/_Textfile.txt}"
done
In the above example, we are renaming all the files to .txt type. To do this, we have used a for loop with the command mv.
Now the general syntax used for this purpose is mv "$LoopVar" "${LoopVar/PreviousName.txt/NewName.txt}"
.
You have to note here that you need to include a loop variable in the file name; otherwise, it may cause overwriting since it is a system-generated name.
Now when you run the above code, you will see that all your files have been renamed as shown below.
1_Textfile.txt
2_Textfile.txt
3_Textfile.txt
4_Textfile.txt
5_Textfile.txt
6_Textfile.txt
An important thing about this command is to specify the common part of the file name to identify the file. It can be the file type or any other common pattern in the file name.
Otherwise, the command may not successfully rename all files.
Rename Files in Bash Using rename Command
There is also a third party command for Bash scripting called rename. But you need to install it before using it.
To install this command in your shell environment, you can follow the commands below for Ubuntu and Debian.
$ sudo apt install rename
Afterwards, we can rename the file like below.
rename [Your Options] 's/[Current Filename]/[New Filename]/' [Filename]
This command also includes some options, which are shown below.
-
-a
- This option will replace all file names except the first one. -
-f
- This option will force overwriting of an existing file. -
-h
- This option will display help text. -
-i
- This option will display a prompt notification before overwriting an existing file. -
-l
- This option will replace the last occurrence of an element instead of the first occurrence. -
-n
- This option performs a dry run. -
-s
- This option renames the target instead of the symbolic link. -
-v
- This option displays the output in a verbose version. -
-V
- This option displays the command version.
We can rename the above set of files using the following command.
rename -v 's/_Testfile/_Test/' *.txt
It will display the following output.
1_Testfile.txt renamed as 1_Test.txt
2_Testfile.txt renamed as 2_Test.txt
3_Testfile.txt renamed as 3_Test.txt
4_Testfile.txt renamed as 4_Test.txt
5_Testfile.txt renamed as 5_Test.txt
6_Testfile.txt renamed as 6_Test.txt
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.
Related Articles
Open Emacs in Bash
Publish Date:2025/03/21 Views:141 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit
Clear the Terminal Screen in Bash
Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM
-
There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your
Reload .bash_profile from the command line
Publish Date:2025/03/21 Views:67 Category:OPERATING SYSTEM
-
In the shell, .bash_profile is used to customize the configuration of user settings. It is stored in the root directory or home directory and is mostly hidden from other users. This file holds all the configuration of the shell and is also
Check if input parameter exists in Bash
Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM
-
When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh
Getting optional input arguments in Bash
Publish Date:2025/03/21 Views:180 Category:OPERATING SYSTEM
-
Sometimes we need to create a dynamic function which can be executed in both without passing any arguments or without passing any arguments. For this we need to set some default values for those arguments so that if the arguments are
Using Double and Single Pipes in Bash
Publish Date:2025/03/21 Views:136 Category:OPERATING SYSTEM
-
In Bash, the double pipe || is also known as the OR operator in other programming languages. On the other hand, the single pipe | is known as a pipeline. In this article, we will see how to use double pipe (also known as OR) and pipelines i
Breaking out of a loop in Bash
Publish Date:2025/03/21 Views:131 Category:OPERATING SYSTEM
-
Using loops is a common task in any programming or scripting language. While using a loop, sometimes we need to stop it under a predefined condition. Like other programming and scripting languages, Bash uses the keyword break to stop any lo
Bash configuration files in macOS
Publish Date:2025/03/21 Views:185 Category:OPERATING SYSTEM
-
In this article, we will discuss how to create, delete, and edit bash profiles in macOS. Create .bash_profile To create .bash_profile, open a terminal and move to your home directory using the following command. cd ~/ Now you can create usi
eval command in bash script
Publish Date:2025/03/21 Views:87 Category:OPERATING SYSTEM
-
This article is about using strings as commands in Bash scripts. For this purpose, the eval command is used. Eval Command in Bash Scripts In some Bash scripts, you have to create a string using variables or input values (for example)