Learning how to rename a file in Linux helps you manage your filesystem more efficiently. This guide will teach you how to rename file linux using multiple methods, from simple commands to advanced batch renaming techniques.
Renaming files in Linux is a common task that every user needs to master. Whether you’re organizing documents, fixing typos in filenames, or batch-renaming hundreds of files, Linux offers several powerful tools. Let’s explore them step by step.
Understanding The Linux Rename Commands
Linux provides two main ways to rename files: the mv command and the rename command. Each has its own strengths. The mv command is simpler and works for single files. The rename command is more powerful for batch operations.
Before we start, open your terminal. Most Linux distributions use bash as the default shell. You can use any terminal emulator like GNOME Terminal, Konsole, or xterm.
Using The Mv Command To Rename Single Files
The mv command is the most straightforward way to rename a file. Its primary purpose is moving files, but renaming is just moving a file to a new name in the same directory.
Basic syntax:
mv old_filename new_filename
Example: Rename “report.txt” to “report_2024.txt”
mv report.txt report_2024.txt
This command renames the file instantly. No confirmation prompt appears unless the new filename already exists. To get a confirmation prompt, use the -i (interactive) flag:
mv -i report.txt report_2024.txt
If “report_2024.txt” already exists, you’ll see a prompt like: mv: overwrite 'report_2024.txt'? Type “y” to confirm or “n” to cancel.
Renaming Files In Different Directories
You can also rename a file while moving it to another directory. This is useful for reorganizing your filesystem.
Example: Move and rename “data.csv” to “/archive/old_data.csv”
mv data.csv /archive/old_data.csv
The file is moved to the /archive directory and renamed to “old_data.csv” in one step.
How To Rename File Linux Using The Rename Command
The rename command is more advanced. It uses Perl expressions to rename multiple files at once. This is perfect for batch renaming tasks.
First, check if rename is installed on your system. Some distributions don’t include it by default. Install it using your package manager:
- Debian/Ubuntu:
sudo apt install rename - Fedora:
sudo dnf install prename - Arch Linux:
sudo pacman -S perl-rename
Basic syntax:
rename 's/old_pattern/new_pattern/' files
The s/old_pattern/new_pattern/ is a substitution expression. It replaces “old_pattern” with “new_pattern” in the filenames.
Example: Change all “.txt” extensions to “.md”
rename 's/\.txt$/.md/' *.txt
This renames every file ending with “.txt” to end with “.md”. The $ anchors the pattern to the end of the filename.
Using Rename With Regular Expressions
Regular expressions give you incredible flexibility. Here are some common patterns:
- Remove spaces from filenames:
rename 's/ /_/g' * - Convert to lowercase:
rename 'y/A-Z/a-z/' * - Add a prefix:
rename 's/^/backup_/' * - Add a suffix:
rename 's/$/_v2/' *.txt
Always test your rename command first. Use the -n (dry run) flag to see what changes will be made without actually renaming:
rename -n 's/\.txt$/.md/' *.txt
This shows a preview of the renaming. If it looks correct, remove the -n flag to execute.
Batch Renaming Files With Loops
You can combine bash loops with the mv command for batch renaming. This approach gives you more control than the rename command in some cases.
Example: Add a date prefix to all log files
for file in *.log; do
mv "$file" "2024_$file"
done
This loop iterates over every “.log” file and renames it by adding “2024_” to the beginning.
Example: Remove the first 3 characters from filenames
for file in *.txt; do
mv "$file" "${file:3}"
done
The ${file:3} syntax removes the first three characters from the variable $file.
Using Find With Exec For Complex Renaming
The find command can locate files in subdirectories and rename them. This is useful for recursive renaming.
Example: Rename all “.jpg” files to “.jpeg” in the current directory and subdirectories
find . -type f -name "*.jpg" -exec bash -c 'mv "$0" "${0%.jpg}.jpeg"' {} \;
This command finds all files ending with “.jpg”, then renames them by replacing the extension. The ${0%.jpg} removes the “.jpg” suffix from the filename.
Renaming Files With GUI File Managers
Not everyone prefers the command line. Most Linux desktop environments include a file manager with renaming capabilities.
Nautilus (GNOME)
Right-click a file and select “Rename”. Or select the file and press F2. Type the new name and press Enter.
For batch renaming, select multiple files, right-click, and choose “Rename…”. Nautilus offers options like:
- Replace text in filenames
- Add numbering
- Change case
Dolphin (KDE)
Right-click and select “Rename” or press F2. For batch renaming, select multiple files and press F2. Dolphin’s batch rename tool is very powerful, supporting:
- Search and replace
- Insert text at positions
- Numbering with custom formats
Thunar (XFCE)
Thunar has a simple rename dialog. Right-click and select “Rename” or press F2. For batch renaming, use the “Rename Multiple Files” option from the Edit menu.
Advanced Renaming Techniques
Renaming Files With Special Characters
Filenames with spaces, parentheses, or other special characters require careful handling. Always quote filenames in commands.
Example: Rename “my file.txt” to “my_file.txt”
mv "my file.txt" my_file.txt
Or use escape characters:
mv my\ file.txt my_file.txt
For batch renaming files with spaces, use the rename command:
rename 's/ /_/g' *
This replaces all spaces with underscores in every file in the current directory.
Renaming Files By Extension
Sometimes you need to change only the extension of multiple files. Here’s how:
Using rename:
rename 's/\.csv$/.tsv/' *.csv
Using a bash loop:
for file in *.csv; do
mv "$file" "${file%.csv}.tsv"
done
The ${file%.csv} removes the “.csv” suffix, then we add “.tsv”.
Renaming Files With Date And Time Stamps
Adding timestamps to filenames helps with versioning. Use the date command inside a loop.
Example: Add current date to all backup files
for file in backup_*.tar.gz; do
mv "$file" "${file%.tar.gz}_$(date +%Y%m%d).tar.gz"
done
This renames “backup_data.tar.gz” to “backup_data_20241115.tar.gz” (assuming today is November 15, 2024).
Common Mistakes And How To Avoid Them
Renaming files can go wrong if you’re not careful. Here are common pitfalls:
- Overwriting existing files: Use
-iwithmvto get confirmation. - Accidentally renaming directories: Be specific with your file patterns.
- Using wrong regular expressions: Always test with
-nfirst. - Forgetting to quote filenames: This causes errors with spaces.
Always double-check your command before executing. A typo can rename hundreds of files incorrectly.
Renaming Files With Case Sensitivity
Linux filenames are case-sensitive. “File.txt” and “file.txt” are different files. To change case:
Convert to uppercase:
rename 'y/a-z/A-Z/' *.txt
Convert to lowercase:
rename 'y/A-Z/a-z/' *.txt
Using a bash loop for lowercase conversion:
for file in *; do
mv "$file" "$(echo "$file" | tr '[:upper:]' '[:lower:]')"
done
Renaming Files With Numbering
Adding sequential numbers to files is common for ordering. Use a counter in a loop.
Example: Add numbers to all “.jpg” files
count=1
for file in *.jpg; do
mv "$file" "image_$count.jpg"
count=$((count + 1))
done
This renames “photo.jpg” to “image_1.jpg”, “vacation.jpg” to “image_2.jpg”, and so on.
For zero-padded numbers (01, 02, etc.), use printf:
count=1
for file in *.jpg; do
new_name=$(printf "image_%02d.jpg" $count)
mv "$file" "$new_name"
count=$((count + 1))
done
Using Third-Party Tools For Renaming
Several third-party tools simplify renaming tasks. They often provide a graphical interface or more intuitive syntax.
GPRename
A GTK-based batch renamer. Install it with:
sudo apt install gprename
It supports insert, delete, replace, numbering, and case changes.
KRename
A KDE-based batch renamer. Install with:
sudo apt install krename
It offers advanced features like scripting and regular expressions.
Thunar Bulk Rename
Part of the Thunar file manager. It’s lightweight and easy to use.
Renaming Files With Symlinks
When you rename a file, any symbolic links pointing to it become broken. To update symlinks, you need to recreate them.
Example: Rename a file and update its symlink
mv original.txt renamed.txt
ln -sf renamed.txt link_to_file
The -f flag forces the creation of the new symlink, overwriting the old one.
Renaming Files With Hard Links
Hard links are different. Renaming a file doesn’t affect its hard links. The hard link still points to the same inode, so the data remains accessible under the old name.
Example: Create a hard link and rename the original
ln file.txt hardlink.txt
mv file.txt renamed.txt
Now “hardlink.txt” still contains the same data as “renamed.txt”. Both point to the same inode.
Automating Renaming Tasks With Scripts
For repetitive renaming tasks, write a bash script. Save it as a file and make it executable.
Example script: Rename all “.txt” files to include a date prefix
#!/bin/bash
for file in *.txt; do
mv "$file" "$(date +%Y%m%d)_$file"
done
Save as “add_date.sh”, then run chmod +x add_date.sh to make it executable. Run it with ./add_date.sh.
Renaming Files With Special Permissions
If you don’t have write permission on a file, you can’t rename it. Use sudo for system files, but be careful.
Example: Rename a system log file
sudo mv /var/log/syslog /var/log/syslog_old
Only use sudo when necessary. Renaming system files can break your system.
Renaming Files In A Git Repository
Git tracks file renames. Use git mv instead of mv to keep Git aware of the change.
Example:
git mv old_name.txt new_name.txt
This renames the file and stages the change for commit. If you use mv instead, Git sees it as a deletion and a new file. You can still commit it, but the rename history is lost.
Renaming Files With Accented Characters
Filenames with accented characters (é, ü, ñ) can cause issues in scripts. Use Unicode-aware tools.
Example: Replace accented characters with ASCII equivalents
rename 's/é/e/g; s/ü/u/g; s/ñ/n/g' *
Or use a more comprehensive script with iconv:
for file in *; do
new_name=$(echo "$file" | iconv -f utf-8 -t ascii//TRANSLIT)
mv "$file" "$new_name"
done
Renaming Files With Multiple Extensions
Some files have multiple extensions, like “archive.tar.gz”. Renaming only the last extension requires careful pattern matching.
Example: Change “.tar.gz” to “.tgz”
rename 's/\.tar\.gz$/.tgz/' *.tar.gz
Using a bash loop:
for file in *.tar.gz; do
mv "$file" "${file%.tar.gz}.tgz"
done
Renaming Files With Hidden Files
Hidden files start with a dot. To rename them, include the dot in your pattern.
Example: Rename “.bashrc” to “.bashrc_backup”
mv .bashrc .bashrc_backup
For batch renaming hidden files, use patterns like .* but be careful not to rename “.” and “..” (current and parent directories).
Renaming Files With Wildcards
Wildcards make pattern matching easier. The asterisk (*) matches any characters, and the question mark (?) matches a single character.
Example: Rename all files starting with “report” to start with “doc”