Linux cp command, copying files
Last updated: May 26, 2022
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.
- Syntax for cp.
- Copy the file.
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
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
You can also rename the file when copying a file to another directory.
$. cp file_copy.sql /tmp/file_copy_1.sql
2.3. Copy the file while maintaining the rights.
$. cp -p /home/oracle/file.txt /tmp/file.txt
Created file file.txt under oracle account.
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/
Created two directories.
The folder1 directory contains three files and another directory, while the folder2 directory is empty.
After executing the command, all contents from the folder1 directory were successfully copied to the folder2 directory.
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.