To rename a Linux file, you can use the `mv` command followed by the current and desired filenames. This article will teach you exactly how to rename linux file using simple commands, graphical tools, and bulk renaming tricks. You don’t need to be a terminal expert—just follow these steps and you’ll be renaming files like a pro in no time.
Linux gives you many ways to rename files. The most common method is the `mv` command, which stands for “move.” In Linux, renaming is just moving a file to a new name in the same location. Let’s start with the basics and then move to advanced techniques.
How To Rename Linux File Using The Mv Command
The `mv` command is your go-to tool for renaming single files. It’s simple, fast, and works on every Linux distribution. Here’s the basic syntax:
mv [options] current_filename new_filename
For example, to rename a file called “old.txt” to “new.txt,” you would type:
mv old.txt new.txt
That’s it. The file is now renamed. No confirmation message appears unless there’s an error. If the new filename already exists, `mv` will overwrite it without asking. To avoid accidental overwrites, use the `-i` (interactive) option:
mv -i old.txt new.txt
This prompts you before overwriting. Type “y” to confirm or “n” to cancel.
Renaming Files With Paths
You can rename files in other directories without changing your current location. Just include the full or relative path:
mv /home/user/documents/report.txt /home/user/documents/final_report.txt
This renames “report.txt” to “final_report.txt” inside the documents folder. You stay in your current directory.
Using The Verbose Option
If you want to see what `mv` is doing, add the `-v` (verbose) flag:
mv -v old.txt new.txt
Output: renamed 'old.txt' -> 'new.txt'. This is helpful when renaming multiple files in scripts.
Renaming Multiple Files With A Loop
When you need to rename several files at once, a simple `for` loop in the terminal works wonders. For example, to add a prefix “backup_” to all .txt files:
for file in *.txt; do mv "$file" "backup_$file"; done
This loops through every .txt file and renames it by adding “backup_” at the beginning. The quotes around `$file` handle filenames with spaces.
Changing File Extensions In Bulk
To change extensions from .txt to .md for all text files:
for file in *.txt; do mv "$file" "${file%.txt}.md"; done
The `${file%.txt}` part removes the .txt extension, then you add .md. This is a clean way to convert file types.
Renaming Files With A Counter
Need numbered filenames? Use a counter variable:
i=1; for file in *.jpg; do mv "$file" "image_$i.jpg"; i=$((i+1)); done
This renames all .jpg files to image_1.jpg, image_2.jpg, and so on. Adjust the starting number as needed.
Using The Rename Command For Advanced Bulk Renaming
The `rename` command is more powerful than a loop. It uses Perl expressions to rename files based on patterns. Install it first if missing:
sudo apt install rename # Debian/Ubuntu
sudo yum install rename # RHEL/CentOS
Basic syntax: rename 's/old_pattern/new_pattern/' files. For example, to replace all spaces with underscores in filenames:
rename 's/ /_/g' *
This changes “my file.txt” to “my_file.txt”. The `g` flag makes it global (all spaces in the name).
Changing Case With Rename
To convert all filenames to lowercase:
rename 'y/A-Z/a-z/' *
Or to uppercase:
rename 'y/a-z/A-Z/' *
This is useful for standardizing filenames across your system.
Renaming Files With Date Stamps
Add a date prefix to files using `rename` and shell expansion:
rename 's/^/2025-03-27_/' *.log
This prepends “2025-03-27_” to all .log files. Combine with `date` command for dynamic dates.
How To Rename Linux File Using Graphical Tools
If you prefer a visual interface, Linux offers several file managers with rename capabilities. Nautilus (GNOME), Dolphin (KDE), and Thunar (XFCE) all support renaming.
Renaming In Nautilus
Right-click a file and select “Rename.” Type the new name and press Enter. For bulk renaming, select multiple files, right-click, and choose “Rename…” A dialog appears with options:
- Replace text: Find and replace characters
- Add text: Insert text at beginning or end
- Rename with numbers: Auto-number files
- Change case: Convert to uppercase/lowercase
This is great for users who avoid the terminal.
Using GPRename For Batch Operations
GPRename is a dedicated bulk renaming tool. Install it with:
sudo apt install gprename
Launch it from the menu. Add files, then apply rules like insert, delete, replace, or numbering. It shows a preview before renaming.
Renaming Files With Special Characters
Filenames with spaces, hyphens, or symbols require care. Always quote the filename or escape special characters. For example:
mv "my file.txt" "my_file.txt"
mv my\ file.txt my_file.txt
Both commands work. The backslash escapes the space. For filenames with dollar signs or backticks, use single quotes to prevent shell expansion:
mv '$file.txt' 'new_file.txt'
Renaming Hidden Files
Hidden files start with a dot (.). To rename them, include the dot:
mv .hidden .visible
Be careful—renaming hidden files can affect system configurations.
Using The Mmv Command For Pattern-Based Renaming
The `mmv` command (mass move) handles complex patterns. Install it first:
sudo apt install mmv
Example: Rename all files with “abc” to “xyz” in the middle of the name:
mmv '*abc*' '#1xyz#2'
The `#1` and `#2` capture parts of the original name. This is more intuitive than Perl expressions for some users.
Renaming Files With Date And Time
To add a timestamp to a file:
mv report.txt "report_$(date +%Y%m%d_%H%M%S).txt"
This creates “report_20250327_143022.txt”. The `date` command inserts the current date and time.
How To Rename Linux File Safely
Always double-check before renaming, especially with bulk operations. Here are safety tips:
- Use `-i` (interactive) with `mv` to avoid overwrites
- Test `rename` commands with `-n` (dry run) first:
rename -n 's/old/new/' * - Backup important files before bulk renaming
- Use version control (like Git) for critical files
Undoing A Rename
If you rename a file and regret it, rename it back immediately. There’s no undo command. Keep a log of changes if needed:
mv new.txt old.txt
For bulk operations, save the original list of filenames before renaming.
Renaming Directories
The same `mv` command works for directories. To rename a folder:
mv old_folder new_folder
This renames the directory. All contents inside remain unchanged. Use the same options like `-i` and `-v`.
Renaming With Symbolic Links
If you rename a file that has symbolic links pointing to it, the links break. Update them manually or use `ln -sf` to create new links:
ln -sf /path/to/new_file link_name
Plan ahead to avoid broken links.
Scripting Rename Operations
For repetitive tasks, write a bash script. Here’s a simple example that adds a prefix to all .txt files:
#!/bin/bash
for file in *.txt; do
mv "$file" "prefix_$file"
done
Save it as `rename_script.sh`, make it executable with `chmod +x rename_script.sh`, and run it with `./rename_script.sh`.
Using Find With Exec For Complex Renames
The `find` command combined with `exec` can rename files in subdirectories. For example, rename all .html files to .php in a directory tree:
find . -type f -name "*.html" -exec bash -c 'mv "$0" "${0%.html}.php"' {} \;
This searches recursively and renames each file. Test with `-exec echo` first to see what will happen.
Common Mistakes And How To Avoid Them
Even experienced users make errors. Here are frequent pitfalls:
- Forgetting quotes around filenames with spaces—always quote
- Overwriting files accidentally—use `-i` flag
- Renaming system files—never rename files in /etc, /bin, or /usr without knowing the consequences
- Using wildcards incorrectly—test with `echo` first:
echo *.txt - Not checking current directory—ensure you’re in the right folder
Recovering From A Mistake
If you overwrite a file, check for backups. Many Linux systems have snapshot tools or you can use `extundelete` to recover deleted files. Prevention is better than cure.
How To Rename Linux File With Case Sensitivity
Linux filenames are case-sensitive. “File.txt” and “file.txt” are different. To change case, use `rename` or a loop:
for file in *; do mv "$file" "${file,,}"; done
This converts all filenames to lowercase using bash parameter expansion. The `,,` operator lowers the case.
Renaming Files With Unicode Characters
Modern Linux supports Unicode filenames. Use quotes to handle them:
mv "résumé.txt" "resume.txt"
Be aware that some tools may not display Unicode correctly. Stick to ASCII for portability.
Using Midnight Commander For Visual Renaming
Midnight Commander (mc) is a text-based file manager. Install it with:
sudo apt install mc
Navigate to the file, press F6 to rename, type the new name, and press Enter. It’s fast and works over SSH.
Renaming In Vim Or Emacs
You can rename files from within text editors. In Vim, use:
:!mv % new_filename
In Emacs, use `M-x dired` and then `R` to rename. These methods are for advanced users.
Performance Tips For Large Numbers Of Files
Renaming thousands of files can be slow. Use efficient commands:
- Avoid loops with `ls`—use `for file in *` instead
- Use `rename` for pattern-based bulk operations
- Consider using `parallel` for multi-core renaming
- Test on a small sample first
Renaming Files With Null Separators
For filenames with newlines or weird characters, use `find` with `-print0` and `xargs -0`:
find . -type f -name "*.txt" -print0 | xargs -0 -I {} mv {} {}_backup
This handles any filename safely.
Conclusion
Now you know multiple ways to rename files in Linux. Start with the `mv` command for single files, then explore loops and `rename` for bulk operations. Graphical tools like Nautilus and GPRename offer user-friendly alternatives. Always test commands with dry runs or backups to avoid data loss. With practice, you’ll master file renaming and boost your productivity.
Frequently Asked Questions
How Do I Rename A File In Linux Without Using The Terminal?
Use a graphical file manager like Nautilus. Right-click the file, select “Rename,” type the new name, and press Enter. For bulk renaming, select multiple files and use the rename dialog.
What Is The Difference Between Mv And Rename In Linux?
The `mv` command renames a single file or moves it. The `rename` command uses Perl expressions for bulk renaming based on patterns. `mv` is simpler; `rename` is more powerful for complex tasks.
Can I Rename Multiple Files At Once In Linux?
Yes, use a `for` loop in bash, the `rename` command, or graphical tools like GPRename. All methods allow bulk renaming with patterns, numbering, or text replacement.
How Do I Rename A File With Spaces In Linux?
Quote the filename: `mv “old file.txt” “new file.txt”`. Or escape spaces with backslashes: `mv old\ file.txt new\ file.txt`. Always use quotes to avoid errors.
What Happens If I Rename A File That Is In Use?
Linux allows renaming open files. The file descriptor remains valid, and the process continues using the new name. However, some applications may break if they rely on the old path.
Remember to practice these commands in a safe directory first. Renaming files is a fundamental skill that will save you time and frustration. Happy renaming!