Introduction
Batch renaming, also known as batch file renaming, is the process of changing the name of multiple files at once. In Linux environment, there are several ways to batch rename files, including terminal commands, GUI applications, and scripts. In this article, we will explore some commonly used methods for batch renaming files in Linux.
Batch Renaming with CLI Commands
Rename Command
The rename command is a powerful tool that can be used to rename files in a batch. It takes two arguments: a regular expression and a replacement string. The regular expression specifies the pattern to match in the file names, and the replacement string specifies the new name for the files.
The following example demonstrates how to rename all files with the extension ".txt" to ".md" in the current directory:
rename 's/\.txt$/.md/' *.txt
The regular expression 's/\.txt$/.md/' matches all file names ending with ".txt" and replaces the extension with ".md". The *.txt argument specifies that only files with the extension ".txt" should be renamed.
MV Command
The mv command can also be used for batch renaming files. It takes two arguments: the current name of the file and the new name for the file.
The following example demonstrates how to rename all files with the extension ".txt" to ".md" in the current directory:
for f in *.txt; do mv -- "$f" "${f%.txt}.md"; done
The for loop iterates over all files with the extension ".txt" in the current directory. The ${f%.txt}.md argument replaces the extension ".txt" with ".md" in the new name for each file.
Batch Renaming with GUI Applications
Thunar Bulk Rename
Thunar is the default file manager for XFCE desktop environment. It includes a powerful bulk rename tool that allows you to rename multiple files at once. To use Thunar bulk rename:
Select the files you want to rename in Thunar.
Right-click and select Rename... from the context menu.
In the bulk rename dialog, you can specify the rename pattern and preview the changes before applying them.
Click Rename to apply the changes.
Kraken
Kraken is a simple GUI tool for batch renaming files in Linux. It provides a user-friendly interface that allows you to easily rename files using different rename patterns.
To use Kraken:
Open Kraken from the application menu.
Add the files you want to rename from the toolbar or by drag-and-drop.
Choose one of the predefined rename patterns or create a custom pattern.
Preview the changes and click Rename to apply them.
Batch Renaming with Scripts
Bash Script
You can also use Bash scripts to automate the batch renaming process. The following example demonstrates how to rename all files with the extension ".txt" to ".md" in the current directory:
#!/bin/bash
for file in *.txt; do
mv "$file" "$(basename "$file" .txt).md"
done
The for loop iterates over all files with the extension ".txt" in the current directory. The $(basename "$file" .txt).md argument replaces the extension ".txt" with ".md" in the new name for each file.
Python Script
Python is a popular programming language that can be used for batch renaming files. The following example demonstrates how to rename all files with the extension ".txt" to ".md" in the current directory:
import os
for filename in os.listdir("."):
if filename.endswith(".txt"):
os.rename(filename, filename[:-4] + ".md")
The os.listdir function returns a list of all files in the current directory. The if filename.endswith(".txt") statement filters the list to include only files with the extension ".txt". The os.rename function renames each file by replacing the extension with ".md".
Conclusion
Batch renaming files is a common task in Linux environment. There are several methods that can be used to accomplish this task, including CLI commands, GUI applications, and scripts. Each method has its own advantages and disadvantages, and the choice depends on the user's preference and the specific requirements of the task.