TExcelTAD
Home › Terminal & Command Line › Copy, Move & Rename

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:

cmd · try it yourself
@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 /s
C:\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:

  • ren takes only a new name, not a path: ren docs\a.txt b.txt is right, ren docs\a.txt docs\b.txt is an error.
  • ren and copy accept wildcards, so ren *.htm *.html renames every .htm file at once.
  • When you type copy at the prompt and the target exists, CMD asks Overwrite b.txt? (Yes/No/All). Inside a batch file it overwrites silently. /y and /-y force either behaviour.
  • copy copies files only. For whole folder trees use xcopy /e /i or, better, robocopy.
cmd · try it yourself
@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-backup
C:\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):

cmd
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         0

Windows (PowerShell)

PowerShell uses Copy-Item, Move-Item and Rename-Item. Folders need -Recurse to copy their contents:

powershell · try it yourself
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 -Name
report.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:

powershell · try it yourself
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 -Name
What 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):

bash · try it yourself
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:

bash · try it yourself
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.txt
old
'a.txt' -> 'b.txt'
new

For bulk renaming, use a loop (bash) or zsh's zmv function, which works like a pattern-based rename:

zsh · try it yourself
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'
ls
mv -- 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 -r works on macOS too. To keep Mac-specific metadata (extended attributes, resource forks) use cp -R with -p, or ditto 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?

  1. ren old.txt new.txt
  2. rename-item old.txt new.txt
  3. mv old.txt new.txt
  4. cp -r old.txt new.txt

Which PowerShell parameter previews a change without making it?

  1. -Force
  2. -WhatIf
  3. -Recurse
  4. -Preview

Why does cp project backup fail when project is a folder?

  1. cp can't copy to a new name
  2. Folders need -r (recursive)
  3. backup must exist first
  4. cp only works on text files