Command Line · Lesson 10 of 19
cp (Copy)
0 / 5 checkpoints answered
The cp command copies files and directories while leaving the source in place. Its basic syntax is:
cp [OPTIONS] SOURCE DESTINATION
You can copy one file to another path, copy several files into a directory, or recursively copy a directory tree.
Copying One File
Place the source first and the destination second:
$ cp mycoolfile /home/pete/Documents/cooldocs
If /home/pete/Documents/cooldocs is an existing directory, the copy is created inside it as mycoolfile. You can instead provide a new destination filename:
$ cp mycoolfile /home/pete/Documents/mycoolfile_backup
In the second example, the copied data receives the name mycoolfile_backup.
CheckWhich command copies draft.txt to a file named final.txt while keeping draft.txt?
press 1/2/3 to answer · → next lesson
Copying Multiple Files into a Directory
List every source first and put the destination directory last:
$ cp report.txt notes.txt summary.txt /home/pete/Documents/
The final argument must be a directory when you provide more than one source.
CheckWhich command copies a.txt and b.txt into the existing archive/ directory?
press 1/2/3 to answer · → next lesson
Selecting Files with Wildcards
The shell can expand wildcard patterns into several source pathnames:
*: Matches any sequence of characters.?: Matches any single character.[]: Matches any one of the characters enclosed in the brackets.
For example, copy names ending in .jpg from the current directory to Pictures:
$ cp *.jpg /home/pete/Pictures
Preview the matches before a bulk copy, especially when the destination contains important data:
$ ls *.jpg
beach.jpg lunch.jpg profile.jpg
$ cp *.jpg /home/pete/Pictures
CheckBefore copying *.jpg, which command shows the non-hidden names that the pattern currently matches?
press 1/2/3 to answer · → next lesson
Copying Directory Trees
Copying a directory and everything below it requires recursive operation. Use -r or -R:
$ cp -r Pumpkin/ /home/pete/Documents
This copies the Pumpkin directory and its descendants into Documents.
Uppercase -R also requests recursive copying:
$ cp -R website /home/pete/backups/
Archive mode, -a, is useful for backup-style copies. It copies recursively while preserving links and many file attributes:
$ cp -a project/ project-backup/
CheckYou want a recursive backup-style copy of project/ that preserves links and many attributes. Which command fits that goal?
press 1/2/3 to answer · → next lesson
Controlling Overwrites
By default, cp can replace an existing destination file. Use -i to request confirmation before an overwrite:
$ cp -i mycoolfile /home/pete/Pictures
cp: overwrite '/home/pete/Pictures/mycoolfile'? n
Use -n when an existing destination should not be overwritten:
$ cp -n mycoolfile /home/pete/Pictures
The -f option tells GNU cp to try removing an existing destination if it cannot open that file for writing, then retry the copy. It is not a substitute for checking targets carefully. Shell aliases can also add options such as -i, so inspect an unexpected prompt rather than assuming a particular configuration.
CheckWhich command copies report.txt into backup/ but skips an existing destination of the same name?
press 1/2/3 to answer · → next lesson
Preserving or Refreshing Files
Use -p to preserve the source file's mode, ownership when permitted, and timestamps:
$ cp -p mycoolfile /home/pete/backups/
Use -u to copy a source only when the destination is missing or the source is newer:
$ cp -u *.txt /home/pete/Documents/
Other common options include:
-f: Force overwriting by removing the destination first if needed.-v: Show each file as it is copied.
To practice copying files and directory trees, try these hands-on labs:
- Linux cp Command: File Copying - Practice basic usage, advanced options like recursive copying, preserving attributes, and using wildcards to efficiently copy files and directories.
- Organizing Files and Directories - Practice essential Linux file management skills by using
cp,mv, andrmcommands to organize a project structure, move files, and clean up unnecessary directories.
Summary
You can now copy files and directory trees while controlling how destinations are handled.
- Place source operands before the destination.
- Preview wildcard matches before a bulk copy.
- Copy directory trees recursively or in archive mode.
- Confirm, skip, or deliberately replace existing destinations.
- Preserve attributes or copy only newer sources when needed.
Lesson complete
You finished cp (Copy). Nice work — keep the momentum going.