Unix Copy Command Examples [ cp command ]
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.txtThis will copy the file
file1.txtto a new file namedfile2.txt. - Copy multiple files:
cp file1.txt file2.txt file3.txt /destination/directoryThis will copy
file1.txt,file2.txt, andfile3.txtto the directory/destination/directory. - Copy a directory and its contents:
cp -r source_directory /destination/directoryThe
-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.txtThe
-poption tellscpto preserve the original file's permissions, timestamps, and other metadata. - Overwrite an existing file:
cp -f file1.txt file2.txtThe
-foption tellscpto force the overwrite of an existing file. Without this option,cpwill prompt you for confirmation before overwriting an existing file.
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.