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.
Table of Contents
- 1 Linux cp command example
- 2 Copy a file from current directory to subdirectory
- 3 Copy a file from current directory to an absolute destination path
- 4 Copy a file from absolute source path to an absolute path
- 5 Copy multiple files
- 6 Copy file with verbose output
- 7 Copy files interactively
- 8 Copy files associated with a certain file extension
- 9 Linux Copy Directory Recursively
- 10 Linux man cp
- 11 Summary
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
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
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 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
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
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
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
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 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
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.
very much helpful for beginners.