【Linux】命令 - cp

Posted by 西维蜀黍 on 2021-02-11, Last Modified on 2022-12-10

复制文件

# Copy a file to another location:
$ cp path/to/source_file.ext path/to/target_file.ext

# Copy a file into another directory, keeping the filename:
$ cp path/to/source_file.ext path/to/target_parent_directory

复制文件夹

# Recursively copy a directory's contents to another location (if the destination exists, the directory is copied inside it):
$ cp -R path/to/source_directory path/to/target_directory

# Copy a directory recursively, in verbose mode (shows files as they are copied):
$ cp -vR path/to/source_directory path/to/target_directory

把文件夹递归(-R)拷贝到另一个文件夹 vs 把文件夹中的子文件(夹)拷贝到另一个文件夹:

# 把文件夹拷贝到另一个文件夹
$ cp -R ./wordpress ./sw_test/
$ playground tree
.
├── sw_test
│   └── wordpress
│       └── docker-compose.yml
└── wordpress
    └── docker-compose.yml

3 directories, 2 files

# 把文件夹中的子文件(夹)拷贝到另一个文件夹
# 等价于 cp -vR ./wordpress/* ./sw_test/
$ cp -R ./wordpress/ ./sw_test/
$ tree
.
├── sw_test
│   └── docker-compose.yml
└── wordpress
    └── docker-compose.yml

2 directories, 2 files

Reference