How to rename a file in Linux: mv oldname newname for single files (also moves between directories). For batch renames, use rename 's/old/new/' *.txt (Perl-based) or shell loops. For git-tracked files, use git mv to keep git history clean. The team that uses mv for single, rename for batch, and git mv for tracked files has the right tool for each.
Table of contents
- mv command
- rename (batch patterns)
- Shell loop (no rename)
- git mv (for tracked files)
- Rename a directory
- Cross-filesystem rename
- Quirks
- FAQ
mv command
Single file:
mv oldname.txt newname.txt
Same command moves between directories:
mv file.txt /path/to/destination/
mv /path/to/file.txt /new/path/newname.txt
The team that uses mv for single renames has the standard tool.
rename (batch patterns)
For multiple files, use rename (Perl-based, available via perl package):
# Rename all .txt to .md
rename 's/\.txt$/.md/' *.txt
# Add prefix
rename 's/^/old_/' *.log
# Replace spaces with underscores
rename 's/ /_/g' *
The team that has 100+ files to rename uses rename.
Shell loop (no rename)
If rename isn’t available:
for f in *.txt; do
mv "$f" "${f%.txt}.md"
done
The team that uses shell loops has full control without external dependencies.
git mv (for tracked files)
git mv oldname.txt newname.txt
Same as mv but git tracks the rename. The team that uses git mv has clean git history (no add+delete).
Rename a directory
mv olddir newdir
Same as file rename. The team that renames a directory has both the directory and its contents moved.
Cross-filesystem rename
On the same filesystem, mv is a rename (instant). Across filesystems, it’s copy + delete (slower):
mv /home/file.txt /mnt/external/
# If /mnt is different filesystem: copy + delete
The team that has large files crossing filesystems has slow renames.
Quirks
mvoverwrites without confirmation. Usemv -ifor interactive.- Spaces in filenames: quote them.
mv "old name.txt" "new name.txt". - Hidden files (starting with
.): not matched by*. Usemv .hidden .newnamedirectly.
FAQ
Is mv atomic?
On the same filesystem, yes. Across filesystems, no (it’s copy + delete).
What’s the difference between mv and rename?
mv: single file/dir. rename: batch pattern. Use the right tool for the job.
Can I undo a rename?
Not directly. Use git to revert (if tracked). Otherwise, restore from backup.
How do I rename and keep the file in git?
git mv instead of mv. The team that uses git mv has git track the rename as a rename, not add+delete.
What about case-insensitive filesystems?
On macOS (HFS+ default) and Windows, renaming from File to file may be a no-op. On Linux (case-sensitive), it’s a real rename.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.
Useful related references: