Renaming multiple files at once can be a time-saving task, especially when dealing with large numbers of files. In this article, we will explore how to rename multiple files simultaneously in Linux Ubuntu using various methods and command-line tools ( Renaming Multiple Files).
Renaming Multiple Files
Method 1: Using the rename
Command The rename
command is a powerful tool that allows us to rename files in bulk based on specific patterns. Follow these steps to rename multiple files at once:
- Open a terminal by pressing Ctrl+Alt+T or searching for “Terminal” in the application launcher.
- Navigate to the directory where the files are located. For example, if the files are in the “Documents” folder, you can use the following command to change to that directory:
![Renaming Multiple Files](http://techvibeblog.com/wp-content/uploads/2023/07/Copy-of-read-write-files-python-3--1024x572.png)
bashCopy code
cd ~/Documents
- Use the
rename
command in the following format to rename files based on a pattern:
bashCopy code
rename 's/old_pattern/new_pattern/' files
Replace old_pattern
with the current pattern in the file names that you want to change, and new_pattern
with the desired new pattern. You can use regular expressions to match complex patterns.
For example, to rename all files with the extension “.txt” to “.doc”, you would use the following command:
bashCopy code
rename 's/.txt$/.doc/' *.txt
- Verify the changes by listing the files in the directory using the
ls
command:
bashCopy code
ls
Method 2: Using the mmv
Command The mmv
command is another useful tool for batch renaming files in Linux Ubuntu. Follow these steps to use the mmv
command:
- Open a terminal.
- Navigate to the directory where the files are located.
- Use the following command to rename files:
bashCopy code
mmv 'old_pattern' 'new_pattern'
Replace old_pattern
with the current pattern in the file names that you want to change, and new_pattern
with the desired new pattern.
For example, to rename all files with the extension “.jpg” to “.png”, you would use the following command:
bashCopy code
mmv '*.jpg' '#1.png'
- Verify the changes using the
ls
command.
Method 3: Using a Bash Script If you have more complex renaming requirements, creating a bash script can provide more flexibility. Follow these steps to create a bash script for renaming files:
- Open a text editor and create a new file. For example, you can use the following command to open the file in the Nano editor:
bashCopy code
nano rename_script.sh
- Write the script, specifying the renaming rules using the
mv
command. For example, to replace all spaces in file names with underscores, you would use the following script:
bashCopy code
#!/bin/bash
for file in *; do
mv "$file" "${file// /_}"
done
- Save the file and exit the text editor.
- Make the script executable by running the following command:
bashCopy code
chmod +x rename_script.sh
- Execute the script by running the following command:
bashCopy code
./rename_script.sh
Remember to navigate to the directory containing the files you want to rename before running the script.
By following these methods, you can easily rename multiple files at once in Linux Ubuntu, saving time and effort. Choose the method that best suits your renaming needs and efficiently manage your files
Renaming Multiple Files.