The cp
(copy) command is used to copy files and directories in Unix-like systems. Here are some common examples of using the cp
command:
- Copy a single file:
cp file1.txt file2.txt
This will copy the file
file1.txt
to a new file namedfile2.txt
. - Copy multiple files:
cp file1.txt file2.txt file3.txt /destination/directory
This will copy
file1.txt
,file2.txt
, andfile3.txt
to the directory/destination/directory
. - Copy a directory and its contents:
cp -r source_directory /destination/directory
The
-r
option tellscp
to copy the directory and its contents recursively. This will copy the entiresource_directory
and all of its subdirectories and files to the directory/destination/directory
. - Preserve file permissions when copying:
cp -p file1.txt file2.txt
The
-p
option tellscp
to preserve the original file’s permissions, timestamps, and other metadata. - Overwrite an existing file:
cp -f file1.txt file2.txt
The
-f
option tellscp
to force the overwrite of an existing file. Without this option,cp
will prompt you for confirmation before overwriting an existing file.
These are just a few examples of using the cp
command. The cp
command has many other options and uses, and you can find more information by running man cp
in a terminal window.