In the Ubuntu system, the most commonly used command to copy files and directories is cp. Beyond its basic “copy a single file” usage, cp offers many advanced features that help you efficiently handle file operations, such as batch copying, recursive directory copying, and preserving file attributes. Let’s explore these practical advanced usages with examples to help you get started quickly.
1. Batch Copy Multiple Files to a Target Directory¶
If you need to copy multiple files (e.g., all text files, images, etc.) to a directory at once, you don’t have to enter commands one by one. Instead, use the wildcard * for batch processing.
Example: Suppose your current directory has three text files: a.txt, b.txt, and c.txt, and you want to copy all of them to a directory named docs.
First, ensure the docs directory exists (create it with mkdir docs if it doesn’t). Then run:
cp *.txt docs/
Explanation: *.txt matches all files ending with .txt in the current directory, and docs/ is the target directory. After execution, the docs folder will contain these three text files.
2. Copy an Entire Directory (Including Subfolders and Content)¶
If you want to copy a folder with subfolders or files, using cp alone only copies the folder itself, not its contents. In this case, add the -r parameter (recursive copy).
Example: Suppose you have a work directory containing a src subfolder and a data.txt file, and you want to copy the entire work directory to the backup folder.
Run:
cp -r work/ backup/
Explanation: The -r flag makes cp recursively traverse all subfolders and files within work, copying the entire directory structure to backup. Without -r, backup would only contain an empty work folder shell, and its contents would not be copied.
3. Preserve File Attributes (Permissions, Timestamps, etc.)¶
Sometimes, after copying a file, you might find its permissions changed (e.g., a read-only file becomes writable) or its modification time reset. Use the -a parameter (archive) to retain almost all file attributes, including permissions, ownership, and timestamps.
Example: Copy the system configuration file /etc/hosts to the /tmp directory and rename it to hosts.bak while preserving its original attributes:
cp -a /etc/hosts /tmp/hosts.bak
Explanation: The -a flag ensures hosts.bak fully inherits the original file’s permissions, owner, modification time, etc., rather than just copying content. Without -a, the permissions might reset to default settings.
4. Interactive Copy to Avoid Accidental Overwrites¶
If the target location already has a file with the same name, cp will overwrite it by default (risking data loss). Add the -i parameter (interactive) to prompt you before overwriting, ensuring confirmation.
Example: Copy old.txt to new.txt in the current directory (if new.txt exists):
cp -i old.txt new.txt
Explanation: The terminal will display: new.txt: 目标存在,是否覆盖? [y/n] (Does new.txt exist? Overwrite? [y/n]). Enter y to confirm or n to cancel, preventing accidental data loss.
5. Copy a Symbolic Link (Link Itself or Its Target)¶
A symbolic link (symlink) is a special file that “points to” another file, not the content itself. By default, cp copies the target file’s content. To copy the symlink itself (create a new symlink), use the -P parameter (no dereference).
Example: First, create a symlink link.txt pointing to source.txt:
ln -s source.txt link.txt
To copy the symlink itself to new_link (so new_link also points to source.txt), run:
cp -P link.txt new_link
Explanation: The -P flag prevents cp from following the symlink; it directly copies the symlink itself. Without -P, new_link would become a regular file with the same content as source.txt (not a symlink).
6. Copy and Rename Directly¶
To copy a file to a target location while renaming it, specify the new filename directly in the target path.
Example: Copy a.txt to the docs directory and rename it to memo.txt:
cp a.txt docs/memo.txt
Explanation: The target path docs/memo.txt includes the new filename memo.txt. After execution, docs will contain memo.txt with the same content as a.txt.
Summary¶
These advanced cp usages solve common daily file operations:
- Use *.txt docs/ to batch copy multiple files.
- Use -r to recursively copy entire directories (including subfolders).
- Use -a to preserve file attributes (permissions, timestamps, etc.).
- Use -i to avoid overwriting files accidentally.
- Use -P to handle symbolic links correctly.
While parameters may seem overwhelming at first, practice with test files, directories, and symlinks to master them quickly. For complex scenarios, consult man cp or cp --help for detailed parameter explanations.