cp command in Linux with examples
cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name. cp command require at least two filenames in its arguments.
1. CP COMMAND
SYED SADATH ULLAH
M.Tech;
Linux – Administrator
Topics Listed:
1) Cp command syntax with example
2) How to copy a file from current directory to subdirectory
3) How to copy a file from current directory to an absolute destination path
4) How to copy a file from absolute source path to an absolute path
5) How to copy multiple files
6) How to copy file with verbose output
7) How to copy files interactively
8) How to copy files associated with a certain file extension
9) How to copy Directory Recursively
10) Linux man cp
2. 1) Cp command syntax with example
The basic command structure of cp command is as follows:
Command: $ cp [options] source destination
Let’s look at some examples of using cp command to copy files and folders in Linux.
Command: $ ls
a.txt
Command: $ cp a.txt b.txt
Command: $ ls
a.txt b.txt
2) How to copy a file from current directory to subdirectory
To copy a file from the present working directory to a subdirectory use the syntax below:
Command: $ 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.
Command: $ cp file1.txt data
Sample output
3. 3) How to 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.
Command: $ cp file /path/to/directory
Example
Command: $ cp file1.txt /home/james/
Output:
4) How to 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.
Command: $ cp /path/to/source/file /path/to/destination/directory
Example
Command: $ cp /home/james/file1.txt /opt/data
Sample output
4. 5) How to copy multiple files
Linux cp command also allows you to copy more than one file at a go using the syntax below.
Command: $ cp file1 file2 .... /path/to/directory
Example
Command: $ cp file1.txt file2.txt file3.txt /home/james/
Sample
output
6) How to copy file with verbose output
To display verbose output, use the -v flag as shown.
Command: cp -v file1.txt file2.txt /home/james
Sample output
7) How to copy files interactively
To copy files interactively i.e to provoke a prompt from the Linux shell, use the -i flag as shown.
5. Command: $ cp -i file1.txt /home/james
Sample output
8) How to 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.
Command: $ cp *.txt /home/james
9) How to 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.
Command: $ cp -R data /home/james
Sample output
To copy the contents of a directory but not the directory itself, run the command as follows.
Command: $ cp -R data/* /home/james
Sample output
6. 10) 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.
Command: man cp
Sample output
7. 11) Usinng cp –f (Force)
-f(force): If the system is unable to open destination file for writing operation because the user
doesn’t have writing permission for this file then by using -f option with cp command, destination
file is deleted first and then copying of content is done from source to destination file.
Command: $ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt
User, group and others doesn't have writing permission.
Without -f option, command not executed
Command: $ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied
With -f option, command executed successfully
Command: $ cp -f a.txt b.txt
12) Using cp –b (backup)
-b(backup): With this option cp command creates the backup of the destination file in the same
folder with the different name and in different format.
Command: $ ls
a.txt b.txt
Command: $ cp -b a.txt b.txt
Command: $ ls
a.txt b.txt b.txt~
8. 13) Using cp * wildcard:
The star wildcard represents anything i.e. all files and directories. Suppose we have many text
document in a directory and wants to copy it another directory, it takes lots of time if we copy
files 1 by 1 or command becomes too long if specify all these file names as the argument, but
by using * wildcard it becomes simple.
Note: Initially Folder1 is empty
Command: $ ls
a.txt b.txt c.txt d.txt e.txt Folder1
Command: $ cp *.txt Folder1
Command: $ ls Folder1
a.txt b.txt c.txt d.txt e.txt
14) Using cp -p
. -p(preserve): With -p option cp preserves the following characteristics of each source file in the
corresponding destination file: the time of the last data modification and the time of the last
access, the ownership (only if it has permissions to do this), and the file permission-bits.
Note: For the preservation of characteristics you must be the root user of the system, otherwise
characteristics changes.
Command: $ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt
Command: $ cp -p a.txt c.txt
Command: $ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt
Note: Here, we can see above both a.txt and c.txt (created by copying) have same
characteristics.
Thank U