Javatpoint Logo
Javatpoint Logo

cp Command in Linux/Unix | Linux Copy File

'cp' means copy. 'cp' command is used to copy a file or a directory.

Introduction to cp Command

The cp command is used to copy directories and files. The command contains three primary operation modes, represented by the argument types shown to the program to copy a file to other files, multiple files to any directory, or to copy the whole directories to other directories.

Further, the utility accepts several command-line option flags for detailing the operations implemented. The two primary specifications are GNU cp and POSIX cp. GNU cp contains various extra options on the POSIX version. Also, the command is available within the EFI shell.

Operating Modes of cp command

The cp command contains three primary operating modes. These modes are completed from the count and type of arguments shown to the program under invocation.

  • The program can copy the first file's contents to the second file, making the second file if essential when the program contains two path name arguments to files.
  • The program can copy all source files to the desired directory, making any file not already available when the program contains one or more path name arguments of files and pursuing those a path argument to a directory.
  • The cp command can copy every file inside the source directory to the desired directory, making any directories or files required when the arguments of the program are the path names for two directories. This operation mode needs an extra option flag to represent the recursive directory's copying. The source will be copied into the destination, while a fresh directory is made if the destination doesn't exist if the destination directory is already available.

Options of cp Command

Option Description
--archive, -a It is equivalent to the -dR --preserve=all.
--attributes-only It doesn't copy the data of the file, only the attributes.
-b It is like backup but doesn't accept any argument.
--backup[=CONTROL] It makes a backup of all existing destination files.
--copy-contents When recursive, it copies special file contents.
-d It is equivalent to --no-dereference --preserve=links.
--force, -f If any existing destination file can't be opened, delete it and attempt again (this option is avoided if the -n flag is used as well).
--interactive, -i It asks for confirmation before overwriting (overrides the previous -n flag).
-H In SOURCE, it follows the command-line symbolic links.
--link, -l It specifies hard link files rather than copying.
--dereference, -L In SOURCE, it always follows symbolic links.
--no-clobber, -n It doesn't overwrite any existing file (overrides the -i flag).
--no-dereference, -P In SOURCE, it never follows symbolic links.
-p It is equivalent to --preserve=mode, timestamps, ownership.
--preserve[=ATTR_LIST] It preserves the described attributes. If possible extra attributes: all, xattr, links, context.
--no-preserve=ATTR_LIST It doesn't preserve the described attributes.
--parents Under the directory, it uses the full source file name.
--recursive, -r, -R It recursively copies directories.
--reflink[=WHEN] It controls CoW/clone copies.
--remove-destination It removes all existing destination files before trying to open them (opposed to --force).
--sparse=WHEN It controls the establishment of sparse files.
--string-trailing-slashes It removes the trailing slashes from all SOURCE arguments.
--symbolic-link, -s It makes symbolic links rather than copying.
--suffix=SUFFIX, -S It overrides the common backup suffix.
--target-directory=DIRECTORY, -t It copies every SOURCE argument to DIRECTORY.
--no-target-directory, -T It considers DEST as a general file.
--update, -u It copies if the SOURCE file is newer as compared to the destination file or if the destination file is not specified.
--verbose, -v It specifies what is being done.
--one-file-system, -x It stays on the file system.
-Z It sets the destination file's SELinux security context to the default type.
--context[=CTX] It is the same as -Z, or if CTX is mentioned, it sets the SMACK or SELinux security context to CTX.
--help It shows the help menu and exits.
--version It provides version details and exits.

The sparse SOURCE files are found by a cheap heuristic, and the related DESR file is created sparse by default. It is the behavior chosen by --sparse=auto. We can describe --sparse=always to make a sparse DEST file if the SOURCE file includes a long enough order of zero bytes. We can also apply --sparse=never to constrain the establishment of sparse files.

If --reflink[=always] is mentioned, implement a lightweight copy, in which the data blocks have been copied only if changed. If it's not possible or --reflink=auto is mentioned, the copy fails and falls back to the standard copy. We can also apply --reflink=never to guarantee a standard copy is implemented.

To copy a file into the same directory syntax will be,

Linux File cp

In above snapshot, we have created a copy of 'docu' and named it as 'newdocu'. If in case, <new file name> (in our case it is 'newdocu') alreade exists, then it will simply over write the earlier file.

To copy a file in a different directory

We have to mention the path of the destination directory.

In the snapshot below, earlier there is no 'text' file. After giving the command, 'text' file has been copied to the destination directory that is 'Desktop'.

Linux File cp

Linux cp -r

Option 'r' with the copy command can be used to copy a directory including all its content from a source directory to the destination directory.



linux cp -r

In the above example, we have copied directory 'library' to the destination directory /home/sssit/Documents. Here, all the contents of 'library' directory including its contents have been copied to destination directory.

Linux Copy Multiple Files or Directories

Multiple files or directories can be copied to a destination directory at once. In this case, target must be a directory. To copy multiple files you can use wildcards (cp *.extension) having same pattern.



Linux copy multiple files

In above example, we have copied files (file1, file2, file3) having same extension '.txt' to Documents directory.

Linux cp --backup

If the file you want to copy already exists in the destination directory, you can backup your existing file with the use of this command.



Linux file cp backup

As you can see above, 'file2.txt' already exists in the destination directory. Hence, we have created a backup of this file and copied it in the same directory (having same name).

Now our destination directory that is 'Downloads' has two files with the same name (that is 'file2.txt').

Linux cp -i

The cp '-i' option allows you to confirm once before overwriting your file.



Linux cp -i

As you can see, it is asking for permission to over write the file 'file3.txt' as this file already exists in the destination directory. Now, you can press y to overwrite file and n not to overwrite file.

Linux cp -l

If you want to create a hard link of a file instead of copying that file, you can use option 'l'.

Note: In creating hard link of the file the inode number of the two files will remain same. While in case of copying, inode number changes.



Linux file cp -l

Note here that we have created a hard link of the file 'file1.txt' in 'usr'. Inode number of both the files are same.

Linux cp -p

The cp '-p' option is used to preserve the properties and attributes of a file. You can also preserve the selected properties which you want.



Linux cp -p

Now, you can match the two files (original one and the copied one) in the above picture, both have the same properties.

Linux cp -u -v

The cp -u -v command is used when you want to make sure that destination file is missing or doesn't exist.



Linux cp -u -v

In the above picture, you can see that there are two files 'docc' and 'file1.txt'. Now we want to copy these two files in the 'Download' directory. But we don't know that in the 'Downloads' directory file 'file1.txt' already exists.

Giving the command 'cp -u -v' will automatically take care of the already existing file (file1.txt) and will not over write it.

This command is useful in copying big files.

Next TopicLinux mv command

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA