Method 1: Using mv
and Wildcards
Step 1: Open the Terminal
Open your terminal application. You can do this by pressing Ctrl + Alt + T
or searching for “Terminal” in the application menu.
Step 2: Navigate to the Directory
Navigate to the directory containing the files you want to rename using the cd
command:
cd /path/to/your/directory
Step 3: Rename Files with a New Extension
To rename files with a new extension, use the following command:
mv *.old_extension *.new_extension
Replace old_extension
with the current extension of your files and new_extension
with the desired new extension.
Step 4: Add a Prefix or Suffix
To add a prefix or suffix to multiple files, use the following commands:
Add a prefix
for file in *; do mv "$file" "prefix_$file"; done
Add a suffix
for file in *; do mv "$file" "$file_suffix"; done
Replace prefix_ and _suffix
with the desired prefix and suffix.
Method 2: Using rename Tool
Step 1: Install perl-rename
If you don’t have the perl-rename
package installed, install it using the package manager:
sudo pacman -S perl-rename
Replace a specific string in all filenames
rename 's/old_string/new_string/' *
Add a prefix
rename 's/^/prefix_/' *
Add a suffix
rename 's/$/_suffix/' *
Replace old_string
, new_string
, prefix_
, and _suffix
with your specific renaming requirements.
Example Use Case: Renaming Files with a Numeric Sequence
Suppose you have files named dqend.jpg, scjwe.jpg, and ejoqdw.jpg, and you want to rename them to sd-1.jpg, sd-2.jpg, and sd-3.jpg. Here’s how you can do it:
counter=1
for file in *.jpg; do
mv "$file" "sd-$counter.jpg"
((counter++))
done
This script uses a counter to add a numeric sequence to the filenames.
By following these steps, you can efficiently rename multiple files in the Linux terminal. Always exercise caution when performing bulk renaming operations and consider making backups before making changes.
Happy renaming!