復(fù)制文件或目錄
語法
cp [選項(xiàng)] 源文件 目的
cp [選項(xiàng)] 源目錄 目的
選項(xiàng)
-b,--backup 覆蓋已存在的文件時(shí)先備份-f,--force 強(qiáng)行復(fù)制文件或目錄,不管目標(biāo)文件或目錄是否已經(jīng)存在-i,--interactive 覆蓋已存在的文件之前先詢問-p,--preserve 保留源文件或目錄的屬性-r,-R,--recursive 遞歸處理目錄以及目錄下所有的項(xiàng)目-u,--update 當(dāng)源文件比目的文件較新或目的文件不存在時(shí)才復(fù)制-v,--verbose 顯示命令執(zhí)行的信息
示例
cp a.txt doc復(fù)制 a.txt 到 doc 目錄
lychie@ubuntu:/test$ cp a.txt doc
lychie@ubuntu:/test$ tree doc
doc
└── a.txt
0 directories, 1 file
cp a.txt doc/b.txt復(fù)制 a.txt 到 doc 目錄,并重命名為 b.txt
lychie@ubuntu:/test$ cp a.txt doc/b.txt
lychie@ubuntu:/test$ tree doc
doc
├── a.txt
└── b.txt
0 directories, 2 files
cp -b a.txt doc復(fù)制 a.txt 到 doc 目錄,覆蓋之前先備份
lychie@ubuntu:/test$ cp -b a.txt doc
lychie@ubuntu:/test$ tree doc
doc
├── a.txt
├── a.txt~
└── b.txt
0 directories, 3 files
cp -r doc dir復(fù)制 doc 目錄,并重命名為 dir
lychie@ubuntu:/test$ ls
a.txt doc
lychie@ubuntu:/test$ cp -r doc dir
lychie@ubuntu:/test$ ls
a.txt dir doc
lychie@ubuntu:/test$ tree dir
dir
├── a.txt
├── a.txt~
└── b.txt
0 directories, 3 files
cp -p a.txt b.txt復(fù)制 a.txt 并重命名為 b.txt 同時(shí)保留源文件的屬性
lychie@ubuntu:/test$ ls -l
總用量 4
-rw-rw-r-- 1 lychie lychie 13 9月 24 02:00 a.txt
lychie@ubuntu:/test$ cp -p a.txt b.txt
lychie@ubuntu:/test$ cp a.txt c.txt
lychie@ubuntu:/test$ ls -l
總用量 12
-rw-rw-r-- 1 lychie lychie 13 9月 24 02:00 a.txt
-rw-rw-r-- 1 lychie lychie 13 9月 24 02:00 b.txt
-rw-rw-r-- 1 lychie lychie 13 9月 24 03:24 c.txt
cp a.txt b.txt category復(fù)制 a.txt、b.txt 到目錄 category
lychie@ubuntu:/test$ cp a.txt b.txt category
lychie@ubuntu:/test$ tree category
category
├── a.txt
└── b.txt
0 directories, 2 files