cp command in Linux/UNIX

Filed Under: UNIX/Linux

In this guide, we focus on cp command in Linux/Unix systems. cp command – short for copy – is a command used for copying files and directories in Linux. It’s a command that is widely used both by Linux beginners and seasoned experts on a regular basis. Let’s dive in and see how this command is used.

Linux cp command example

The basic command structure of cp command is as follows:

$ cp [options] source dest

Let’s look at some examples of using cp command to copy files and folders in Linux.

Copy a file from current directory to subdirectory

To copy a file from the present working directory to a subdirectory use the syntax below:

$ cp file directory

Example

For example, to copy a file file1.txt to a sub directory called data execute the cp command as follows. Later use the ls command to verify the existence of the file in the directory.

$ cp file1.txt data

Sample output

cp command in Linux

Copy a file from current directory to an absolute destination path

To copy a file from the present working directory to a different destination directory or absolute path, use the syntax below.

$ cp file /path/to/directory

Example

$ cp file1.txt /home/james/

Output

Linux copy file

Copy a file from absolute source path to an absolute path

To copy a file(s) from a different file path to another absolute path use the syntax:

cp /path/to/source/file /path/to/destination/directory

Example

$ cp /home/james/file1.txt /opt/data

Sample output

Copy File From Absolute Path To Another Absolute Path

Copy multiple files

Linux cp command also allows you to copy more than one file at a go using the syntax below.

$ cp file1 file2 .... /path/to/directory

Example

$ cp file1.txt file2.txt file3.txt /home/james/

Sample output

Linux Copy Mutiple Files using cp command

Copy file with verbose output

To display verbose output, use the -v flag as shown.

cp -v file1.txt file2.txt  /home/james

Sample output

Linux Copy File With Verbose Output

Copy files interactively

To copy files interactively i.e to provoke a prompt from the Linux shell, use the -i flag as shown.

$ cp -i file1.txt /home/james

Sample output

interactive Copying of files using cp command

Copy files associated with a certain file extension

If you wish to copy a number of files with the same file extension, say .txt or .pdf follow the example below.

$ cp *.txt  /home/james

Linux Copy Directory Recursively

If you want to copy a directory alongside all its contents, use the -R attribute followed by the directory name as shown.

$ cp -R data /home/james

Sample output

Linux Copy Directory

To copy the contents of a directory but not the directory itself, run the command as follows.

$ cp -R data/* /home/james

Sample output

Linux Copy Contents Of Directory But Not Directory Itself

Linux man cp

We can run man cp command to look at the documentation of cp command. This is useful to find out the right options to use with the cp command.

man cp

Sample output

man cp

Summary

Linux cp command helps us in copying files and folders easily. It’s one of the most used commands. The man page of cp command is very helpful in finding the suitable options to use.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages