How to copy File and Directory in Linux ?

The “cp” means copy in Linux. This “cp” command is used to copy the files and directory to another file or directory.

Let’s have look at the “cp” command in Linux

The syntax for the “cp” command is :

cp [OPTION]... SOURCE... DESTINATION

The source is the file or directory which we want to copy and the destination where we want to copy.

If the provided destination file does not exist then the “cp” command creates that file.

Copy file to same location : Create a duplicate and rename

To copy the content of the linux1.txt file into the embedded.txt file we write

$ cp linux1.txt embedded.txt

Using the “cat” command we can verify the content.

Copy file to another directory

To copy the linux1.txt file into the dir1 directory we write

$ cp linux1.txt dir1

Copy Multiple files to another directory

To copy multiple files into the directory we write all the source files after “cp” and lastly the desired directory

$ cp linux1.txt embedded.txt os.txt dir1

In the above code linux1.txt, embedded.txt and os.txt has been copied to dir1 and using the “ls” command we can verify that

Recursive Copy : Copy all contents of one directory to another

To copy the entire directory into another directory we use the option “-r

$ cp -r dir1/* dir2

In the above code, dir1/* indicates copy all contents of dir1 to dir2 ( Note: * indicates all )

Have a look at other basic Linux commands on the website

Leave a Comment