Search

Linux cp command howto

The Linux "cp" command is used to copy a file from one location to another location. The syntax of the command is as below.

cp [OPTION]… SOURCE TARGET

Create a copy of the file SOURCE at TARGET. TARGET may be a file or a directory. If TARGET is a directory, "cp" will create a copy of SOURCE in that directory with the same name as the source file. Note that this command is distinct from mv in that an entirely new file is created with a different inode number and a different location in the filesystem.

By default, the Linux "cp" doesn't copy directories. However, you can force it to do so by specifying the -r option.

Example:

[root@RHEL01 ~]# cp install.log /logs/install.log

The following table lists some important options of the Linux "cp" command.

 

Option

 

Description

-a, --archive

Preserve as many of the attributes of the original as possible.

-b, --backup

Make backups of the files that are about to be overwritten.

-d, --nodereference

If the file is a symbolic link, copy the link itself rather than the file to which it points.

-f, --force

Force the removal of target files.

-i, --interactive

In the case where TARGET is an existing file, prompt the user before removing it.

-l, --link

Create a hard link instead of copying the file

-p, --preserve

Preserve the owner, group, permissions, and timestamp of the original file.

-P, --parents

Retain the directory path

-r, -R, --recursive

Recursively copy any available subdirectories

--sparse

In the case where a file is sparse treat the sparse sequence as specified. ie., auto,  always or never

-s, --symbolic-link

Create symbolic links instead of actual copies.

-u, --update

In the case where the target file already exists and has a more recent update time than the source, do not make the copy.

-v, --verbose

Verbose output.

Related Tutorials