How do I copy a file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Copying a file means creating a second copy of the file at another path while keeping the original in place. The exact command depends on the environment, but the idea is always the same: specify the source and the destination.
For day-to-day work, you usually use a graphical file manager or a shell command. For automation, a language library such as Python shutil is often the better choice.
Copying a File from the Command Line
On Linux and macOS, the standard command is cp:
If the destination is a directory, cp keeps the original filename:
Useful options include:
- '
-ito prompt before overwrite' - '
-pto preserve timestamps and mode bits' - '
-vto print what was copied'
Example:
On Windows Command Prompt, the classic command is copy:
In PowerShell, use Copy-Item:
PowerShell is usually more flexible than copy, especially when you start working with filters, recursion, or scripting.
Copying a File in a GUI
If you are using a file manager instead of a shell:
- On Windows, select the file, press
Ctrl+C, then move to the destination and pressCtrl+V. - On macOS, use
Command+CandCommand+V, or holdOptionwhile dragging to force a copy. - On Linux desktop environments, the shortcuts are usually
Ctrl+CandCtrl+Vas well.
Graphical copying is simple, but the command line is easier to document, automate, and repeat accurately.
Copying Files in Code
If you need to copy files in a program, use a standard library instead of spawning shell commands. In Python, the shutil module is the usual answer:
shutil.copy2() copies the file content and also tries to preserve metadata such as modification time. If you only want the file contents, shutil.copy() is slightly simpler.
This approach is preferable in scripts because it avoids shell quoting issues and works consistently across platforms.
Copying to a Remote Machine
Sometimes "copy a file" really means "copy it to another server." On Unix-like systems, scp is a common choice:
That still follows the same source-and-destination model, but the destination includes a host and remote path.
For large or repeated transfers, rsync is often more efficient because it can skip unchanged files and show transfer progress.
Understanding Overwrite Behavior
The most important detail in file copying is what happens if the destination already exists.
- '
cpmay overwrite silently unless you use-i' - '
copyon Windows may ask for confirmation depending on context' - '
Copy-Itemcan overwrite, and flags such as-Forceaffect behavior' - '
shutil.copy2()overwrites the destination file if the path already exists'
If accidental overwrite would be a problem, add a check before copying. For example, in Python:
That is safer than assuming the environment will stop you automatically.
Common Pitfalls
One common mistake is forgetting to quote paths with spaces. Commands such as cp My File.txt backup/ are parsed as multiple arguments unless the path is quoted.
Another issue is confusing file copy with directory copy. cp file.txt dir/ copies a single file, but copying a directory usually requires cp -r or a directory-aware command such as Copy-Item -Recurse.
Permissions can also block the operation. If the source is unreadable or the destination directory is unwritable, the command fails even when the syntax is correct.
People also assume metadata is preserved automatically. Some commands preserve only contents by default, while timestamps, permissions, or extended attributes may need special flags.
Finally, be careful with relative paths. Running the same command from a different current directory can copy the wrong file or place the copy in an unexpected location.
Summary
- A file copy always needs a source path and a destination path.
- Use
cpon Linux or macOS,copyorCopy-Itemon Windows, andshutil.copy2()in Python scripts. - Quote paths that contain spaces.
- Check overwrite behavior before copying important files.
- Use directory-specific options such as
-ror-Recursewhen the source is a folder rather than a single file.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.