每日一個linux命令07-cp

1. 命令解析

命令用途

復制文件或目錄至目標位置,或復制一組文件或目錄至目標位置

命令格式

復制文件或目錄至指定位置 cp [OPTION]... [-T] SOURCE DEST
復制一組文件或目錄至指定位置: cp [OPTION]... SOURCE... DIRECTORY
復制一組文件或目錄至指定位置: cp [OPTION]... -t DIRECTORY SOURCE...

命令參數(shù)

-a, --archive same as -dR --preserve=all
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents copy contents of special files when recursive
-d same as --no-dereference --preserve=links
-f, --force if an existing destination file cannot be
opened, remove it and try again (redundant if
the -n option is used)
-i, --interactive prompt before overwrite (overrides a previous -n
option)
-H follow command-line symbolic links in SOURCE
-l, --link link files instead of copying
-L, --dereference always follow symbolic links in SOURCE
-n, --no-clobber do not overwrite an existing file (overrides
a previous -i option)
-P, --no-dereference never follow symbolic links in SOURCE

-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST] preserve the specified attributes (default:
mode,ownership,timestamps), if possible
additional attributes: context, links, xattr,
all
-c same as --preserve=context
--no-preserve=ATTR_LIST don't preserve the specified attributes
--parents use full source file name under DIRECTORY
-R, -r, --recursive copy directories recursively
--reflink[=WHEN] control clone/CoW copies. See below.
--remove-destination remove each existing destination file before
attempting to open it (contrast with --force)
--sparse=WHEN control creation of sparse files. See below.
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-s, --symbolic-link make symbolic links instead of copying
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-x, --one-file-system stay on this file system
-Z, --context=CONTEXT set security context of copy to CONTEXT
--help display this help and exit
--version output version information and exit

2. 示例

2.1 不帶參數(shù)的復制文件

