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.txtto 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.txtto the directory/destination/directory. - Copy a directory and its contents:
cp -r source_directory /destination/directory
The
-roption tellscpto copy the directory and its contents recursively. This will copy the entiresource_directoryand all of its subdirectories and files to the directory/destination/directory. - Preserve file permissions when copying:
cp -p file1.txt file2.txt
The
-poption tellscpto preserve the original file’s permissions, timestamps, and other metadata. - Overwrite an existing file:
cp -f file1.txt file2.txt
The
-foption tellscpto force the overwrite of an existing file. Without this option,cpwill 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.