Cheat Sheet #day47 - cp
cp
Command Cheatsheet
The cp
command in Unix/Linux is used to copy files and directories. It stands for "copy" and can be utilized for various file management tasks. Below are the basic and advanced usages of the cp
command along with practical examples.
Basic Usage
Copy a file to another directory
cp source_file /destination/directory/
Copy and rename a file
cp source_file /destination/directory/new_filename
Common Options
Interactive mode (prompt before overwrite)
cp -i source_file /destination/directory/
Force copy without prompting
cp -f source_file /destination/directory/
Verbose mode (show what is being done)
cp -v source_file /destination/directory/
Recursive copy (copy directories and their contents)
cp -r source_directory /destination/directory/
Preserve file attributes (ownership, timestamps, etc.)
cp -p source_file /destination/directory/
Update only if the source file is newer
cp -u source_file /destination/directory/
Create hard links instead of copying
cp -l source_file /destination/directory/
Create symbolic links instead of copying
cp -s source_file /destination/directory/
Examples
Copy a single file
cp file.txt /home/user/Documents/
Copy and rename a file
cp file.txt /home/user/Documents/new_file.txt
Copy multiple files to a directory
cp file1.txt file2.txt file3.txt /home/user/Documents/
Copy a directory recursively
cp -r /home/user/Downloads /home/user/Documents/
Interactive copy (prompt before overwrite)
cp -i file.txt /home/user/Documents/
Verbose copy
cp -v file.txt /home/user/Documents/
Advanced Usage
Copy all files with a specific extension
cp *.txt /home/user/Documents/
Copy files using a wildcard and rename
cp *2023.log /home/user/Logs/
Copy and overwrite only if the source file is newer
cp -u file.txt /home/user/Documents/
Preserve file attributes during copy
cp -p file.txt /home/user/Documents/
Create a hard link to a file
cp -l file.txt /home/user/Documents/
Create a symbolic link to a file
cp -s file.txt /home/user/Documents/
Practical Tips
Safety First: Use the
-i
option to prevent accidental overwrites.Preserve Attributes: Use the
-p
option to maintain file attributes.Verbose Mode: Use
-v
to see exactly whatcp
is doing, useful for debugging scripts.Recursive Copy: Always use
-r
when copying directories to ensure all contents are copied.
Quick Reference
Copy a file:
cp source_file /destination/directory/
Copy and rename a file:
cp source_file /destination/directory/new_filename
Interactive mode:
cp -i source_file /destination/directory/
Force copy:
cp -f source_file /destination/directory/
Verbose mode:
cp -v source_file /destination/directory/
Recursive copy:
cp -r source_directory /destination/directory/
Preserve file attributes:
cp -p source_file /destination/directory/
Update only if newer:
cp -u source_file /destination/directory/
Create hard link:
cp -l source_file /destination/directory/
Create symbolic link:
cp -s source_file /destination/directory/
This cheatsheet covers the essential commands and options for using cp
effectively, from basic file copies to more advanced file management tasks. Adjust the commands according to your specific requirements and environment.
Subscribe to my newsletter
Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by