cheat sheet
copy
Copy one or more files to a new location in the Windows command prompt. Covers single-file copy, wildcard batches, file concatenation, and binary vs ASCII modes.
copy — Copy Files
What it is
copy is a built-in cmd.exe command that copies one or more files to a destination file or directory, and can also concatenate multiple source files into a single destination file. Present since MS-DOS 1.0, it handles simple flat-file copying well; for recursive tree copies, preserving attributes, or network paths, prefer xcopy or robocopy instead.
Availability
copy is built into cmd.exe on every Windows version. PowerShell's equivalent is Copy-Item (aliased cp, copy).
copy /?
Output:
Copies one or more files to another location.
COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ ...]] [destination [/A | /B]]
Syntax
The source can be a filename or wildcard; the destination can be a file name, a directory (trailing \ or existing folder name), or omitted to copy into the current directory.
copy [switches] source [destination]
copy source1 + source2 [destination]
Output: (shows count of files copied, or error)
Essential options
| Switch | Meaning |
|---|---|
/Y | Overwrite existing destination without prompting |
/-Y | Prompt before overwriting (default when interactive) |
/V | Verify that the copy is identical to the source |
/A | Treat file(s) as ASCII text (stop at Ctrl+Z / EOF marker) |
/B | Treat file(s) as binary (copy exact byte count — default for single files) |
/D | Allow the destination to be decrypted if source is EFS-encrypted |
/Z | Copy in restartable mode (safe for network interruptions) |
/N | Use short (8.3) filename if available |
Copying a single file
The simplest form copies one file to another name or into a directory. If the destination is an existing directory, copy places the file inside it with the same name.
rem Copy to a new name in the same folder
copy report.docx report_backup.docx
Output:
1 file(s) copied.
rem Copy into a directory (keeps original name)
copy report.docx C:\Backups\
Output:
1 file(s) copied.
rem Copy to a different drive and folder
copy notes.txt D:\Archive\notes.txt
Output:
1 file(s) copied.
Wildcard copies
Wildcards * and ? let you copy groups of files matching a pattern. The destination must be a directory when copying multiple files; specifying a filename as destination with wildcards copies only the last matching file.
rem Copy all .txt files to an archive folder
copy *.txt C:\Archive\
Output:
4 file(s) copied.
rem Copy files matching a partial name
copy report*.docx D:\Reports\
Output:
3 file(s) copied.
Overwrite behaviour
By default in an interactive session copy prompts before overwriting an existing file. Use /Y to suppress the prompt in scripts, or /-Y to force a prompt even when the COPYCMD environment variable would suppress it.
rem Silent overwrite — essential in batch scripts
copy /Y source.txt destination.txt
Output:
1 file(s) copied.
rem Force prompt even if COPYCMD=/Y is set
copy /-Y source.txt destination.txt
Output:
Overwrite destination.txt? (Yes/No/All):
File concatenation
Placing + between multiple sources merges them into a single destination file. This is copy's unique feature absent in newer tools. ASCII mode (/A) stops reading each source at the first Ctrl+Z (ASCII 26) character; binary mode (/B) copies all bytes verbatim.
rem Concatenate three text files into one
copy header.txt + body.txt + footer.txt combined.txt
Output:
header.txt
body.txt
footer.txt
1 file(s) copied.
rem Append one file to another (source and destination may overlap)
copy existing.txt + new_section.txt existing.txt
Output:
1 file(s) copied.
rem Binary concatenation (no Ctrl+Z stripping)
copy /B part1.bin + part2.bin combined.bin
Output:
1 file(s) copied.
Copying to CON, NUL, and other devices
cmd.exe devices (CON, NUL, PRN, COM1, …) can be used as source or destination. CON is the console; copying to NUL discards output (useful for silencing command output).
rem Write console input to a file (Ctrl+Z then Enter to stop)
copy CON myfile.txt
Output:
(cursor waits for input; type lines, then Ctrl+Z + Enter)
1 file(s) copied.
rem Send a file to the default printer
copy report.txt PRN
Output:
1 file(s) copied.
Restartable mode for network copies
/Z enables restartable mode: if the connection drops mid-copy the transfer can be resumed rather than starting over. This only applies when copying to or from network paths.
copy /Z \\myhost\share\bigfile.iso C:\Downloads\bigfile.iso
Output:
1 file(s) copied.
Common pitfalls
- Wildcards with a filename destination copy only the last match — if the destination is
output.txtand source is*.txt, only the final matching file ends up there. Use a directory destination for multi-file copies. copyis not recursive — it will not descend into subdirectories; usexcopy /Sorrobocopyfor trees.- Concatenation in ASCII mode may truncate binary data —
/Ais the default when combining files; always specify/Bfor binary concatenation. - Overwrite prompt suppressed by
COPYCMD=/Y— if this environment variable is set,copynever prompts. Specify/-Yexplicitly to get the prompt back. - Long paths (>260 chars) may fail —
copydoes not support extended-length paths (\\?\). Userobocopyfor paths over the 260-character limit.
Real-world recipes
Back up a config file before editing
copy /Y app.config app.config.bak
notepad app.config
Output:
1 file(s) copied.
Merge CSV exports into one file
copy /B jan.csv + feb.csv + mar.csv Q1.csv
Output:
jan.csv
feb.csv
mar.csv
1 file(s) copied.
Copy all files of a type to a backup folder silently in a scheduled task
copy /Y C:\Logs\*.log C:\Backups\Logs\
Output:
7 file(s) copied.
Create an empty file
copy NUL placeholder.txt
Output:
1 file(s) copied.
ASCII vs binary modes in depth
/A and /B switches sit before or after a source/destination filename and apply to the closest preceding argument. The defaults are subtle: a single-file copy defaults to binary; a concatenation (+) defaults to ASCII for sources and binary for destination. ASCII mode stops reading at the first Ctrl+Z (0x1A) byte and appends a Ctrl+Z marker on write — which silently corrupts binary files. Always specify /B explicitly when copying anything that is not plain text.
rem Binary copy (default for single file)
copy /B installer.exe C:\Tools\installer.exe
Output:
1 file(s) copied.
rem Demonstrating ASCII truncation — DON'T do this with binaries
copy /A binary.dat copy.dat
fc /B binary.dat copy.dat
Output:
1 file(s) copied.
Comparing files binary.dat and COPY.DAT
00001A05: 1A FF
rem Per-argument switches: source binary, destination ASCII (rare)
copy source.txt /B + extra.txt destination.txt /A
Output:
source.txt
extra.txt
1 file(s) copied.
Verification with /V
/V reads back every copied file from the destination and compares it byte-for-byte to the source. It catches transient write errors that the filesystem accepts but later fails to read correctly. The cost is roughly 2x the copy time. Modern SSDs and NTFS journaling make /V largely redundant on local drives, but it remains useful for flaky USB sticks, optical media, and unreliable network shares.
copy /V important.docx D:\Backup\important.docx
Output:
1 file(s) copied.
rem Verification failure surfaces as an error
copy /V flaky_file.bin E:\backup\flaky_file.bin
Output:
The data being verified does not match the file that was copied.
Restartable mode with /Z
/Z enables a restartable transfer protocol designed for unreliable network links. If a copy is interrupted (network drop, transient share unavailability) /Z resumes from the last successful byte rather than starting over. The trade-off is significant per-block overhead, so reserve /Z for slow or unstable links — on a fast LAN or local copy it is pure overhead.
copy /Z \\myhost\share\bigfile.iso C:\Downloads\bigfile.iso
Output:
1 file(s) copied.
Long path support and /B for size accuracy
copy historically caps at MAX_PATH (260 characters) and does not honour LongPathsEnabled reliably. For multi-gigabyte files crossing the size cap on cmd.exe, switch to xcopy, robocopy, or PowerShell's Copy-Item. The size in the 1 file(s) copied summary is accurate only when /B is used; ASCII mode reports the apparent text size.
File concatenation in depth
The + operator is copy's most distinctive feature. Multiple sources combine in order into a single destination. Sources may include wildcards: copy *.csv all.csv concatenates every matching file in the working directory (in directory-enumeration order, which is typically alphabetical on NTFS). Be aware: if the destination filename matches the wildcard, copy skips it to avoid infinite recursion — but only when it can detect the collision, which is not always.
rem Concatenate all CSVs in the folder
copy *.csv combined.csv
Output:
january.csv
february.csv
march.csv
1 file(s) copied.
rem Concatenate with explicit binary mode for safety
copy /B part1.zip + part2.zip combined.zip
Output:
part1.zip
part2.zip
1 file(s) copied.
rem Append in place — source and destination overlap
copy /B existing.log + new_entries.log existing.log
Output:
1 file(s) copied.
COPYCMD environment variable
The COPYCMD environment variable provides a default flag for copy, move, and xcopy. Setting COPYCMD=/Y suppresses overwrite prompts globally — which is convenient for scripts but dangerous in interactive sessions where you may want the safety prompt. Use /-Y on any individual command to force the prompt back on.
set COPYCMD=/Y
copy source.txt destination.txt
rem No prompt even if destination exists
Output:
1 file(s) copied.
rem Force prompt despite COPYCMD=/Y
copy /-Y source.txt destination.txt
Output:
Overwrite destination.txt? (Yes/No/All):
PowerShell equivalents
Copy-Item (aliases cp, copy, cpi) is the PowerShell native. It handles single files, wildcards, recursive trees (-Recurse), filters (-Filter, -Include, -Exclude), and produces typed objects. Unlike copy, it always preserves binary content correctly and natively supports long paths. PowerShell's copy is not the cmd builtin — it is Copy-Item with PowerShell semantics, so /Y and /Z have no meaning there.
# Single file
Copy-Item report.docx report_backup.docx
Output: (none — silent success)
# Wildcard
Copy-Item *.txt C:\Archive\
Output: (none — silent success)
# Recursive directory copy with overwrite
Copy-Item C:\Projects\myapp C:\Backup\myapp -Recurse -Force
Output: (none — silent success)
# Copy with include/exclude filters
Copy-Item C:\Source\* C:\Dest\ -Recurse -Include '*.py','*.md' -Exclude '*.pyc'
Output: (none — silent success)
# Concatenate files (no native + operator; use Get-Content / Set-Content)
Get-Content header.txt, body.txt, footer.txt | Set-Content combined.txt
Output: (none — silent success)
# Binary concatenation
$bytes = foreach ($f in 'part1.bin','part2.bin') { [IO.File]::ReadAllBytes($f) }
[IO.File]::WriteAllBytes('combined.bin', ($bytes | ForEach-Object { $_ }))
Output: (none — silent success)
# Dry run with -WhatIf
Copy-Item C:\Source\* C:\Dest\ -Recurse -WhatIf
Output:
What if: Performing the operation "Copy File" on target "Item: C:\Source\a.txt Destination: C:\Dest\a.txt".
# Long path with literal path
Copy-Item -LiteralPath '\\?\C:\very\long\source\file.dat' -Destination 'D:\Backup\'
Output: (none — silent success)
CMD vs PowerShell vs bash comparison
| Goal | CMD | PowerShell | bash (Linux/macOS) |
|---|---|---|---|
| Single file copy | copy a b | Copy-Item a b | cp a b |
| Silent overwrite | copy /Y a b | Copy-Item a b -Force | cp -f a b |
| Recursive directory | (use xcopy/robocopy) | Copy-Item a b -Recurse | cp -r a b |
| Preserve timestamps | (not built in; robocopy /DCOPY:T /COPY:DAT) | Copy-Item -Recurse (preserves) | cp -p a b |
| Concatenate files | copy a + b + c out | Get-Content a,b,c | Set-Content out | cat a b c > out |
| Verify after copy | copy /V a b | (manual: compare hashes) | cp -p a b; cmp a b |
| Restartable network | copy /Z a b | (use Start-BitsTransfer) | (use rsync --partial) |
| Dry run / preview | (none built in) | Copy-Item -WhatIf | cp -v or rsync --dry-run |
| Pattern with exclude | (not built in; use xcopy /EXCLUDE) | Copy-Item -Exclude | rsync --exclude |
| Create empty file | copy NUL foo.txt | New-Item foo.txt or Set-Content foo.txt '' | touch foo.txt |
| Pipe to printer | copy a PRN | (use Out-Printer) | lpr a |
Common pitfalls (continued)
- ASCII mode truncates at Ctrl+Z —
copy file.exe other.exedefaults to binary for single-file copies, butcopy a + b outdefaults to ASCII for sources. Always pass/Bfor non-text. copydoes not preserve timestamps when concatenating — the destination gets the current time. Userobocopy /COPY:DAT /DCOPY:Tif timestamps matter.- No recursive flag —
copycannot descend into subdirectories. Reach forxcopy /S /Eorrobocopy /Efor trees. - Wildcards with filename destination —
copy *.txt out.txtwrites only the last matched file toout.txtif the destination is treated as a file. Always end the destination with\to force directory interpretation. - Sparse files and alternate data streams are not preserved —
copycollapses NTFS sparse files into their physical representation and drops ADS. Userobocopy /COPY:DAT(or/Bfor backup mode) for those. - Junctions and symlinks —
copyfollows them and copies the target file or directory. Usexcopy /Borrobocopy /SLto copy the link itself. - COPYCMD=/Y in user environment — a global
COPYCMD=/Yremoves the safety prompt everywhere. Auditsetoutput if interactive copies silently overwrite.
Real-world recipes (continued)
Combine log shards in chronological order
rem Each shard sorts alphabetically by ISO date prefix
copy /B shard_2026*.log combined.log
Output:
shard_20260101.log
shard_20260102.log
...
1 file(s) copied.
Daily snapshot of a config file with date stamp
set STAMP=%DATE:~-4,4%%DATE:~-10,2%%DATE:~-7,2%
copy /Y app.config "C:\Backup\app.config.%STAMP%"
Output:
1 file(s) copied.
Verify copy by hash comparison
copy /Y bigfile.iso D:\bigfile.iso
for /F %a in ('certutil -hashfile bigfile.iso SHA256 ^| findstr /V ":"') do set H1=%a
for /F %a in ('certutil -hashfile D:\bigfile.iso SHA256 ^| findstr /V ":"') do set H2=%a
if "%H1%"=="%H2%" (echo Match) else (echo MISMATCH)
Output:
1 file(s) copied.
Match
Bundle CSV exports into a single archive
copy /B exports\*.csv consolidated.csv
echo Combined %ERRORLEVEL%
Output:
7 file(s) copied.
Combined 0
PowerShell: copy preserving timestamps and ACLs
Copy-Item C:\Source\* C:\Dest\ -Recurse -Force
Get-ChildItem C:\Source -Recurse | ForEach-Object {
$dest = $_.FullName -replace [regex]::Escape('C:\Source'), 'C:\Dest'
if (Test-Path $dest) {
Get-Acl $_.FullName | Set-Acl -Path $dest
(Get-Item $dest).LastWriteTime = $_.LastWriteTime
}
}
Output: (none — silent success)
Create a sparse placeholder of an expected size
copy NUL placeholder.dat
fsutil file setvaliddata placeholder.dat 1048576
Output:
1 file(s) copied.
Valid data length is changed
Touch — bump a file's modified time without changing content
copy documents an idiomatic "touch" trick: concatenate the file with nothing (two trailing commas indicate the destination is intentionally omitted), and the file is rewritten with the current timestamp.
rem Update mtime on existing.log to now, leave content untouched
copy /B existing.log +,,
Output:
existing.log
1 file(s) copied.
This is the cmd equivalent of touch -m existing.log on Linux. PowerShell's version is (Get-Item existing.log).LastWriteTime = Get-Date.
Sources
Microsoft Learn — copy command — Authoritative reference for /d /v /n /y /-y /z /a /b, the + concatenation operator and ASCII/binary mode interplay, the COPYCMD environment variable, device-name destinations (PRN, COMn), and the +,, timestamp-touch idiom.
See also
xcopy— recursive directory copy with attribute filtering; the historical step up fromcopy.robocopy— production-grade copy and sync with retry, mirror, and logging.move— copy-then-delete in one step.del— remove files; inverse of copy.fc— verify two files differ byte-by-byte; useful after copy.certutil -hashfile— verify content integrity post-copy.- PowerShell
Copy-Item,Start-BitsTransfer— modern alternatives with progress and resume. - Linux
cp,rsync— Unix equivalents.