介绍
rm(copy)用来复制文件(或目录)到指定的路径,可同时复制多个文件和目录。
命令格式:
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file [target_file | target_directory]
第一种.将源文件的内容复制到目标文件中。
第二种.将每个指定的源文件的内容复制到目标目录中。文件名本身不会更改。
如果 cp
检测到尝试将文件复制到自身的情况,复制操作将失败。
常用选项:
-a, --archive
保留链接、文件属性,并复制目录下的所有内容。其作用等于 dpR 参数组合
-b
覆盖已存在的文件前将文件备份
-d
当复制符号连接时,把符号连接指向源文件或源目录
-f, --force
强制复制,覆盖已经存在的文件时不提示
-i, --interactive
在覆盖已经存在的文件时给出提示,要求用户确认是否覆盖
-l, --link
对源文件建立硬连接,而非复制文件
-n, --no-clobber
不覆盖已经存在的文件
-p
除复制文件的内容外,同时复制修改时间和访问权限等
-R, -r, --recursive
递归处理,将指定目录下的文件与子目录一并复制
-s, --symbolic-link
对源文件建立符号连接,而非复制文件
➜ linuxtest ls
d1 d2 test1.txt test2.txt
# 复制并重命名
➜ linuxtest cp test2.txt d2/test2new.txt
➜ linuxtest ls -l d2/
total 0
-rw-r--r-- 1 justx staff 0 4 9 17:27 test2new.txt
# 复制多个到指定目录
➜ linuxtest cp test1.txt test2.txt d2
➜ linuxtest ls -l d2/
total 0
-rw-r--r-- 1 justx staff 0 4 9 17:28 test1.txt
-rw-r--r-- 1 justx staff 0 4 9 17:28 test2.txt
-rw-r--r-- 1 justx staff 0 4 9 17:27 test2new.txt
# 递归复制文件夹到指定目录
➜ linuxtest cp -r d2 d1
➜ linuxtest ls -l d1
total 0
drwxr-xr-x 5 justx staff 160 4 9 17:28 d2
-rw-r--r-- 1 justx staff 0 4 9 17:25 test.txt
-rw-r--r-- 1 justx staff 0 4 9 17:26 test2.txt
# 显示提示信息
➜ linuxtest cp -i d2 d1
cp: d2 is a directory (not copied).
➜ linuxtest cp -ir d2 d1
overwrite d1/d2/test1.txt? (y/n [n]) y
overwrite d1/d2/test2.txt? (y/n [n]) y
overwrite d1/d2/test2new.txt? (y/n [n]) y
文章评论