In this tutorial, you’ll explore how to use the cp command in Linux. The cp
command is a widely used command in Linux used to copy files and directories. The command stands for “copy,” and it is used to copy files and directories from one location to another. In this blog, we will discuss the cp
command in detail and how to use it.
The cp
command is a command-line utility used to copy files and directories. The command takes the following syntax:
cp [options] source destination
The cp
command takes the following options:
-r
: Copy directories recursively.-v
: Verbose output, displays the name of each file as it is copied.-i
: Interactive mode, prompts the user before overwriting any files.-p
: Preserves the original file attributes, including ownership, mode, and timestamps.
Usage of cp command in Linux
Here are some examples to understand how the cp
command works:
Copy a single file:
To copy a single file, you can specify the source file name and the destination file name as arguments to the cp
command. For example, to copy a file named file1.txt
to a new file named file2.txt
, you can run the following command:
cp file1.txt file2.txt
Copy multiple files:
To copy multiple files at once, you can specify the source file names and the destination directory name as arguments to the cp
command. For example, to copy three files named file1.txt
, file2.txt
, and file3.txt
to a directory named mydir
, you can run the following command:
cp file1.txt file2.txt file3.txt mydir/
Copy a directory:
To copy a directory, you need to use the -r
option along with the cp
command. For example, to copy a directory named mydir
to a new directory named mydir2
, you can run the following command:
cp -r mydir mydir2
Verbose output:
To display the name of each file as it is copied, you can use the -v
option with the cp
command. For example, to copy a file named file1.txt
to a new file named file2.txt
with verbose output, you can run the following command:
cp -v file1.txt file2.txt
Interactive mode:
To prompt the user before overwriting any files, you can use the -i
option with the cp
command. For example, to copy a file named file1.txt
to a new file named file2.txt
with interactive mode, you can run the following command:
cp -i file1.txt file2.txt
Preserve file attributes:
To preserve the original file attributes, including ownership, mode, and timestamps, you can use the -p
option with the cp
command. For example, to copy a file named file1.txt
to a new file named file2.txt
and preserve file attributes, you can run the following command:
cp -p file1.txt file2.txt
Conclusion
The cp
command is a powerful tool that is used to copy files and directories from one location to another. It is important to use this command with caution as it can overwrite existing files and directories. Always double-check the files and directories before copying them to avoid any accidental deletion or overwriting, see the man page for the cp command in Linux.
I hope this article has been helpful in understanding the cp command in Linux. Thank you for reading!
If you have any questions please leave them in the comments below