Linux Commands

Linux cp Command Examples

While working on a Linux system, copying files and directories is an important task that is performed daily. All users need simple and easy utility through which they can copy all their files and directories. For this purpose, the most common cp command-line utility is used on UNIX and Linux systems.

We will explain the cp command with some examples in this article.

Basic Syntax of cp command

To use the cp command, follow the syntax, which is given below:

$ cp [flags] [source-file] [destination-file]

The source file can contain more than one file and directories in the above syntax, and the destination file can only be a single file or a directory.

Important Note: The user should have read permission on a source file while copying files and directories, and the user must have write permission on the destination file or directory. Otherwise, an error of ‘permission denied’ will display.

Use of cp command

There are the following uses of the ‘cp command’ that we will now explain with some examples:

Copy file into the current working directory

To copy a file into the current directory, execute the following command on the terminal:

Example

For example, by running the below-mentioned command to you can copy a test_file.txt to the backup_file.txt:

$ cp test_file.txt backup_file.txt

Copy file into another directory

To copy the file into another directory, define an absolute or relative directory path of the destination.

Example

For example, to copy test_file.txt to the /lookup directory, use the following command:

$ cp test_file.txt /lookup

In the above command, the file is copied with the same original file name. If you want to copy the file with a different name, then use the below-mentioned command:

$ cp test_file.txt /lookup/newtest_file.txt

The above command will copy the file with a new name ‘newtest_file.txt’ into the specified destination.

Copy directories into another directory

Using the option ‘-R’ or ‘-r’, you can copy all files, including sub-directories, into another directory.

Example

In the following example, we are coping personal_directory into the official_directory:

$ cp -R personal_directory official_directory

If you want to copy only files and all subdirectories rather than the source directory, use the following command with option ‘-RT’:

$ cp -RT personal_directory official_directory

The above command will copy the directory’s content, including all hidden files instead of the directory itself.

Copy multiple files in different directories

Using the following command, you can copy multiple files into different directories:

$ cp test_file.txt personal_directory test_file1.txt official_directory

Linux cp command with options

The following options you can use with the cp command to copy a file differently:

If the destination file exists, then the file will be overwritten by default. Using the ‘-n’ flag with the cp command tells us not to overwrite an already existing file.

Use the option ‘i’ to generate the prompt for confirmation forcefully.

$ cp -i test_file.txt test_file1.txt

If you only want to copy a file on the destination, if it doesn’t already exist then, use the following command with option ‘-u’:

$ cp -u test_file.txt test_file1.txt

To preserve the ownership and timestamps of a file, use the following command with option ‘-v’:

$ cp -u test_file.txt test_file1.txt

Conclusion

We have explained the cp command by using the different examples in this article. Moreover, we also explained how to use different options with the cp command to get the desired results. Hence, using the above all options with the cp command, you copy files and directories on different locations into your Linux system. I am sure now you have good knowledge about the cp command and its usage.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.