Linux command
cp 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Copy a file to a destination
cp [path/to/source.txt] [path/to/destination.txt]
Copy a directory recursively
cp -r [path/to/source_dir] [path/to/dest_dir]
Copy with interactive prompt before overwrite
cp -i [path/to/source] [path/to/dest]
Preserve mode, ownership, and timestamps
cp -p [path/to/source] [path/to/dest]
Archive copy (preserve all attributes, recursive)
cp -a [path/to/source_dir] [path/to/dest_dir]
Copy multiple files to a directory
cp [file1] [file2] [file3] [path/to/dest_dir/]
说明
cp copies files and directories. It can copy single files, multiple files to a directory, or entire directory trees. It preserves or modifies file attributes depending on options used. The command is a fundamental tool for file management on Unix systems.
参数
- -r, -R, --recursive
- Copy directories recursively
- -i, --interactive
- Prompt before overwrite
- -f, --force
- Force overwrite
- -n, --no-clobber
- Don't overwrite existing files
- -p, --preserve
- Preserve mode, ownership, timestamps
- -a, --archive
- Archive mode (same as -dR --preserve=all)
- -u, --update
- Copy only when source is newer
- -v, --verbose
- Verbose output
- -l, --link
- Hard link instead of copy
- -s, --symbolic-link
- Symbolic link instead of copy
- -t, --target-directory _DIR_
- Copy all sources into DIR
- -T, --no-target-directory
- Treat destination as a normal file
- --backup=_CONTROL_
- Backup existing files
- -d
- Same as --no-dereference --preserve=links
- --preserve=_ATTR_LIST_
- Preserve specified attributes (mode,ownership,timestamps,context,links,xattr,all)
FAQ
What is the cp command used for?
cp copies files and directories. It can copy single files, multiple files to a directory, or entire directory trees. It preserves or modifies file attributes depending on options used. The command is a fundamental tool for file management on Unix systems.
How do I run a basic cp example?
Run `cp [path/to/source.txt] [path/to/destination.txt]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -r, -R, --recursive do in cp?
Copy directories recursively