LFCS 10 ๐ง Copying Files and Directories โ cp
The cp command copies files and directories. It creates a new file or directory at the destination with the same contents as the source, leaving the source unchanged. It is one of the most-used commands in the shell, and the LFCS exam includes tasks that require copying files to a specific location, preserving permissions, and copying directory trees recursively. The command is simple, but the options change its behavior significantly.
Key point: cp source dest copies a file. cp -r source dest copies a directory and its contents. By default, cp overwrites the destination if it exists, does not preserve permissions or timestamps, and does not copy hidden files when a glob is used. The -i option prompts before overwriting, the -p option preserves attributes, and the -a option is equivalent to -r -p plus preserving links and other attributes.
Why cp matters
The backup problem. Before modifying a configuration file, the standard practice is to copy it to a backup. cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak creates the backup. The -p option preserves the permissions and timestamps so the backup matches the original.
The deployment problem. Copying a built application or a configuration file to a target directory is a routine deployment step. The cp command is the mechanism. The destination directory must exist, and the permissions must allow the copy.
The recursive problem. Copying a directory requires -r or -a. Without it, cp fails with “omitting directory.” The -a option is the one that preserves the attributes and is the standard for recursive copies.
The overwrite problem. cp overwrites the destination without prompting by default. On a production system, this can destroy data. The -i option prompts before overwriting, and the -n option skips existing files. The alias cp='cp -i' is common on interactive systems to make the prompt the default.
The permission problem. A copied file gets the permissions of the creating process, not the source file, unless -p is used. A copied file is owned by the user who ran the command, not the source owner, unless -p is used. The -p option preserves the mode, ownership, and timestamps.
The exam problem. The LFCS tasks often require copying a file to a location with specific permissions, or copying a directory tree. The options matter. A copy without -p may not have the permissions the task requires. A copy without -r fails on a directory.
a. Copying files
The cp command takes a source and a destination.
$ cp source.txt dest.txt
$ ls
dest.txt source.txt
The dest.txt is created with the contents of source.txt. The source.txt is unchanged. The destination is created with the permissions of the creating processโthe umask appliesโand the owner is the user who ran the command.
If the destination is a directory, the file is copied into it with the same name.
$ cp source.txt /tmp/
$ ls /tmp/source.txt
/tmp/source.txt
If the destination is a directory and the file already exists there, it is overwritten.
$ cp source.txt /tmp/
$ cp source.txt /tmp/
# no error, the file is overwritten
The -i option prompts before overwriting.
$ cp -i source.txt /tmp/
cp: overwrite '/tmp/source.txt'? y
The -n option skips existing files without prompting.
$ cp -n source.txt /tmp/
# no prompt, no overwrite
The -v option prints each file as it is copied.
$ cp -v source.txt dest.txt
'source.txt' -> 'dest.txt'
Multiple sources can be copied to a directory.
$ cp file1.txt file2.txt file3.txt /tmp/
The last argument is the destination directory. The preceding arguments are the sources. If the destination is not a directory, the command fails.
The -t option specifies the destination first.
$ cp -t /tmp/ file1.txt file2.txt file3.txt
The -t form is useful when the source list is generated by a glob or a command.
A glob can be used as the source.
$ cp *.txt /tmp/
The shell expands the glob to the matching files, and cp copies each. Hidden files are not matched by *. Use .* or find for hidden files.
$ cp .*.txt /tmp/ # hidden .txt files
The -u option copies only if the source is newer than the destination.
$ cp -u source.txt /tmp/
The -u option is useful for incremental copies where only changed files should be updated.
b. Copying directories
The cp command does not copy directories by default.
$ cp projects /tmp/
cp: omitting directory 'projects'
The -r option copies a directory and its contents recursively.
$ cp -r projects /tmp/
$ ls /tmp/projects
The -R option is equivalent to -r.
$ cp -R projects /tmp/
The -a option is the archive option. It is equivalent to -r -p -d --preserve=all. It preserves the permissions, ownership, timestamps, symbolic links, and other attributes.
$ cp -a projects /tmp/
The -a option is the one to use for backups and for copies where the attributes matter.
The -p option preserves the mode, ownership, and timestamps. It does not imply -r.
$ cp -rp projects /tmp/
The combination -rp is equivalent to -a for most purposes, except that -a also preserves links.
The -d option preserves symbolic links rather than following them.
$ cp -d link.txt /tmp/
Without -d, the symbolic link is followed and the target file is copied. With -d, the link itself is copied.
$ ln -s /etc/hosts link.txt
$ cp link.txt /tmp/link_copy.txt
$ ls -l /tmp/link_copy.txt
-rw-r--r-- 1 alice alice 221 /tmp/link_copy.txt # regular file
$ cp -d link.txt /tmp/link_copy.txt
$ ls -l /tmp/link_copy.txt
lrwxrwxrwx 1 alice alice 10 /tmp/link_copy.txt -> /etc/hosts
The -L option follows symbolic links. The -P option does not follow them. The default is -P for cp -a and -L for cp -r in some implementations.
The -l option creates hard links instead of copying the files.
$ cp -l source.txt /tmp/source.txt
$ ls -li source.txt /tmp/source.txt
# same inode
The -s option creates symbolic links instead of copying.
$ cp -s source.txt /tmp/source.txt
$ ls -l /tmp/source.txt
lrwxrwxrwx 1 alice alice 10 /tmp/source.txt -> source.txt
The -l and -s options are not true copies. They create links to the source. The source must remain in place for the link to work.
c. Preserving attributes and the recursive pattern
The -p option preserves the attributes of the source.
$ cp -p source.txt dest.txt
$ ls -l source.txt dest.txt
-rw-r--r-- 1 alice alice 100 Oct 8 09:00 source.txt
-rw-r--r-- 1 alice alice 100 Oct 8 09:00 dest.txt
The permissions, ownership, and timestamps of source.txt are applied to dest.txt. Without -p, dest.txt would have the current time and the creating user’s ownership.
The --preserve option specifies which attributes to preserve.
$ cp --preserve=mode source.txt dest.txt
$ cp --preserve=timestamps source.txt dest.txt
$ cp --preserve=ownership source.txt dest.txt
$ cp --preserve=all source.txt dest.txt
The mode, ownership, and timestamps are the common attributes. The all option preserves everything, including links, extended attributes, and ACLs.
The -a option is the combination that preserves everything.
$ cp -a /etc/nginx /srv/backup/nginx
The entire directory tree is copied with all attributes. The -a option is the standard for backups and for copying system directories.
A trailing slash on the source directory affects the copy.
$ cp -a /etc/nginx /srv/backup/nginx
The destination /srv/backup/nginx is created as a copy of /etc/nginx. If the destination already exists as a directory, the source is copied into it.
$ mkdir -p /srv/backup/nginx
$ cp -a /etc/nginx/. /srv/backup/nginx/
The /. on the source copies the contents of /etc/nginx into /srv/backup/nginx, rather than creating /srv/backup/nginx/nginx. The trailing /. is a common idiom for copying contents.
The rsync command is often preferred over cp -a for large trees because it is faster and can resume. But rsync is not installed by default on all systems, and the LFCS exam may not have it. The cp -a command is always available.
The cp command does not preserve hard links by default. The -a option preserves them.
$ cp -a /srv/data /srv/backup/data
The hard links in the source tree are preserved in the copy. Without -a, each hard link becomes a separate file.
The cp command does not have a built-in progress indicator. For large copies, pv or rsync --progress is used. Neither is installed by default.
The cp command fails if the destination is not writable, if the source does not exist, or if the source is a directory without -r.
$ cp nonexistent.txt /tmp/
cp: cannot stat 'nonexistent.txt': No such file or directory
$ cp projects /tmp/
cp: omitting directory 'projects'
$ cp /etc/hosts /root/
cp: cannot create regular file '/root/hosts': Permission denied
Complete Example Session
# ============================================
# PART 1: BASIC COPY
# ============================================
$ cp source.txt dest.txt
$ ls
dest.txt source.txt
# ============================================
# PART 2: COPY TO DIRECTORY
# ============================================
$ cp source.txt /tmp/
$ ls /tmp/source.txt
/tmp/source.txt
# ============================================
# PART 3: OVERWRITE PROMPT
# ============================================
$ cp -i source.txt /tmp/
cp: overwrite '/tmp/source.txt'? y
# ============================================
# PART 4: NO OVERWRITE
# ============================================
$ cp -n source.txt /tmp/
# no prompt, no overwrite
# ============================================
# PART 5: MULTIPLE SOURCES
# ============================================
$ cp file1.txt file2.txt file3.txt /tmp/
$ ls /tmp/file*.txt
/tmp/file1.txt /tmp/file2.txt /tmp/file3.txt
# ============================================
# PART 6: RECURSIVE COPY
# ============================================
$ cp -r projects /tmp/
$ ls /tmp/projects
# ============================================
# PART 7: ARCHIVE COPY
# ============================================
$ cp -a projects /tmp/backup
$ ls -ld /tmp/backup
drwxr-xr-x 2 alice alice 4096 Oct 8 10:00 /tmp/backup
# ============================================
# PART 8: PRESERVE ATTRIBUTES
# ============================================
$ cp -p source.txt dest.txt
$ ls -l source.txt dest.txt
-rw-r--r-- 1 alice alice 100 Oct 8 09:00 source.txt
-rw-r--r-- 1 alice alice 100 Oct 8 09:00 dest.txt
# ============================================
# PART 9: COPY CONTENTS WITH /.
# ============================================
$ mkdir -p /srv/backup/nginx
$ cp -a /etc/nginx/. /srv/backup/nginx/
$ ls /srv/backup/nginx/
# ============================================
# PART 10: VERBOSE COPY
# ============================================
$ cp -v source.txt dest.txt
'source.txt' -> 'dest.txt'
The ten parts covered a basic copy, copying to a directory, the overwrite prompt, -n, multiple sources, recursive copy, archive copy, preserving attributes, copying contents with /., and verbose copy.
Quick Reference
cp Options
| Option | Effect |
|---|---|
cp src dest | Copy file |
cp src1 src2 dir | Copy multiple to directory |
cp -r src dest | Recursive |
cp -R src dest | Recursive |
cp -a src dest | Archive (recursive + preserve all) |
cp -p src dest | Preserve attributes |
cp -i src dest | Prompt before overwrite |
cp -n src dest | Do not overwrite |
cp -u src dest | Copy only if newer |
cp -v src dest | Verbose |
cp -t dir src | Destination first |
cp -d src dest | Preserve symlinks |
cp -l src dest | Hard link |
cp -s src dest | Symbolic link |
Preserve Options
| Option | Preserves |
|---|---|
-p | Mode, ownership, timestamps |
--preserve=mode | Permissions only |
--preserve=timestamps | Timestamps only |
--preserve=ownership | Owner and group |
--preserve=all | Everything |
-a | -r -p -d --preserve=all |
Common Patterns
| Pattern | Purpose |
|---|---|
cp file.txt file.txt.bak | Backup a file |
cp -a /etc/nginx /srv/backup/ | Backup a directory |
cp -a /etc/nginx/. /srv/backup/ | Copy contents |
cp -i *.txt /tmp/ | Prompt on overwrite |
cp -u *.txt /tmp/ | Incremental copy |
cp -v file.txt /tmp/ | Show progress |
Error Cases
| Error | Cause |
|---|---|
omitting directory | Missing -r |
cannot stat | Source does not exist |
Permission denied | No write access |
cannot overwrite | Destination is a directory |
same file | Source and destination are the same |
Best Practices
โ Do This:
# Use -a for directory copies
cp -a /etc/nginx /srv/backup/nginx # โ
# Use -p to preserve attributes
cp -p nginx.conf nginx.conf.bak # โ
# Use -i in interactive shells
alias cp='cp -i' # โ
# Use /./ to copy contents
cp -a /etc/nginx/. /srv/backup/nginx/ # โ
# Use -v to see progress
cp -v file.txt /tmp/ # โ
# Use -n to avoid overwriting in scripts
cp -n file.txt /tmp/ # โ
# Check the destination after the copy
ls -l /tmp/file.txt # โ
โ Don’t Do This:
# Don't copy a directory without -r
cp projects /tmp/ # fails # โ
# Don't assume the permissions are preserved
cp file.txt /tmp/ # new permissions from umask # โ ๏ธ
# Don't overwrite without checking
cp file.txt /etc/ # may overwrite # โ ๏ธ
# Don't use -l or -s and expect a copy
cp -l file.txt /tmp/ # hard link, not a copy # โ ๏ธ
# Don't forget the trailing /.
cp -a /etc/nginx /srv/backup/nginx # creates nginx/nginx # โ ๏ธ
# Don't copy to a file when the destination is a directory
cp file.txt /tmp/dir # error if dir exists # โ ๏ธ
# Don't use cp for large trees without considering rsync
cp -a /var /backup/ # slow, no resume # โ ๏ธ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
omitting directory | Missing -r | Use -r or -a |
| Permissions not preserved | Missing -p | Use -p or -a |
| Destination created as subdirectory | No trailing /. | Use /. |
| Overwrite without warning | Default behavior | Use -i or -n |
| Hidden files not copied | Glob does not match | Use .* or find |
| Symbolic link followed | Missing -d | Use -d or -a |
| Owner changed | No -p | Use -p or -a |
Real-World Examples
1. Backup a File
cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
2. Copy to a Directory
cp file.txt /tmp/
3. Recursive Copy
cp -r projects /tmp/
4. Archive Copy
cp -a /etc/nginx /srv/backup/nginx
5. Copy Contents
cp -a /etc/nginx/. /srv/backup/nginx/
6. Prompt on Overwrite
cp -i file.txt /tmp/
7. Do Not Overwrite
cp -n file.txt /tmp/
8. Incremental Copy
cp -u *.txt /tmp/
9. Verbose Copy
cp -v file.txt /tmp/
10. Multiple Sources
cp file1.txt file2.txt file3.txt /tmp/
Visual
cp File vs Directory
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ cp file.txt dest.txt โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ โ file.txtโ โโโโโโโถ โ dest.txtโ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ Both exist. Same contents. โ
โ โ
โ cp -r dir /tmp/ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ โ dir/ โ โโโโโโโถ โ /tmp/dir/โ โ
โ โ โโโ a โ โ โโโ a โ โ
โ โ โโโ b โ โ โโโ b โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ Directory and contents copied. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Without vs With -p
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ WITHOUT -p โ
โ โ
โ source.txt: -rw-r--r-- alice alice Oct 8 09:00 โ
โ dest.txt: -rw-r--r-- alice alice Oct 8 10:05 โ
โ โ
โ Timestamps are the current time. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ WITH -p โ
โ โ
โ source.txt: -rw-r--r-- alice alice Oct 8 09:00 โ
โ dest.txt: -rw-r--r-- alice alice Oct 8 09:00 โ
โ โ
โ Permissions, ownership, and timestamps preserved. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Copy Contents vs Copy Directory
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ cp -a /etc/nginx /srv/backup/nginx โ
โ โ
โ /srv/backup/nginx/ โ
โ โโโ nginx/ โ
โ โโโ nginx.conf โ
โ โโโ sites-enabled/ โ
โ โ
โ Creates nginx inside nginx. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ cp -a /etc/nginx/. /srv/backup/nginx/ โ
โ โ
โ /srv/backup/nginx/ โ
โ โโโ nginx.conf โ
โ โโโ sites-enabled/ โ
โ โ
โ Copies the contents. No nested nginx. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Overwrite Behavior
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ cp file.txt /tmp/ โ
โ โ Overwrites without prompt โ
โ โ
โ cp -i file.txt /tmp/ โ
โ โ Prompts before overwriting โ
โ โ
โ cp -n file.txt /tmp/ โ
โ โ Skips if the destination exists โ
โ โ
โ cp -u file.txt /tmp/ โ
โ โ Copies only if the source is newer โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Basic copy | cp source dest |
| Directory copy | cp -r source dest |
| Archive copy | cp -a source dest |
| Preserve attributes | cp -p source dest |
| Prompt on overwrite | cp -i source dest |
| Do not overwrite | cp -n source dest |
| Copy if newer | cp -u source dest |
| Verbose | cp -v source dest |
| Copy contents | cp -a source/. dest/ |
| Multiple sources | cp a b c dest/ |
Key takeaways:
cpcopies files;-ror-acopies directories. Without-r, a directory copy fails with “omitting directory.” The-aoption is the archive form and preserves everything.- The default copy does not preserve attributes. The permissions come from the umask, the owner is the creating user, and the timestamps are the current time. Use
-por-ato preserve them. cpoverwrites by default. Use-ito prompt,-nto skip existing files, and-uto copy only when the source is newer. Thealias cp='cp -i'is common on interactive systems.- A trailing
/.on the source copies the contents.cp -a /src/. /dest/copies the files inside/srcinto/dest, rather than creating/dest/src. This is the standard idiom for merging a source tree into an existing destination. - The
-doption preserves symbolic links. Without it, the link is followed and the target is copied. The-aoption implies-d. cp -lcreates hard links andcp -screates symbolic links. These are not copies; they create links to the source. The source must remain in place.cpdoes not preserve hard links unless-ais used. With-a, the hard links in the source tree are preserved in the copy.
Remember: cp is the command for copying files and directories. The options determine what is copied and how. Use -r or -a for directories. Use -p or -a to preserve attributes. Use -i or -n to control overwriting. Use /. to copy contents. And remember that the destination directory must exist, the permissions must allow the copy, and a copy without -p has the attributes of the creating process, not the source. The command is simple, but the options are the difference between a copy that works and a copy that breaks the task.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!