Copy Files using cp command in Linux


Copy File using cp command in Linux

Do you want to learn how to copy files in the Linux terminal? Then you are the right place. In this tutorial, you are going to learn How to Copy File using cp command in Linux.

Basic Syntax for cp command

Following is the basic syntax for command:

cp [OPTIONS] SOURCE_1 SOURCE_2 SOURCE_N DESTINATION
cp [OPTIONS] SOURCE DESTINATION

Where,
OPTIONS: The extra options used with the cp command listed here.
SOURCE: The source file or directory.
DESTINATION: The destination directory or file to copy the source.

Copy a File to Another Location

We can copy a file to another location by using a simple cp command. In the following example, the file first.txt will be copied in the same directory with the name second.txt:

cp first.txt second.txt

The output should look like:

"Copy

Copy Multiple Files Using cp Command

To copy multiple files to another directory you can just pass their names with cp command without using any option.

In the following example, three files named first.txt,second.txt,third.txt will be copied to ~/Desktop/demo2 directory using below command:

cp first.txt second.txt third.txt ~/Desktop/demo2

The output should be:

"Copy

As given in the above example you can copy multiple files to another directory using cp command.

Copy Files with --verbose Option

You can copy the file to another directory using verbose option -v like given below, the file will be copied to backup directory:

cp first.txt backup

The output should be:

"Copy

Copy One Directory to Another Directory using cp command

You can a directory to another directory by using -R (recursive) option. When you use -R option, the current directory will be copied to another destination directory.

In the following example, it will copy demo directory to demo2 directory:

cp -R demo demo2

The output should be:

"Copy

Copy All Files in Current Directory to Another Directory

You can copy all files in a directory to another directory by using -R (recursive) option. When you use -R option all files in the given directory will be copied to another destination directory.

In the following cp command example, it will demo directory to demo2 directory:

cp -R * ~/Desktop/demo2

The output should be:

"Copy

Copy File to Destination Directory With –force Option

You can use -f option to copy files when you don’t have write permission for the destination directory. if you use -f option then files will be forcefully copied to destination directory even if write permission to destination directory is not given.

In the following example, the file named data.txt will be copied to /home/Desktop/demo3 directory:

cp -f data.doc demo3

The output should look like:

"Copy

Copy Files by Preserving File Attributes using cp Command

When you copy files using cp command, you can preserve file attributes like modification date, time, and access control list, etc. To preserve file attributes using cp command with -p option.

In the below example the file named first.txt copied to file four.txt by preserving attributes.

cp -p first.txt four.txt

Conclusion

You have successfully learned how to Copy Files using cp command in Linux. If you have any queries regarding this then please don’t forget to comment below.