[root@fanshifeng cpTest]# ls
f1  f2  f2.~1~  f2.~2~  f3  f4
[root@fanshifeng cpTest]# cp f1 f5
[root@fanshifeng cpTest]# ls
f1  f2  f2.~1~  f2.~2~  f3  f4  f5
[root@fanshifeng cpTest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 11 19:39 f1
-rw-r--r-- 1 root root 0 Apr 11 19:46 f2
-rw-r--r-- 1 root root 0 Apr 11 19:39 f2.~1~
-rw-r--r-- 1 root root 0 Apr 11 19:46 f2.~2~
-rw-r--r-- 1 root root 0 Apr 11 19:40 f3
-rw-r--r-- 1 root root 0 Apr 11 19:39 f4
-rw-r--r-- 1 root root 0 Apr 11 19:53 f5

可以看到,復制的文件的屬性(創(chuàng)建時間)發(fā)生了變化

2.2 同時復制多個文件

[root@fanshifeng cpTest]# mkdir d1
[root@fanshifeng cpTest]# ls
d1  f1  f2  f2.~1~  f2.~2~  f3  f4  f5
[root@fanshifeng cpTest]# cp f1 f2 f3 f4 f5 d1
[root@fanshifeng cpTest]# tree d1
d1
├── f1
├── f2
├── f3
├── f4
└── f5

0 directories, 5 files

2.3 復制一個目錄

[root@fanshifeng cpTest]# ls
d1  f1  f2  f2.~1~  f2.~2~  f3  f4  f5
[root@fanshifeng cpTest]# cp -r d1 d2
[root@fanshifeng cpTest]# ls
d1  d2  f1  f2  f2.~1~  f2.~2~  f3  f4  f5
[root@fanshifeng cpTest]# tree d2
d2
├── f1
├── f2
├── f3
├── f4
└── f5

0 directories, 5 files

2.4 復制目錄或文件,且保留其歸檔信息(mode,ownership,timestamps) -a

[root@fanshifeng cpTest]# ls
d1  d2  d3  f1  f2  f2.~1~  f2.~2~  f3  f4  f5
[root@fanshifeng cpTest]# ll
total 12
drwxr-xr-x 2 root root 4096 Apr 11 19:55 d1
-rw-r--r-- 1 root root    0 Apr 11 19:39 f1
[root@fanshifeng cpTest]# cp -a f1 f6
[root@fanshifeng cpTest]# cp -a d1 d6
[root@fanshifeng cpTest]# ll
total 16
drwxr-xr-x 2 root root 4096 Apr 11 19:55 d1
drwxr-xr-x 2 root root 4096 Apr 11 19:55 d6
-rw-r--r-- 1 root root    0 Apr 11 19:39 f1
-rw-r--r-- 1 root root    0 Apr 11 19:39 f6

2.5 創(chuàng)建文件的硬鏈接(快捷方式)而不是復制它們 -l

[root@fanshifeng cpTest]# cp -l f1 d7 -v
`f1' -> `d7/f1'
[root@fanshifeng cpTest]# ls -lvi
total 16
1321179 drwxr-xr-x 2 root root 4096 Apr 11 19:55 d1
1321176 drwxr-xr-x 2 root root 4096 Apr 11 19:56 d2
1321191 drwxr-xr-x 2 root root 4096 Apr 11 20:07 d7
1321172 -rw-r--r-- 2 root root    6 Apr 11 20:05 f1
1321197 -rw-r--r-- 1 root root    0 Apr 11 19:39 f6
[root@fanshifeng cpTest]# ls -lvi d7
total 4
1321172 -rw-r--r-- 2 root root 6 Apr 11 20:05 f1

可以看到:原文件的inode為1321172,目標目錄的文件的inode也是1321172

2.6 創(chuàng)建文件的軟鏈接 -s

[root@fanshifeng cpTest]# cp f2 -s d7 -v
`f2' -> `d7/f2'
cp: `d7/f2': can make relative symbolic links only in current directory
[root@fanshifeng cpTest]# cd d7
[root@fanshifeng d7]# cp -s -v ../f1 f1.soft
`../f1' -> `f1.soft'

軟鏈接只能在當前目錄下創(chuàng)建

2.7 只復制軟鏈接(不復制文件本身) -P

[root@fanshifeng d7]# cp -v -P f1.soft f3.soft
`f1.soft' -> `f3.soft'
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:05 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:18 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1

2.8 僅當原文件比較新時拷貝 -u

[root@fanshifeng d7]# cp -u -v f1 f2
[root@fanshifeng d7]# ls
f1  f1.soft  f2  f3.soft
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:05 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:18 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# touch f1
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:18 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# cp -u -v f1 f2;
cp: overwrite `f2'? y
`f1' -> `f2'
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:49 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1

2.9 賦值前刪除目標文件(若存在) --remove-destination

[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:49 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# cp -v -remove-destination f1 f2;
cp: invalid option -- 'e'
Try `cp --help' for more information.
[root@fanshifeng d7]# cp -v --remove-destination f1 f2;
cp: overwrite `f2'? y
removed `f2'
`f1' -> `f2'
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 21:01 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1

2.10 復制文件僅當目標文件不存在時 -n

[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 21:01 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# cp -v -n f1 f2
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 21:01 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 說明本次redis集群安裝在rhel6.8 64位機器上,redis版本為3.2.8,redis的gem文件版本為...
    讀或?qū)?/span>閱讀 15,642評論 3 9
  • 1、Linux上的文件管理命令有哪些以及常用方法shell:shell負責接收用戶輸入的命令并進行解釋,將需要執(zhí)行...
    乀koala閱讀 902評論 0 3
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • 96女 喜文藝,喜歐美,喜拍照,愿識你?
    Y_鹿九少女閱讀 363評論 0 0
  • #玩卡不卡·每日一抽# 每一位都可以通過這張卡片覺察自己: 1、直覺他叫什么名字? 花 2、他幾歲了? 53歲 3...
    文俊zh閱讀 167評論 0 0

友情鏈接更多精彩內(nèi)容