cp Command in Linux



cp command is a versatile command used in Linux systems for copying files and directories from one location to another. To use the cp command on Linux, you must provide a source and destination location. The source location is the path where the file or directory is placed, while destination is the new location of the file or directory you want to place the copy.

Syntax of cp Command

The basic syntax to use the cp command in Linux is provided below −

cp [OPTIONS] SOURCE... DESTINATION

Here, the SOURCE can be one or more files or directories, while DESTINATION can be a single file or directory. The [OPTIONS] include different flags that you can use with the cp command for changing its behavior.

Common Options with cp Command

The following table summarizes different options available for the cp command in Linux −

Option Description
-a, --archive Equivalent to --dR --preserve=all
--attributes-only Copy only the file attributes, not the data.
--backup[=CONTROL] Creates a backup of every existing destination file.
-b Similar to --backup but doesn’t accept an argument
--copy-contents Copies the content of special files when used recursively.
-d Equivalent to --no-dereference --preserve=links.
--debug Provide an explanation about the way a specific file is copied.
-f, --force If an existing destination file fails to open, remove it and tries again (it ignores when -n is used).
-i, --interactive Show prompts before performing the overwriting operation (overrides a previous -n option).
-H Follow command-line symbolic links in the source.
-l, --link Create hard links instead of performing the copy operation.
-L, --dereference Always go towards the symbolic links in the source.
-n, --no-clobber Prevent overwriting an existing file and avoid failure (overrides -u or previous option); equivalent to --update=none.
-P, --no-dereference Does not follow the symbolic links in the source.
-p Equivalent to --preserve=mode,ownership, timestamps.
--preserve[ATTR_LIST] Preserve the specified attributes.
--no-preserve=ATR_LIST Does not preserve the specified attributes.
--parents Use the full source file name under the directory.
-R, -r, --recursive Recursively copy directory.
--reflink[=When] Control CoW/clone copies.
--remove-destination Remove any existing destination files from the system before attempting to open them (in contrast to using the --force option).
--sparse=When Manage the sparse files creation.
--strip-trailing-slashes Eliminate any trailing slashes from each source argument.
-s, --symbolic-link Create symbolic links instead of performing the copying process.
-S, --suffix=SUFFIX Specify an alternative backup suffix instead of the default.
-t, --target-directory=Directory Move all the source arguments into the specified directory.
--update[=UPDATE] Controls which existing files are updated; UPDATE={all, none, older(default)}
-u Equivalent to --update[=older]
-v, --verbose Explains what is being done.
-x, --one-file-system Don’t move from this file system.
-Z Assign the default SELinux security context to the destination file.
--context[=CTX] Perform similar to -Z, or if CTX is specified, it will set the SELinux or SMACK security context to CTX.
--help Display help related to command and exit.
--version Output the command’s version information and exit.

Examples of cp Command in Linux

Let’s explore some a few examples of cp command in Linux system −

  • Copying Content from One File to Another
  • Copying Files to a Directory
  • Copying Directories
  • Copying Hidden Files
  • Creating a File Backup

Copying Content from One File to Another

One of the basic advantages of using the cp command on Linux is to copy the content of a file into another file. To use it, simply specify both the source and the destination file; If the destination file already exists, its content will be overwritten.

For example, let’s say you have two files named file1.txt and file2.txt that are saved on a Linux system. The text inside file1.txt is “Hello Tutorial Point Users”, while there is nothing inside the file2.txt (empty file). If you want to copy the content of file1.txt into the file2.txt, you can simply run the below-given command −

cp file1.txt file2.txt

The above command will simply copy the file1.txt into the file2.txt, you can confirm it by using the cat command −

cat file1.txt file2.txt
Copying Content One File to Another

Copy Files to a Directory

Another advantage of cp command on Linux is to copy a single or multiple files from one location to another. You can do this by using the cp command with the files names you want to copy and the destination in the end where you want to place them.

For example, if you have a single file let’s say file1.txt and want to copy that specific file to a destination called Documents, then simply use the following command −

cp file1.txt Documents
Copy Files to a Directory 1

You can replace the file name and destination according to your desired choice.

For copying multiple files on Linux from one location to another, you can simply provide the file names you want to copy separated by a space and the destination path in the last. For example, you have two files named file1.txt and file2.txt. To copy these files to a destination Documents, you can use the following command −

cp file1.txt file2.txt Documents
Copy Files to a Directory 2

If a file with the same name is already present at the destination, the cp command will replace that file and to avoid it, you can use the -i option with the cp command. It will ask for your permission whether to replace the file with the one already available at the destination. You can accept the overwrite by typing yes or prevent it by typing no.

cp -i file1.txt file2.txt Documents
Copy Files to a Directory 3

You can also copy multiple files all at once using the wild card option *, this helps you avoid specifying the file name separately. The syntax to copy multiples files of the same .txt format is given below −

cp *.txt destination

Copying Directories

Apart from copying files, you can also use the cp command on Linux to copy directories and all of its content from one location to another. However, for that purpose, you must use the -r or -R option with the cp command. For example, to copy a directory named myfolder to a destination called Downloads, you can use the following command −

cp -r myfolder Downloads
Copying Directories

Copying Hidden Files

The cp command can also be used to copy hidden files from one place to another. For that purpose, you must use the -a option with the flag, which stands for archive mode. It preserves file attributes and copies hidden files and regular files.

For example, the following command will copy the hidden file .myfile.txt to the Downloads directory −

cp -a .myfile.txt Downloads
Copying Hidden Files

Creating a File Backup

The cp command can also be used on Linux to create a backup of your existing file. It is possible with the help of using the --backup option. When you use this method, it avoids overwriting existing files in the directory, instead, it creates a new file with the same name.

For example, to create a backup of a text file named file1.txt with the name file1_backup.txt, run the below-given command −

cp --backup file1.txt file1_backup.txt
Creating a File Backup

Make sure to replace the file1.txt with the filename of the source text file, while file1_backup.txt needs to be set on your own.

In this way, you can use the cp command to perform different copy related activities on your system.

Conclusion

The cp command is a versatile and robust tool for copying files and directories in Linux. Whether you are copying individual files, entire directories, or hidden files, understanding its different options allows you to customize the copying process according to your needs.

We have discussed in this tutorial the syntax for cp command, along with its different options and examples for better understanding. Following this will help you efficiently manage file copies operation on your Linux system.

Advertisements