rename file in linux is a filesystem operation to change a file’s name using mv for single files or rename for batch substring replacement.
mv [options] source target
rename [options] substring replacement file...
Syntax
mv [options] source target
rename [options] substring replacement file...
Options and Flags
| Flag | Type | Default | Description |
|---|---|---|---|
-i |
mv |
Off | Prompt before overwrite. |
-f |
mv |
Off | Force overwrite without prompt. |
-v |
mv / rename |
Quiet | Verbose output showing operations. |
-n |
mv |
Off | No‑clobber: do not overwrite existing target. |
-u |
mv |
Off | Update: move only when source is newer than target or missing. |
-s / --symlink |
rename |
N/A | Rename symlink target instead of symlink itself. |
-n / --no-act |
rename |
Off | Dry run: print what would be done without making changes. |
-o / --no-overwrite |
rename |
Off | Do not overwrite existing files; skip instead. |
-a / --all |
rename |
First occurrence | Replace every occurrence of substring (not just first). |
-c / --copy |
rename |
Off | Copy files instead of renaming; preserve permission bits (S_IRWXU|S_IRWXG|S_IRWXO). |
Usage Examples
1. Rename a single file interactively
mv -i config.conf config.conf.old
Prompts before overwriting if target exists. Use -v for verbose output.
2. Batch rename file extensions
rename '.txt' '.csv' *.txt
Replaces the first occurrence of .txt with .csv in every matching file. To replace every occurrence, add --all.
3. Dry-run batch rename
rename -n '2024_' '2025_' report_*.pdf
Dry‑run mode (-n) prints what would be renamed without executing. Replace 2024_ with 2025_ in filenames.
4. Force overwrite without prompt
mv -f /tmp/important.conf /etc/important.conf
Force overwrites the destination file without confirmation. Use with caution in scripts.
Troubleshooting & Common Errors
| Error Message | Root Cause | Resolution Command |
|---|---|---|
mv: cannot stat 'file': Permission denied |
Insufficient read/write permissions on source directory | ls -l file to check ownership; sudo mv or chmod before rename |
bash: rename: command not found |
rename not installed |
sudo apt install rename (Debian/Ubuntu) or sudo yum install prename (RHEL/CentOS) |
rename: can't rename 'foo' to 'bar': No such file or directory |
Regex mismatch or path issue | Use rename -n to test pattern; verify files exist with ls |
mv: are you sure? (y/n) – unexpected input |
Interactive mode in automated script | Use mv -f or redirect stdin: echo y | mv -i old new (avoid in scripts) |
Frequently Asked Questions
What is the difference between mv and rename?
Answer: mv renames a single file or directory by specifying old and new names; rename applies substring replacement to multiple files using a pattern. Use mv for individual operations (mv file.txt newfile.txt). Use rename for bulk renaming, e.g., rename '.txt' '.md' *.txt replaces .txt with .md. The util-linux rename only does simple substring replacement; the Perl version supports regex.
When should I use the -v flag with mv?
Answer: Use mv -v to log or visually confirm each rename operation, especially in scripts or when auditing batch renames. It prints source and destination after each successful move.
How do I fix ‘mv: cannot stat’ error?
Answer: Verify the source file path exists and is accessible. Common causes: wrong working directory, trailing spaces, or hidden characters. Use pwd and ls -la to inspect.
Does rename work on AWS EC2 Linux instances?
Answer: Yes, both mv and rename are available on EC2 instances running Amazon Linux 2, Ubuntu, RHEL, or any standard Linux distribution. Install the Perl rename if needed: sudo apt install rename (Debian/Ubuntu) or sudo yum install prename (RHEL/Amazon Linux).
What is the fastest way to rename multiple files with a pattern?
Answer: Use the rename command with a single substring substitution. Preview with -n (dry run) then execute without it: rename -n 'jpeg' 'jpg' *.jpeg to test; remove -n to apply. For bulk prepend/append, use rename 'prefix' '' * or rename '' 'suffix' * (with caution).

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.