Copy file/folder Using cp Command in Linux

0
138

cp (copy) is a command in various Unix and Unix-like operating systems for copying files and directories. The command has three principal modes of operation, copying a file to another file, one or more files to a directory, or for copying entire directories to another directory.

Copy a file

First of all create a file “demo.txt” and a directory “record” using below commands.

touch demo.txt
mkdir record
ls

Now, in the below command we will copy a file “demo.txt” to the “record” directory.

cp demo.txt record

Then, use following command to verify demo.txt file is copied into record directory.

ls record

Copy multiple files

So, in this example we will copy multiple files into a directory. let say we have many files with .txt extension and we want all them to copy in a directory “record” simply use following command.

cp *.txt record
ls record

* means all files matching with .txt extension.

Copy a Directory

Now we will copy a directory into another directory.
Note: Always use -R (capital R) option whenever you copy a directory so that all of data placed in it will also be copied.

SO, We are copying demo directory to the record directory.

ls
cp -R demo record

Then, verify demo directory is copied into record directory.

ls record

You can explore more commands of cp by reading manual using command “man cp

LEAVE A REPLY

Please enter your comment!
Please enter your name here