| |

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

OptionEffect
cp src destCopy file
cp src1 src2 dirCopy multiple to directory
cp -r src destRecursive
cp -R src destRecursive
cp -a src destArchive (recursive + preserve all)
cp -p src destPreserve attributes
cp -i src destPrompt before overwrite
cp -n src destDo not overwrite
cp -u src destCopy only if newer
cp -v src destVerbose
cp -t dir srcDestination first
cp -d src destPreserve symlinks
cp -l src destHard link
cp -s src destSymbolic link

Preserve Options

OptionPreserves
-pMode, ownership, timestamps
--preserve=modePermissions only
--preserve=timestampsTimestamps only
--preserve=ownershipOwner and group
--preserve=allEverything
-a-r -p -d --preserve=all

Common Patterns

PatternPurpose
cp file.txt file.txt.bakBackup 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

ErrorCause
omitting directoryMissing -r
cannot statSource does not exist
Permission deniedNo write access
cannot overwriteDestination is a directory
same fileSource 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

PitfallWhy It HappensFix
omitting directoryMissing -rUse -r or -a
Permissions not preservedMissing -pUse -p or -a
Destination created as subdirectoryNo trailing /.Use /.
Overwrite without warningDefault behaviorUse -i or -n
Hidden files not copiedGlob does not matchUse .* or find
Symbolic link followedMissing -dUse -d or -a
Owner changedNo -pUse -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

ItemValue
Basic copycp source dest
Directory copycp -r source dest
Archive copycp -a source dest
Preserve attributescp -p source dest
Prompt on overwritecp -i source dest
Do not overwritecp -n source dest
Copy if newercp -u source dest
Verbosecp -v source dest
Copy contentscp -a source/. dest/
Multiple sourcescp a b c dest/

Key takeaways:

  • cp copies files; -r or -a copies directories. Without -r, a directory copy fails with “omitting directory.” The -a option 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 -p or -a to preserve them.
  • cp overwrites by default. Use -i to prompt, -n to skip existing files, and -u to copy only when the source is newer. The alias cp='cp -i' is common on interactive systems.
  • A trailing /. on the source copies the contents. cp -a /src/. /dest/ copies the files inside /src into /dest, rather than creating /dest/src. This is the standard idiom for merging a source tree into an existing destination.
  • The -d option preserves symbolic links. Without it, the link is followed and the target is copied. The -a option implies -d.
  • cp -l creates hard links and cp -s creates symbolic links. These are not copies; they create links to the source. The source must remain in place.
  • cp does not preserve hard links unless -a is 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!