Copy, Move & Rename
Copying makes a second file with the same content; moving puts the original somewhere else; renaming changes its name in place. On Linux and macOS, renaming is simply a move within the same folder, so one command (mv) does both. Windows shells have separate commands for renaming.
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Copy a file | copy a.txt b.txt |
Copy-Item a.txt b.txt (cp, copy) |
cp a.txt b.txt |
| Copy into a folder | copy a.txt backup\ |
Copy-Item a.txt backup/ |
cp a.txt backup/ |
| Copy a folder tree | xcopy src dst /e /i, robocopy src dst /e |
Copy-Item src dst -Recurse |
cp -r src dst |
| Move | move a.txt archive\ |
Move-Item a.txt archive/ (mv, move) |
mv a.txt archive/ |
| Rename | ren a.txt b.txt |
Rename-Item a.txt b.txt (ren) |
mv a.txt b.txt |
| Ask before overwriting | copy /-y (default when typed interactively) |
-Confirm |
cp -i, mv -i |
| Never overwrite | — | if (-not (Test-Path …)) |
cp -n, mv -n |
| Overwrite without asking | copy /y |
-Force |
default |
Windows (CMD)
copy copies files, move moves files and folders, and ren (or rename) renames. Both copy and move report what they did ("1 file(s) copied."), which is why scripts often send that message to nul:
@echo off
mkdir work
cd work
echo version 1> report.txt
mkdir backup archive
copy report.txt backup\ > nul
copy report.txt report-copy.txt > nul
ren report-copy.txt draft.txt
move draft.txt archive\ > nul
dir /b /o:n /sC:\Users\learner\work\archive
C:\Users\learner\work\backup
C:\Users\learner\work\report.txt
C:\Users\learner\work\archive\draft.txt
C:\Users\learner\work\backup\report.txt
A few rules worth knowing:
rentakes only a new name, not a path:ren docs\a.txt b.txtis right,ren docs\a.txt docs\b.txtis an error.renandcopyaccept wildcards, soren *.htm *.htmlrenames every.htmfile at once.- When you type
copyat the prompt and the target exists, CMD asks Overwrite b.txt? (Yes/No/All). Inside a batch file it overwrites silently./yand/-yforce either behaviour. copycopies files only. For whole folder trees usexcopy /e /ior, better,robocopy.
@echo off
mkdir site\css
echo ^<h1^>Home^</h1^>> site\index.html
echo body{}> site\css\main.css
xcopy site site-backup /e /i /q > nul
dir /b /o:n /s site-backupC:\Users\learner\site-backup\css
C:\Users\learner\site-backup\index.html
C:\Users\learner\site-backup\css\main.css
/e copies sub-folders (even empty ones) and /i tells xcopy the destination is a folder. robocopy ("robust copy") is the professional tool: it resumes after errors, copies only what changed, and can mirror a folder. Its output is a detailed report (sample output, shortened):
C:\Users\ada>robocopy site D:\backup\site /e
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Source : C:\Users\ada\site\
Dest : D:\backup\site\
...
Total Copied Skipped Mismatch FAILED Extras
Dirs : 2 2 0 0 0 0
Files : 2 2 0 0 0 0Windows (PowerShell)
PowerShell uses Copy-Item, Move-Item and Rename-Item. Folders need -Recurse to copy their contents:
New-Item -ItemType Directory work/backup, work/archive -Force | Out-Null
Set-Location work
Set-Content report.txt 'version 1'
Copy-Item report.txt backup/
Copy-Item report.txt report-copy.txt
Rename-Item report-copy.txt draft.txt
Move-Item draft.txt archive/
Get-ChildItem -Recurse -File -Namereport.txt
archive/draft.txt
backup/report.txt
Rename-Item can compute new names with a script block, which makes bulk renaming a one-liner. -WhatIf shows what would happen without doing it, a great habit before any bulk change:
New-Item -ItemType Directory photos -Force | Out-Null
'IMG_001.JPG', 'IMG_002.JPG' | ForEach-Object { New-Item "photos/$_" | Out-Null }
Get-ChildItem photos | Rename-Item -NewName { $_.Name.ToLower() -replace '^img_', 'holiday-' } -WhatIf
Get-ChildItem photos | Rename-Item -NewName { $_.Name.ToLower() -replace '^img_', 'holiday-' }
Get-ChildItem photos -NameWhat if: Performing the operation "Rename File" on target "Item: /tmp/photos/IMG_001.JPG Destination: /tmp/photos/holiday-001.jpg".
What if: Performing the operation "Rename File" on target "Item: /tmp/photos/IMG_002.JPG Destination: /tmp/photos/holiday-002.jpg".
holiday-001.jpg
holiday-002.jpg
Copy-Item overwrites an existing file silently, even at the interactive prompt, but Move-Item refuses to replace an existing file unless you add -Force.
Linux & macOS (bash/zsh)
cp copies, mv moves and renames. If the last argument is an existing folder, the files go inside it. Folders need cp -r (recursive):
mkdir -p work/backup work/archive && cd work
echo "version 1" > report.txt
cp report.txt backup/
cp report.txt report-copy.txt
mv report-copy.txt draft.txt
mv draft.txt archive/
find . -type f | sort
echo ---
cd .. && cp -r work work-copy && ls work-copy./archive/draft.txt
./backup/report.txt
./report.txt
---
archive
backup
report.txt
cp and mv overwrite without asking. -i asks first, -n never overwrites, and -v reports each action:
echo "new" > a.txt
echo "old" > b.txt
cp -n a.txt b.txt
cat b.txt
cp -v a.txt b.txt
cat b.txtold
'a.txt' -> 'b.txt'
new
For bulk renaming, use a loop (bash) or zsh's zmv function, which works like a pattern-based rename:
autoload -U zmv
mkdir photos && touch photos/IMG_001.JPG photos/IMG_002.JPG
cd photos
zmv -n 'IMG_(*).JPG' 'holiday-$1.jpg'
zmv 'IMG_(*).JPG' 'holiday-$1.jpg'
lsmv -- IMG_001.JPG holiday-001.jpg
mv -- IMG_002.JPG holiday-002.jpg
holiday-001.jpg
holiday-002.jpg
zmv -n is a dry run, like PowerShell's -WhatIf. In bash the same job is a for loop with parameter expansion: for f in IMG_*.JPG; do mv "$f" "holiday-${f#IMG_}"; done.
macOS note:
cp -rworks on macOS too. To keep Mac-specific metadata (extended attributes, resource forks) usecp -Rwith-p, orditto src dst. For large or repeated copies on any Unix system,rsync -a src/ dst/copies only what changed.
Learn more: cp & mv · copy, move & ren · xcopy & robocopy · PowerShell Files & Folders
Test yourself
How do you rename old.txt to new.txt in bash?
ren old.txt new.txtrename-item old.txt new.txtmv old.txt new.txtcp -r old.txt new.txt
Which PowerShell parameter previews a change without making it?
-Force-WhatIf-Recurse-Preview
Why does cp project backup fail when project is a folder?
cpcan't copy to a new name- Folders need
-r(recursive) backupmust exist firstcponly works on text files