copy, move & ren
Three built-in commands cover everyday file shuffling: copy duplicates files, move relocates (or renames) files and folders, and ren renames in place.
copy
copy source destination copies one file, or many with wildcards. The destination can be a new file name or a folder:
C:\Users\ada\demo>copy notes.txt notes.bak
1 file(s) copied.
C:\Users\ada\demo>copy *.txt archive
notes.txt
todo.txt
2 file(s) copied.On Windows, copy reports what it did. In the runnable examples the report is redirected to nul (the "nowhere" device) so they focus on the result, which dir /b and type show:
@echo off
mkdir demo\archive & cd demo
echo buy milk> todo.txt
echo call Ada> notes.txt
copy notes.txt notes.bak >nul
copy *.txt archive >nul
echo --- demo:
dir /b /o:n
echo --- demo\archive:
dir /b /o:n archive
type archive\notes.txt--- demo:
archive
notes.bak
notes.txt
todo.txt
--- demo\archive:
notes.txt
todo.txt
call Ada
Tip: Write a trailing backslash when the destination is meant to be a folder:
copy report.pdf archive\. Ifarchivedoesn't exist,copy report.pdf archivesilently creates a file calledarchive, whilecopy report.pdf archive\fails with an error instead.
Overwriting
When the destination file already exists, copy typed at the prompt asks before replacing it:
C:\Users\ada\demo>copy todo.txt notes.txt
Overwrite notes.txt? (Yes/No/All): y
1 file(s) copied.Inside a batch file copy overwrites without asking. You can force the behaviour either way: /y never asks, /-y always asks. Scripts should say /y explicitly so their behaviour is obvious:
@echo off
mkdir demo & cd demo
> config.ini echo version 1
> config.new echo version 2
copy /y config.new config.ini >nul
type config.iniversion 2
The example writes > config.ini echo version 1 instead of echo version 1> config.ini on purpose: CMD would read 1> as "redirect stream 1" and write the wrong text. The Redirection lesson explains why.
Joining files with +
copy can concatenate files: list the sources joined by + and give one destination. This is a quick way to merge CSV parts or log chunks:
@echo off
mkdir demo & cd demo
echo id,name> header.csv
echo 1,Ada> part1.csv
echo 2,Grace> part2.csv
copy /b header.csv+part1.csv+part2.csv all.csv >nul
type all.csvid,name
1,Ada
2,Grace
Always add /b (binary) when joining files. Without it, copy treats the sources as old-style text: it stops reading at a Ctrl+Z character and appends a Ctrl+Z (byte 0x1A) to the result, which can confuse other programs.
move
move takes files out of one place and puts them in another. Unlike copy, it also works on folders, and if the destination is a new name in the same folder, it simply renames:
@echo off
mkdir demo\inbox demo\done & cd demo
echo invoice> inbox\invoice-17.pdf
echo receipt> inbox\receipt-03.pdf
move inbox\invoice-17.pdf done >nul
move inbox\*.pdf done >nul
move done finished >nul
echo --- demo:
dir /b /o:n
echo --- demo\finished:
dir /b /o:n finished--- demo:
finished
inbox
--- demo\finished:
invoice-17.pdf
receipt-03.pdf
The last move renamed the folder done to finished. On the same drive a move is instant, because Windows only updates the folder entries; across drives it copies the data and then deletes the original. Like copy, move prints a summary on Windows ( 1 file(s) moved.) and asks before overwriting at the prompt; /y turns the question off.
ren: rename in place
ren (or rename) changes a name without moving anything. The new name must be a name only, not a path:
@echo off
mkdir demo & cd demo
echo draft> report-draft.txt
ren report-draft.txt report-final.txt
mkdir old_stuff
ren old_stuff archive
dir /b /o:narchive
report-final.txt
ren can also rename many files at once with wildcards. Each * or ? in the new name keeps the matching characters of the old name, so changing extensions is easy:
@echo off
mkdir demo & cd demo
for %%n in (jan.log feb.log mar.log) do echo data> %%n
ren *.log *.txt
dir /b /o:nfeb.txt
jan.txt
mar.txt
Wildcard renames that change the start of names (for example adding a prefix) don't work the way you might hope; use a for loop for those, as shown in Batch for Loops.
These are the messages Windows prints when ren can't do its job:
C:\Users\ada\demo>ren missing.txt new.txt
The system cannot find the file specified.
C:\Users\ada\demo>ren jan.txt feb.txt
A duplicate file name exists, or the file
cannot be found.
C:\Users\ada\demo>ren jan.txt archive\jan.txt
The syntax of the command is incorrect.Command summary
| Task | CMD | PowerShell | bash |
|---|---|---|---|
| copy a file | copy a.txt b.txt |
Copy-Item a.txt b.txt |
cp a.txt b.txt |
| copy into a folder | copy *.txt backup\ |
Copy-Item *.txt backup |
cp *.txt backup/ |
| copy a folder tree | xcopy / robocopy |
Copy-Item -Recurse |
cp -r |
| move / rename | move a.txt dir\ / ren a.txt b.txt |
Move-Item / Rename-Item |
mv |
| overwrite silently | /y |
-Force |
-f |
Copying whole folder trees needs xcopy or robocopy, the subject of the xcopy & robocopy lesson.
Learn more: Copy, Move & Rename · Bash cp & mv
Test yourself
What does copy a.txt+b.txt c.txt do?
- Copies a.txt and b.txt into the folder c.txt
- Creates c.txt containing a.txt followed by b.txt
- Adds c.txt to the end of a.txt
- It is a syntax error
Which command renames the folder old to archive in the current folder?
- copy old archive
- ren old archive
- rd old archive
- mkdir old archive
Inside a batch file, what happens when copy targets an existing file and you didn't give /y or /-y?
- It always asks for confirmation
- It overwrites without asking
- It skips the file
- It appends to the file