The cp command in Linux: Copying files and directories
Introduction
While working in the terminal, you often have to copy files. The cp command (short for copy) is one of the main Linux tools for this task. It comes by default with all distributions and can copy files and folders, as well as preserve their attributes on Linux file systems.
About the cp command
The cp command is a powerful tool for copying files and directories. Its basic syntax is as follows:
cp [options] <source file/directory> <target file/directory>Basic usage examples
- Copying a single file:
cp file.txt /home/user/documents/- Copying a directory with all its contents:
cp -r dir1/ dir2/Key options of the cp command
INFO
Using the options correctly can greatly simplify the copying process and make it safer.
-ror--recursive: Recursive copying of directories with all their contents-vor--verbose: Detailed output of information about the copying process-ior--interactive: Prompt for confirmation before copying files-uor--update: Copying only new or updated files-por--preserve: Preserving all file attributes (owner, permissions, timestamps)
Advanced copying techniques
WARNING
When using advanced options, be careful, as incorrect use can lead to undesirable results.
- Copying with all attributes preserved:
cp -ap /source/directory/* /target/directory/- Copying only updated files:
cp -u /source/* /destination/- Copying with progress display:
cp -v /source/file /target/location/Practical recommendations
- Always use the
-iflag when working with important files - Use
-rto copy directories - Use
-pto preserve access permissions and timestamps - The
-vflag will help you track the copying process - Combine flags to work more efficiently
Additional information
To get detailed documentation on the cp command, you can use:
man cpINFO
It is recommended to always check access permissions and available free space before performing copy operations.
Frequently asked questions
Q: How do I copy hidden files?
A: Use the .* pattern: cp -r .* /destination/
Q: Can copying be cancelled?
A: No, the copying process cannot be cancelled once it has started. Use -i for confirmation.
INFO
Regularly make backups of important files and check the integrity of the copied data.
Our resources
Telegram channel: https://t.me/
F3 Cloud: https://f3cloud.com