Copy file using cp command in Linux

Copy file using cp command in Linux

Copy file using cp command in Linux

Want to learn how to copy files in Linux terminal? Then you are the right place. In this tutorial, you learn how to copy files using: cp command On Linux.

Basic syntax cp command

The basic syntax of the command is as follows:

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

Where,option: Additional options used in cp command It is listed here.Source: Source file or directory.Destination: The destination directory or file to copy the source to.

Copy files to another location

You can copy the file to another location using a simple cp command. In the following example, the file first.txt is copied to the same directory by name second.txt:

cp first.txt second.txt

The output looks like this:

Copy file using cp command in Linux

Copy multiple files using cp command

To copy multiple files to another directory, simply copy their names cp command Without any options.

In the following example, three files first.txt,second.txt,third.txt Will be copied to ~/Desktop/demo2 Directory using the following command:

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

The output looks like this:

Copy file using cp command in Linux

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

Copy file --verbose option

Use advanced options to copy files to another directory -v The file will be copied to as below backup directory:

cp first.txt backup

The output looks like this:

Copy file using cp command in Linux

Copy one directory to another using cp command

You can use to move a directory to another directory. -R (Recursive) Optional. When to use -R Selecting an option copies the current directory to another destination directory.

The following example will copy demo To directory demo2 directory:

cp -R demo demo2

The output looks like this:

Copy file using cp command in Linux

Copy all files in the current directory to another directory

Can be used to copy all the files in one directory to another directory. -R (Recursive) Optional. When to use -R The option is to copy all files in the specified directory to another destination directory.

In the following cp command example, demo To directory demo2 directory:

cp -R * ~/Desktop/demo2

The output looks like this:

Copy file using cp command in Linux

Copy files to destination directory using -force option

Can be used -f Option to copy files if you do not have write permission to the destination directory. When to use -f Specifying an option forces the file to be copied to the destination directory, even if you do not have write permission to the destination directory.

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

cp -f data.doc demo3

The output looks like this:

Copy file using cp command in Linux

Copy file with file attributes preserved using cp command

When copying files using the cp command, you can preserve file attributes such as modification date and time and access control lists. To preserve file attributes using the cp command -p option.

In the following example, the file name is first.txt Copied to file four.txt Holds attributes.

cp -p first.txt four.txt

Conclusion

You learned how to copy files using the cp command on Linux. If you have any questions about this, don’t forget to comment below.

Sidebar