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
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u