1. Home
  2. Directory Operations
  3. cp Command Examples and Usages
  1. Home
  2. File Operations
  3. cp Command Examples and Usages

cp Command Examples and Usages

The copy or cp is a command in various Unix and Unix-like operating systems for copying files and directories. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory. You find some useful cp command examples and usages.

NAME:

cp – copy files and directories

cp Command Examples - Enlinux
cp Command Examples – Enlinux

SYNOPSIS

cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…

Description

The cp command is a Linux shell command to copy files and directories. Mandatory arguments to long options are mandatory for short options too.

-a, --archive

same as -dR --preserve=all

--attributes-only

Don’t copy the file data, just the attributes.

--backup[=CONTROL]

Make a backup of each existing destination file. -b like --backup but does not accept an argument.

--copy-contents

Copy contents of special files when recursive.

-d same as –-no-dereference --preserve=links

-f, --force

if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used).

-i, --interactive

prompt before overwrite (overrides a previous -n option).

-H follow command-line symbolic links in SOURCE.

-l, --link

Hard link files instead of copying.

-L, --dereference

Always follow symbolic links in SOURCE.

-n, --no-clobber

Do not overwrite an existing file (overrides a previous -i option)

-P, --no-dereference

Never follow symbolic links in SOURCE.

-p same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST]

preserve the specified attributes (default: mode,ownership,time‐stamps), if possible additional attributes: context, links, xattr, all.

--no-preserve=ATTR_LIST
--no-preserve=ATTR_LIST

Don’t preserve the specified attributes.

--parents

Use full source file name under DIRECTORY.

-R, -r, --recursive

Copy directories recursively.

--reflink[=WHEN]

Control clone/CoW copies. See below.

--remove-destination

Remove each existing destination file before attempting to open it (contrast with --force)

--sparse=WHEN

Control creation of sparse files. See below.

--strip-trailing-slashes

Remove any trailing slashes from each SOURCE argument.

-s, --symbolic-link

Make symbolic links instead of copying.

-S, --suffix=SUFFIX

Override the usual backup suffix.

-t, --target-directory=DIRECTORY

Copy all SOURCE arguments into DIRECTORY.

-T, --no-target-directory

Treat DEST as a normal file.

-u, --update

Copy only when the SOURCE file is newer than the destination file or when the destination file is missing.

-v, --verbose

Explain what is being done.

-x, --one-file-system

stay on this file system.

-Z set SELinux security context of destination file to default type.

--context[=CTX]

Like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX.

--help display this help and exi.
--version Output version information and exit.

By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well. That is the behavior selected by --sparse=auto. Specify --sparse=always to create a sparse DEST file whenever the SOURCE file contains a long enough sequence of zero bytes. Use --sparse=never to inhibit creation of sparse
files.

When --reflink[=always] is specified, perform a lightweight copy, where the data blocks are copied only when modified. If this is not possible the copy fails, or if --reflink=auto is specified, fall back to a standard copy. Use --reflink=never to ensure a standard copy is performed.

The backup suffix is ‘~’, unless set with --suffix or SIM‐PLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable.

Here are the values:

  • none, off: never make backups (even if –backup is given)
  • numbered, t: make numbered backups
  • existing, nil: numbered if numbered backups exist, simple otherwise
  • simple, never: always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same name for an existing, regular file.

Options

Short description for cp command main options:

optiondescription
cp -aarchive files
cp -fforce copy by removing the destination file if needed
cp -iinteractive – ask before overwrite
cp -llink files instead of copy
cp -Lfollow symbolic links
cp -nno file overwrite
cp -Rrecursive copy (including hidden files)
cp -uupdate – copy when source is newer than dest
cp -vverbose – print informative messages
cp Command Options

cp Command Examples

Copy single file project.c to destination directory backup:

$ cp project.c backup

Copy 2 files main.c and def.h to destination absolute path directory /home/usr/rapid/ :

$ cp main.c def.h /home/usr/rapid/

Copy all C files in current directory to subdirectory backup:

$ cp *.c backup

Copy directory src to absolute path directory /home/usr/rapid/ :

$ cp src /home/usr/rapid/

Copy all files and directories in dev recursively to subdirectory backup:

$ cp -R /dev backup

Force file copy:

$ cp -f test.c backup 

Interactive prompt before file overwrite:

$ cp -i test.c backup
cp: overwrite 'backup/test.c'? y

Update all files in current directory – copy only newer files to destination directory backup:

$ cp -u * backup

Read more about Linux command lines.

Updated on September 8, 2021

Was this article helpful?

Related Articles

Leave a Comment