Linux CP Command

Introduction

Linux CP Command; it is a command in multiple Unix and Unix-like Operating systems for copying file and folders. The cp 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.

Description of Linux CP Command

In common form this command is cp source destination, let’s see the below example.

If the Linux cp command is successful, by default, no output is displayed – this is the behaviors of most of the Linux commands.

Linux CP Command

Syntax

cp [option]… [-T] source destination

cp [option]… sourcedirectory

cp [option]… -t directory source

cp –help

cp –version

Various list of options in Linux CP Command

Options Description
-a, –archive Same as -dR –preserve=ALL. When performing the copy, attempt to preserve as much of the original file structure, attributes, and associated metadata as possible. This metadata includes security context data if you are running SELinux.
–attributes-only Don’t copy the file data, only create a file with the same attributes. If the destination file already exists, don’t alter its contents. You can control exactly which attributes are copied with the –preserve option.
–backup[=control] Make a backup of each existing destination file that would otherwise be overwritten or removed. The control parameter specifies what version control method to use;   As a special case, cp –force –backup makes a backup of source when source and dest are the same, regular file
-b Like –backup, but does not accept a control argument; the default control method is always used.
–copy-contents When operating recursively, copy contents of special files, such as FIFOs and devices found in /dev. You usually don’t want to use this option, because it can have undesired results, such as hanging forever or filling up your entire disk. However, this option is available for special, expert use cases.
-d Copy symbolic links themselves, rather than the files they refer to, and preserve hard links between source files in the copies. Same as “–no-dereference –preserve=links“.
-f, –force If an existing destination file cannot be opened, remove it and try again. This option has no effect if the -n/–no-clobber option is used. However, it applies independently of -i/–interactive; neither option cancels the effect of the other.
-I,  –interactive Prompt before overwrite (overrides a previous -n option).
-H Follow symlinks specified on the command line, but preserve discovered links.   If one of the arguments on the command line is a symbolic link, copy the referenced file, not the link itself. However, if a symbolic link is discovered during recursive traversal, it’s copied as a symlink, not a regular file.
-l,  –link Create hard links to files instead of copying them.
-L, –dereference Always follow symbolic links in source; if source is a symlink, copy the file linked to rather than the symlink itself. When this option is specified, cp cannot create symlinks in the destination copies.
-n, –no-clobber Do not overwrite an existing file. If -i/–interactive was previously specified, this option overrides it. This option cannot be specified with -b/–backup, because backups are only created when a file would be overwritten.
-P, –no-dereference Never follow symbolic links in source; copy symlinks as symlinks. Existing symlinks encountered in the destination may still be followed, however.
-p Same as –preserve=mode,ownership,timestamps.
–preserve=[attr_list] Preserve the specified attributes, separated by a comma. Attributes are:     mode –   Preserve file mode bits (as set with chmod), and any ACLs.   ownership – Preserve owner and group (as set with chown). Ability to preserve these attributes is restricted in the same way as using chown.   timestamps – Preserve time of last file access and modification (atime and mtime, as set with touch). If possible   links – Preserve in the destination files any links between the source files. With -L or -H, this option can potentially copy symbolic links as hard links.   context – Preserve SELinux security context of source files, or fail with verbose diagnostics.
–no-preserve=attr_list Don’t preserve the specified attributes.
–parents Create missing parent directories in the destination, if necessary, when copying to target directory, according to the pathname specified in source.
-R, -r, –recursive Copy directories recursively.
-S, –suffix=suffix Override the usual backup suffix.
-t, –target-directory=directory Copy all source arguments into directory
-T, –no-target-directory Treat destination 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 Verbose mode; explain what is being done
-x, –one-file-system Only operate on the filesystem where the command was executed.
–help Display a help message, and exit.
–version Output version information, and exit

In usually, sparse source files are detected by a crude heuristic and the corresponding destination file is made sparse as well.

Examples

Suppose you want to copy a file to same location, you could be run the below Linux cp command.

At the same time, if you need to be copy a file to another location from the source path you can run the following command.

Linux CP Command

In the above example, the sample_New file is copied to “/root” location from “/root/work” directory. Now the same file is available in both directories also.

Multiple Files Copy:

If suppose sometime you have to copy the multiple files from one directory to another one directory. So you can do this follow the command, you can specify multiple files as the source, and a directory name as the destination.

Let’s have this scenario, the user name is “Paul”, the user has a lot of files in their directory “/home/Paul/docs/” named as file1, file2, etc. and you need to copy them into the directory /home/Paul/docs-backup/.

You could be run the below Linux cp command to copy the files.

cp ~/docs/docs-*.txt ~/docs-backup

Wildcard (the asterisk  “*” ) – it is used to indicate that the source files are all the files in the directory /home/Paul/docs whose name starts with “docs-” and has the extension “.txt“. And the new files have been copied to /home/Paul/docs-backup directory.

In this place you have note the /home/Paul/docs-backup directory already available. If that directory is not there in previously means Linux cp command gives you an error message, and also no files will copy.

Copying Files Recursively

Yet another Linux cp command options is called as recursive, which is used to copy entire directory structures from one place to another using the -R option to perform a recursive copy.

Let’s have this scenario, the user name is “Paul”, the user has a lot of files and sub directories in their directory “/home/Paul/docs/”. You want to copy all those files, and all the sub directories (and the files and sub directories they contain), to a new location, /home/Paul/docs-backup. You can copy all of them using the command:

cp -R ~/docs ~/docs-backup

In the above example, If the directory docs-backup already exists, the directory files will be placed inside. And If docs-backup does not already exist, it will be created and the contents of the files directory will be placed inside it.

Creating symbolic links instead of Copying Data

It is used to create symbolic links to your source files. You might be use In command to create a symlinks. Linux cp command is a best for create multiple symlinks all at once.  

cp creates symbolic links if you specify the -s option. So, for instance,

cp -s file.txt file2.txt

The above command creates a symbolic link, file2.txt, which points to file.txt and also you can also create symbolic links from various source files, specifying a directory as the destination.

Note: You must have keep in mind, if you want to create symbolic links in another directory, cp needs you to specify the full path name, including the full directory name, in your source file name(s). Relative paths will not work.

Version Control

The version control method may be selected via the –backup=control option or through the VERSION_CONTROL Environment variable. The list of possible values of control or VERSION_CONTROL are:

none, off             –              Never make backups (even if –backup is given)

simple, never    –              Always make simple backups.

numbered, t       –              Make numbered backups.

existing, nil        –              Numbered if numbered backups exist, simple otherwise.

Read More : Sh Command

Leave a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Scroll to Top