In this article, you will learn about the cp command in Linux.
The cp is the command in Linux and Unix-like operating systems. So, the use of the cp command is copying files and directories. By default, the cp command only copies files and not directories. If you are copying a file or directory and it already exists with the same name in the destination, then it will overwrite without asking. But, you can use the arguments for this issue.
How to get help for the cp command
I always suggest reading the manual page and help. So, It will give you an in-depth introduction to the command.
To get the manual page type the below command,
To get the only command options, you can type the below command.
Syntax of the cp command
Now you know how to get help with the cp command. So, let’s talk about cp syntax. The syntax of the cp command is as below,
Syntax: cp [option] source… destination
Copying from one location to another location
If you want to copy one file to the other location, then it will copy the file to the destination location. If the destination location does not exist, then It will create first, and then it will copy the file to the new location.
But if the destination exists and the same name is there, then it will overwrite that, which means the content of the file will be lost. But, It will have the same owner, group, and permission of the original file. The last access time of the source file and the last modification time of the new file will be set to the file.
For example, I am creating a file in the /root directory with the name test1 using the touch command and then copying this file into the /root/raj directory. See the command below.
cp test1 ./raj/
Copying multiple files at once
You can copy multiple files once.
cp file1 file2 file3 file4 file5 destination_dir_name
Copying with -i argument
When you use
-i an argument along with cp command then if any file with the same name exists then it will prompt for overwriting or leave. You can type y to overwrite or n for no overwrite and not copy. Here -i stands for interactive.
cp -i file1 dir1
Copying with -r argument
By default, you can not copy a directory to a directory. The
-r option, which can also be written with an upper case R, allows directories including all of their contents to be copied. Here -r stands for recursive.
cp -r source_dir destination_dir
Copying with -p argument using cp command
You want to preserve the file permission like ownership, timestamps while copying then you can use the -p option.
cp -p file1 file1.backup
Copying with -u argument using
This argument helps to update the file with the latest changes in the file. It will only copy if the file is newer than the destination.
cp -u file1.txt file1_backup.txt
Copying with -v argument
If you use -v which stands for verbose then you will get an output.
cp -v source destination
In this article, you learned about cp in Linux, and you can see that it is easy to use. So, I hope you understand, but if you have any questions, you can ask in the comment section.
Also, you may like to read about the
- Linux mv command.
- The hostnamectl command in Linux with examples
- You can visit redhat official website for help
- How to install Open Source Zimbra Collaboration Suite
What is the cp command in Linux | How to use the copy command in Ubuntu | How to use the copy command in Kali Linux | What is the use copy command in Linux | How to use copy command in Linux | copy command with examples in Linux | easy examples of copy command | How to copy files in Linux | copy-command |