Friday, April 19, 2024
HomeLinux CommandsHow to copy Folder/File using cp command in Linux

How to copy Folder/File using cp command in Linux


What is cp:

cp (copy) is a command in various Unix and Unix-like operating systems for copy files and directories. It creates an exact same image of a file on a disk with new file name. The command has three principal modes of operation, expressed by the types of arguments presented to the program for 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 we gonna create a file “example.txt” and a directory “test” using following commands.

touch example.txt
mkdir test
ls

Now, in the following command we gonna copy a file “example.txt” to the “test” directory.

cp example.txt test


now use following command to check whether example.txt file is copied into test directory.

ls test

Copy multiple files

So in this example we gonna copy multiple files into a directory. Let assume we have many files with .txt extension and all we want to copy them in a directory “test” simply use following command.

cp *.txt test
ls test

* Asterisk means all files matching with .txt extension.

To 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 complete data having in this directory will also be copied.

We are copying example directory to the test directory.

ls
cp -R example test


Now verify example directory is copied into test directory.

ls test

That’s about it.

RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here


spot_img

Most Popular