Changing a file’s name in Linux is a straightforward process once you know the correct command. If you are new to Linux, understanding how to rename a file in Linux can feel a bit tricky because there is no single “rename” button like in Windows or macOS. Instead, you use the terminal and a few powerful commands. This guide will walk you through every method, from the simple mv command to advanced batch renaming tools. By the end, you will be able to rename files confidently, whether you are managing a single document or hundreds of images.
Linux gives you multiple ways to rename files. The most common method uses the mv (move) command. You can also use the rename command for bulk operations, or graphical tools if you prefer a visual interface. We will cover all these approaches step by step.
How To Rename A File In Linux Using The Mv Command
The mv command is the standard way to rename a single file. It works by moving the file from its current name to a new name. Think of it as giving the file a new identity while keeping it in the same location.
Open your terminal. The basic syntax is:
mv old_filename new_filename
For example, to rename a file called report.txt to final_report.txt, type:
mv report.txt final_report.txt
That is it. The file now has the new name. If you list the directory contents with ls, you will see the change.
You can also rename files in a different directory. Just include the full path:
mv /home/user/documents/old.txt /home/user/documents/new.txt
One common mistake is forgetting to include the file extension. Linux does not require extensions, but most applications expect them. Always keep the extension unless you have a reason to change it.
Moving And Renaming In One Step
The mv command can also move a file to a different directory while renaming it. For instance:
mv report.txt /home/user/backups/backup_report.txt
This moves the file to the backups folder and changes its name. This is handy when reorganizing files.
Renaming With Overwrite Protection
By default, mv will overwrite an existing file with the same name. To avoid accidental data loss, use the -i (interactive) flag:
mv -i old.txt new.txt
If new.txt already exists, the terminal will ask for confirmation before overwriting. Press y to confirm or n to cancel.
Another option is the -n flag, which never overwrites an existing file:
mv -n old.txt new.txt
This is useful in scripts where you want to avoid any risk.
How To Rename A File In Linux Using The Rename Command
The rename command is more powerful for bulk operations. It uses Perl expressions to match and change file names. This is ideal when you need to rename multiple files at once, such as changing file extensions or adding prefixes.
First, check if rename is installed on your system. On Debian-based distributions like Ubuntu, install it with:
sudo apt install rename
On Red Hat-based systems like Fedora, use:
sudo dnf install prename
The basic syntax is:
rename 's/old_pattern/new_pattern/' files
The s/old/new/ part is a substitution command. It replaces old_pattern with new_pattern in the file names.
Example: Changing File Extensions
Suppose you have several .txt files you want to rename to .md. Run:
rename 's/\.txt$/.md/' *.txt
This changes all .txt files in the current directory to .md. The $ ensures only the extension at the end is replaced.
Example: Adding A Prefix
To add “draft_” to the beginning of all .docx files:
rename 's/^/draft_/' *.docx
The ^ matches the start of the filename.
Example: Removing A Suffix
To remove “_backup” from file names:
rename 's/_backup//' *
This deletes the string “_backup” from all files in the directory.
Using The Rename Command With Caution
Always test the command first with the -n (dry run) flag. This shows what changes would be made without actually renaming anything:
rename -n 's/old/new/' *
Review the output. If everything looks correct, run the command without -n. This prevents costly mistakes.
The rename command is case-sensitive by default. Use the i flag for case-insensitive matching:
rename 's/old/new/i' *
How To Rename A File In Linux Using Graphical Tools
If you prefer a visual interface, Linux offers several file managers that make renaming easy. Most desktop environments come with a built-in file manager like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE).
Renaming With Nautilus
Open the Files application. Navigate to the file you want to rename. Right-click on the file and select “Rename” from the context menu. Alternatively, select the file and press F2. Type the new name and press Enter.
Nautilus also supports batch renaming. Select multiple files by holding Ctrl and clicking each file. Right-click and choose “Rename.” A dialog box appears where you can replace text, add numbering, or change case.
Renaming With Dolphin
In Dolphin, right-click a file and select “Rename.” Or press F2. For batch renaming, select multiple files, right-click, and choose “Rename.” Dolphin provides options to insert numbers, dates, or custom text.
Using GPRename For Advanced Batch Renaming
GPRename is a dedicated graphical tool for bulk renaming. Install it with:
sudo apt install gprename
Launch it from the terminal or application menu. Add files or folders, then apply rules like inserting text, changing case, or removing characters. It is intuitive and powerful.
How To Rename A File In Linux Using A Loop In Bash
For advanced users, a Bash loop gives you full control. This is useful when the rename command does not fit your needs. For example, to add a date stamp to all .jpg files:
for file in *.jpg; do
mv "$file" "$(date +%Y%m%d)_$file"
done
This renames each file with the current date as a prefix. The double quotes handle filenames with spaces.
To remove a specific string from all filenames:
for file in *; do
mv "$file" "${file//old_string/}"
done
This uses Bash parameter expansion to replace “old_string” with nothing.
Renaming With Find And Mv
Combine find with mv to rename files in subdirectories. For instance, to rename all .log files to .bak recursively:
find . -type f -name "*.log" -exec sh -c 'mv "$0" "${0%.log}.bak"' {} \;
This is complex but powerful for system administrators.
Common Mistakes And How To Avoid Them
Renaming files in Linux is simple, but beginners often make errors. Here are the most common ones and how to avoid them.
Forgetting The File Extension
If you rename data.csv to data, the file loses its extension. Applications may not recognize it. Always include the extension unless you intend to change it.
Overwriting Existing Files
Using mv without flags can overwrite files silently. Use -i or -n to prevent this.
Spaces In Filenames
If a filename has spaces, you must quote it. For example:
mv "my file.txt" "my new file.txt"
Without quotes, the command sees multiple arguments and fails.
Case Sensitivity
Linux filenames are case-sensitive. File.txt and file.txt are different files. Be precise when typing names.
Renaming Directories In Linux
You can rename directories the same way as files. Use mv or rename. For example:
mv old_folder new_folder
This renames the directory. All contents inside remain unchanged.
For batch renaming directories, use the rename command with caution. Test with -n first.
Renaming Files With Special Characters
Filenames with special characters like !, @, or # require careful handling. Use single quotes to prevent shell interpretation:
mv 'file!name.txt' 'new_file.txt'
Alternatively, escape the character with a backslash:
mv file\!name.txt new_file.txt
This ensures the shell treats the character literally.
Automating Renaming With Cron Jobs
If you need to rename files on a schedule, use a cron job. Create a script with your rename commands, then add it to crontab. For example, to rename log files daily:
0 2 * * * /home/user/rename_logs.sh
This runs the script at 2 AM every day. Ensure the script has execute permissions.
Renaming Files Across Multiple Directories
To rename files in multiple directories, use find with exec. For instance, to add “new_” to all .txt files in subdirectories:
find . -type f -name "*.txt" -exec bash -c 'mv "$0" "$(dirname "$0")/new_$(basename "$0")"' {} \;
This is advanced but very flexible.
Using Wildcards For Pattern Matching
Wildcards like * and ? help select multiple files. For example, to rename all .png files:
rename 's/.png/.jpg/' *.png
The * matches any number of characters. The ? matches a single character.
Renaming Files With A Counter
To rename files sequentially, use a loop with a counter:
i=1; for file in *.jpg; do
mv "$file" "image_$i.jpg"
((i++))
done
This renames files to image_1.jpg, image_2.jpg, etc. The order depends on how the shell lists files.
Undoing A Rename
There is no built-in undo for file renaming in Linux. Always double-check before executing. If you make a mistake, you can rename the file back manually. For bulk operations, consider using version control or backups.
One workaround is to use the trash-cli tool, which moves files to a trash folder instead of deleting them. This gives you a safety net.
Renaming Files With Different Encodings
Filenames with non-ASCII characters, like accented letters, can cause issues. Use the convmv tool to convert encoding:
convmv -f utf8 -t utf8 --nfc --notest file.txt
This normalizes the filename. Install convmv with your package manager.
Renaming Files In A Script
For repetitive tasks, write a Bash script. Save your commands in a file, make it executable with chmod +x script.sh, and run it. This ensures consistency.
Example script for renaming all .txt files to .md:
#!/bin/bash
for file in *.txt; do
mv "$file" "${file%.txt}.md"
done
Run it with ./script.sh.
Renaming Files With Python
If you prefer Python, use the os module:
import os
os.rename('old.txt', 'new.txt')
For bulk operations, iterate over files with os.listdir() or glob. Python gives you more control over complex patterns.
Renaming Files With Perl
Perl is the engine behind the rename command. You can write Perl scripts for advanced renaming. For example:
perl -e 'for (glob "*.txt") { rename $_, s/\.txt$/.md/r }'
This renames all .txt files to .md in one line.
Security Considerations
Be careful when renaming system files or files in shared directories. Incorrect renaming can break applications or permissions. Always test with a copy first.
Use ls -l to check file permissions before renaming. If you lack write permission, use sudo but only when necessary.
Frequently Asked Questions
How do I rename a file in Linux without using the terminal?
Use your file manager. Right-click the file and select “Rename,” or press F2. Most desktop environments support this.
What is the difference between mv and rename in Linux?
mv renames one file at a time or moves files. rename uses Perl expressions to rename multiple files based on patterns.
Can I rename a file in Linux with spaces in the name?
Yes, but you must quote the filename or escape the spaces. For example: mv "my file.txt" "new file.txt".
How do I rename multiple files at once in Linux?
Use the rename command with a substitution pattern, or use a Bash loop. Graphical tools like GPRename also work.
Is there a way to undo a file rename in Linux?
No built-in undo. You must rename the file back manually. Always double-check before renaming, or use a backup.
Renaming files in Linux is a skill you will use daily. Start with the mv command for single files. Move to rename for bulk tasks. Use graphical tools if you prefer a visual approach. Practice with test files to build confidence. Over time, you will find the method that works best for your workflow.
Remember to always check your commands with -n or -i