Linux cp command, copying files

  • Last updated: Oct 14, 2024
  • Views: 7
  • Author: Admin
Linux cp command, copying files

Colleagues hello to all.

In today's article, I will tell you about such a command as cp. The cp  command is designed to copy files and directories, one of the most common tasks in Linux in general.

 

Article content:

  1. Syntax for cp.
  2. Copy the file.
  3. Summary

 

1. Command syntax cp.

The cp command is very easy to use. Command syntax:

cp [OPTIONS] SOURCE... DESTINATION

SOURCE - is a source, the source can be a file or a directory.

DESTINATION - is where we want to put the source SOURCE.

[OPTIONS] - are the options that will be described later in the article.


 

2. Copying the file.

2.1. Copy the file to the same directory with a different name.

$. cp file.sql file_copy.sql

linux_command_copy

The file named file_copy.sql is exactly the same as the file named file.sql only with a different name.

 

2.2. Copy the file to another directory. You can specify both a relative path and an absolute one.

$. cp file_copy.sql /tmp/file_copy.sql

linux_command_copy

 

You can also rename the file when copying a file to another directory.

$. cp file_copy.sql /tmp/file_copy_1.sql

linux_command_copy


 

2.3. Copy the file while maintaining the rights.

$. cp -p /home/oracle/file.txt /tmp/file.txt

linux_command_copy

Created file file.txt under oracle account.

 

linux_command_copy

As you can see in the screenshot, the rights of the file did not change after it was copied to the /tmp directory

 

2.4. Recursively copy files and directories to another directory.

$. cp -R /root/folder1/* /root/folder2/

 

linux_command_copy

Created two directories.

 

linux_command_copy

The folder1 directory contains three files and another directory, while the folder2 directory is empty.

 

linux_command_copy

After executing the command, all contents from the folder1 directory were successfully copied to the folder2 directory.


 

3. Results.

You can write many more examples with this command, but I showed you the options that you will use almost every day.


 

Thank you all, I hope that my article helped you in some way.

 

SIMILAR ARTICLES