How to Rename a File Using Mac Terminal: mv, Spaces, Batch

Updated

rename file mac terminal

To rename a file in Mac Terminal, run mv oldname.txt newname.txt. mv (move) takes the current name first and the new name second, and renames the file when both are in the same folder. macOS has no separate rename command. If newname.txt already exists, mv usually replaces it without asking, so add -i or -n when you aren’t sure.

I ran the commands here on macOS 26.6 (Tahoe) in Terminal’s default zsh shell, apart from the Homebrew install, and checked each flag against man mv. Run man mv to see the flags on your Mac.

Rename a file in Terminal with mv

  1. Go to the file’s folder, for example cd ~/Documents. Or type cd and a space, then drag the folder from Finder into Terminal to insert its path.
  2. Run ls to list the file names.
  3. Run mv oldname.txt newname.txt, then ls again to check the new name.

Rename a file with spaces or special characters

A space ends a name unless you quote it or put a backslash before it:

mv "file with spaces.txt" "renamed spaces.txt"
mv file\ with\ spaces.txt renamed\ spaces.txt

Without them, mv file with spaces.txt renamed spaces.txt hands mv five names and stops with mv: spaces.txt is not a directory. Pressing Tab after the first letters of a name makes zsh complete it and add the backslashes. For names with $, !, parentheses or &, use single quotes, which make everything inside literal. Double quotes still expand $name.

mv 'report (final) & $draft.txt' report-final.txt

A name that starts with a dash looks like a flag: mv -leadingdash.txt notdash.txt prints mv: illegal option -- l. Put -- before the names, or start the name with ./:

mv -- -leadingdash.txt notdash.txt
mv ./-leadingdash.txt notdash.txt

Stop mv from overwriting an existing file

If report.txt already exists, mv report-draft.txt report.txt deletes it, doesn’t move it to the Trash, and prints nothing. Two flags prevent that:

mv -i report-draft.txt report.txt
mv -n report-draft.txt report.txt

-i asks first: overwrite report.txt? (y/n [n]). A reply starting with y or Y goes ahead; anything else, including a bare Return, keeps the file. -n never overwrites and skips the move without a word. Add -v to see what happened: report-draft.txt -> report.txt after a move, report.txt not overwritten after a skip. With -f, -i and -n together, the last one you type wins. A read-only target makes mv ask even without -i.

-n exits with status 0 even when it skips, so a script that checks only the exit status can’t tell. Apple’s man page also calls -n non-standard and not recommended in scripts. The batch file-extension guide covers that trap inside a rename loop.

Rename a folder in Terminal

Folders use the same command, and you don’t need -r:

mv "Old Folder" "New Folder"

If New Folder already exists, nothing is renamed. mv moves Old Folder inside it, which -v shows as Old Folder -> New Folder/Old Folder. -n doesn’t stop this, because New Folder/Old Folder doesn’t exist yet.

Change only the capitalization of a name

The default APFS format on a Mac ignores case in names but keeps the capitals you type, so file.txt and FILE.txt are one file. A plain mv changes the capitalization in place:

mv file.txt FILE.txt

Leave -n and -i off a case-only rename. FILE.txt already “exists” as far as the disk is concerned, so -n skips the rename silently (-v prints FILE.txt not overwritten) and -i asks overwrite FILE.txt? although nothing is at risk.

New name is mv mv -n mv -i
Not used by any file renames renames renames
Another existing file replaces it skips, exit 0 asks first
Same name, different capitals renames skips, exit 0 asks, nothing at risk

On a volume formatted as APFS (Case-sensitive), file.txt and FILE.txt are two files, and -n works normally.

Rename many files at once in Terminal

mv renames one file per command, so use a zsh for loop for a batch. Preview with echo first, then run it for real:

for f in *.txt; do echo "$f -> ${f%.txt}-new.txt"; done
for f in *.txt; do mv -nv -- "$f" "${f%.txt}-new.txt"; done

${f%.txt} is the name without .txt, the quotes keep names with spaces whole, -n skips names that would overwrite a file, and -v lists each rename. If nothing matches, zsh prints zsh: no matches found: *.txt and runs nothing. This loop swaps spaces for underscores:

for f in *\ *; do mv -nv -- "$f" "${f// /_}"; done

zmv does the same from one pattern and checks every new name before renaming anything. It ships with zsh on macOS but must be loaded once per window with the first line below (add that line to ~/.zshrc to load it every time). Keep the single quotes:

autoload -U zmv
zmv -n '(*).txt' '$1-new.txt'
zmv '(*).txt' '$1-new.txt'

The -n run prints what it would do, such as mv -- a.txt a-new.txt. Here -n means “don’t run”, not mv‘s “don’t overwrite”. What the parentheses match becomes $1. If a new name is already taken, or two files would get the same name, zmv stops and renames nothing. Its -f flag allows overwriting existing files. zmv '(*)' '${(L)1}' lowercases every name, case-only changes included. zmv needs zsh; the loops also work in bash.

What mv doesn’t do

  • Rename by pattern. mv *.txt *.md doesn’t turn each .txt file into a .md file. The shell expands both wildcards first, so you get zsh: no matches found: *.md if there are no .md files, or mv: y.md is not a directory if three or more files match. With exactly one .txt and one .md file, mv replaces the .md file with the .txt file without asking. Use the loop or zmv. macOS also has no rename command; brew install rename adds a Perl-based one.
  • Create folders. mv a.txt nodir/b.txt fails with mv: rename a.txt to nodir/b.txt: No such file or directory until you run mkdir nodir. A mistyped source name gives the same message.
  • Convert a file. Renaming photo.heic to photo.jpg changes only the name. It’s still a HEIC image.
  • Keep the original or undo. The old name disappears and there’s no undo. Rename back with mv newname.txt oldname.txt, or copy with cp oldname.txt newname.txt to keep both. Copying has its own macOS quirk: cp -r and cp -R are the same command on Linux and are not on macOS.

Rename a file in Finder instead

In Finder, select the file, press Return, type the new name and press Return again. To rename several files, select them, Control-click one, choose Rename, then replace text, add text or change the name format.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.