Open In App
Related Articles

cp command in Linux with examples

Improve Article
Save Article
Like Article

cp stands for a copy. This command is used to copy files or groups of files or directories. It creates an exact image of a file on a disk with a different file name. cp command requires at least two filenames in its arguments. 


cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

The first and second syntax is used to copy the Source file to the Destination file or Directory.
The third syntax is used to copy multiple Sources(files) to the Directory.

CP command working

It has three principal modes.

Two file names

 If the command contains two file names, then it copies the contents of 1st file to the 2nd file. If the 2nd file doesn’t exist, then first it creates one, and content is copied to it. But if it existed then it is simply overwritten without any warning. So be careful when you choose the destination file name.

cp Src_file Dest_file

Suppose there is a directory named geeksforgeeks having a text file a.txt.


$ ls

$ cp a.txt b.txt

$ ls
a.txt  b.txt

One or more arguments

If the command has one or more arguments, specifying file names and following those arguments, an argument specifying directory name then this command copies each source file to the destination directory with the same name, created if not existed but if already existed then it will be overwritten, so be careful!

cp Src_file1 Src_file2 Src_file3 Dest_directory


Suppose there is a directory named geeksforgeeks having a text file a.txt, b.txt and a directory name new in which we are going to copy all files. 

$ ls
a.txt  b.txt  new

Initially new is empty
$ ls new

$ cp a.txt b.txt new

$ ls new
a.txt  b.txt

Note: For this case the last argument must be a directory name. For the above command to work, Dest directory must exist because cp command won’t create it.

Two directory names

If the command contains two directory names, cp copies all files of the source directory to the destination directory, creating any files or directories needed. This mode of operation requires an additional option, typically R, to indicate the recursive copying of directories.

cp -R Src_directory Dest_directory

In the above command, cp behavior depends upon whether Dest_directory exists or not. If the Dest_directory doesn’t exist, cp creates it and copies content of Src_directory recursively as it is. But if Dest_directory exists then copy of Src_directory becomes sub-directory under Dest_directory.

Options in cp

 There are many options of cp command, here we will discuss some of the useful options: Suppose a directory named geeksforgeeks contains two files having some content named as a.txt and b.txt. This scenario is useful in understanding the following options.

$ ls geeksforgeeks
a.txt  b.txt

$ cat a.txt

$ cat b.txt
Option                           Detail Syntax Example Output

-i(interactive): i stands for Interactive copying. With this option the system first warns the user before overwriting the destination file. cp prompts for a response, if you press y then it overwrites the file and with any other option leaves it uncopied.

cp -i [Source_file] [Destination_file]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          cp -i a.txt b.txt                                                                                                                                                                                                         
cat b.txt



-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.

cp -b [Source_file] [Destination_file] cp -b a.txt b.txt

a.txt b.txt b.txt~

cat a.txt                             


cat b.txt


cat b.txt~



-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.

cp -f [Source_file] [Destination_file] cp -f a.txt b.txt
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.

cp a.txt b.txt

cp: cannot create regular file ‘b.txt’: Permission denied

#With -f option, command executed successfully.

cp -f a.txt b.txt
-r or -R

-r or -R: Copying directory structure. With this option cp command shows its recursive behavior by copying the entire directory structure recursively. Suppose we want to copy geeksforgeeks directory containing many files, directories into gfg directory (not exist).                                            

cp -r [Directory_name1] [Directory_name2] cp -r geeksforgeeks gfg
ls geeksforgeeks/

a.txt  b.txt  b.txt~  Folder1  Folder2

#Without -r option, error

cp geeksforgeeks gfg

cp: -r not specified; omitting directory ‘geeksforgeeks’

#With -r, execute successfully

cp -r geeksforgeeks gfg
ls gfg/

a.txt  b.txt  b.txt~  Folder1  Folder2


-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 change.

cp -p [Source_file] [Destination_file] cp -p a.txt c.txt
ls -l a.txt

-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt

cp -p a.txt c.txt
ls -l c.txt

-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt


Copying using * wildcard: The star wildcard represents anything i.e., all files and directories. Suppose we have many texts documents in a directory and want to copy it to 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.

cp *.txt [Destination Directory or file] cp *.txt Folder1

#Initially Folder1 is empty.


a.txt  b.txt  c.txt  d.txt  e.txt  Folder1

cp *.txt Folder1
ls Folder1

a.txt  b.txt  c.txt  d.txt  e.txt


The `cp` command is an essential tool which is used for copying files or groups of files and directories in Unix-Like operating systems. If we talk about its syntax it takes at least two filenames in as an argument (source and destination). As mentioned, the command has three principles: copying two file names, copying one or more arguments, and copying two directory names. Then we also mention the multiple options available while using `cp` command: `-i` , `-b` , `-f“ , `-r` , `-p`. To work with easy in Unix shell for file management one should know the proper working of `cp` command. 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Apr, 2023
Like Article
Save Article
Similar Reads