Linux-CP命令

CP命令:

用來將一個或多個源文件或者目錄複製到指定的目的文件或目錄。它能夠將單個源文件複製成一個指定文件名的具體的文件或一個已經存在的目錄下。cp命令還支持同時複製多個文件,當一次複製多個文件時,目標文件參數必須是一個已經存在的目錄,不然將出現錯誤

參數:

-a:此參數的效果和同時指定"-dpR"參數相同;
-d:當複製符號鏈接時,把目標文件或目錄也創建爲符號鏈接,並指向與源文件或目錄鏈接的原始文件或目錄;
-f:強行復制文件或目錄,不論目標文件或目錄是否已存在;
-i:覆蓋既有文件以前先詢問用戶;
-l:對源文件創建硬鏈接,而非複製文件;
-p:保留源文件或目錄的屬性;
-R/r:遞歸處理,將指定目錄下的全部文件與子目錄一併處理;
-s:對源文件創建符號鏈接,而非複製文件;
-u:使用這項參數後只會在源文件的更改時間較目標文件更新時或是名稱相互對應的目標文件並不存在時,才複製文件;
-S:在備份文件時,用指定的後綴「SUFFIX」代替文件的默認後綴;
-b:覆蓋已存在的文件目標前將目標文件備份;
-v:詳細顯示命令執行的操做。

實踐操做:

[root@centos7-02 /]# ls /data/
test1.txt  test2.txt  test3.txt  test4.txt
[root@centos7-02 /]# cp /data/test1.txt  /tmp/test/
[root@centos7-02 /]# tree /tmp/test/
/tmp/test/
└── test1.txt
0 directories, 1 file
[root@centos7-02 /]# cp /data/test1.txt  /tmp/test/
cp:是否覆蓋"/tmp/test/test1.txt"? n
[root@centos7-02 /]# \cp /data/test1.txt  /tmp/test/
[root@centos7-02 /]# cp /data/test1.txt  /tmp/test/
cp:是否覆蓋"/tmp/test/test1.txt"? n
[root@centos7-02 /]# which cp
alias cp='cp -i'
    /usr/bin/cp
[root@centos7-02 /]# /usr/bin/cp /data/test1.txt  /tmp/test/
[root@centos7-02 /]# 
[root@centos7-02 /]# cp -r /data/ /tmp/test/
[root@centos7-02 /]# tree tmp/test/
tmp/test/
├── data
│   ├── test1.txt
│   ├── test2.txt
│   ├── test3.txt
│   └── test4.txt
└── test1.txt
1 directory, 5 files
[root@centos7-02 /]# cp -r /data/ /tmp/test/
cp:是否覆蓋"/tmp/test/data/test1.txt"? n
cp:是否覆蓋"/tmp/test/data/test2.txt"? n
cp:是否覆蓋"/tmp/test/data/test3.txt"? n
cp:是否覆蓋"/tmp/test/data/test4.txt"? n
[root@centos7-02 /]# \cp -r /data/ /tmp/test/
[root@centos7-02 /]# tree /tmp/test/
/tmp/test/
├── data
│   ├── test1.txt
│   ├── test2.txt
│   ├── test3.txt
│   └── test4.txt
└── test1.txt
1 directory, 5 files
[root@centos7-02 /]#

特殊符號

Linux 系統中,「!」 符號或者操做符一般被用作邏輯否認的操做符,同時也經過一些調整和改動命令來從歷史記錄中找出你須要的命令行。

關於!幾個神奇操做用法:

1.從歷史記錄中使用命令號來運行命令:
例如:
[root@centos7-02 /]# history 
254  history 
255  ! $253
256  history 
257  ls /tmp/test/
258  history 
[root@centos7-02 /]# !257
ls /tmp/test/
data  test1.txt  test2.txt
[root@centos7-02 /]# 

2.執行指定的以前執行過的命令
例如:
246  tree /tmp/test/
247  cp /data/test2.txt  /tmp/test/
248  echo /data/test2.txt
249  echo /tmp/test/
250  /tmp/test/
251  ls /tmp/test/
252  cp /data/test2.txt  /tmp/test/
253  ls /tmp/test/
254  history 
255  ! $253
256  history 
257  ls /tmp/test/
258  history 
259  ls /tmp/test/
260  history 
261  ls /tmp/test/
262  his
263  history 
[root@centos7-02 /]# !-12
cp /data/test2.txt  /tmp/test/
cp:是否覆蓋"/tmp/test/test2.txt"? n
[root@centos7-02 /]# 

3.向一條新命令傳遞舊命令的參數避免重複輸入(取最後一個參數)
例如:
[root@centos7-02 /]# tree /tmp/test/
/tmp/test/
├── data
│   ├── test1.txt
│   ├── test2.txt
│   ├── test3.txt
│   └── test4.txt
├── test1.txt
└── test2.txt
1 directory, 6 files
[root@centos7-02 /]# ls !$
ls /tmp/test/
data  test1.txt  test2.txt
[root@centos7-02 /]#

[root@centos7-02 /]# ls /tmp/test/
data  test1.txt  test2.txt
[root@centos7-02 /]# tree !$
tree /tmp/test/
/tmp/test/
├── data
│   ├── test1.txt
│   ├── test2.txt
│   ├── test3.txt
│   └── test4.txt
├── test1.txt
└── test2.txt
1 directory, 6 files
[root@centos7-02 /]# cp /data/test4.txt /tmp/test/
[root@centos7-02 /]# ls !$
ls /tmp/test/
data  test1.txt  test2.txt  test4.txt
[root@centos7-02 /]#

4.用!處理兩個以上的參數:
[root@centos7-02 /]# cp /data/test4.txt  /tmp/test/
cp:是否覆蓋"/tmp/test/test4.txt"? y
[root@centos7-02 /]# ls !$
ls /tmp/test/
data  test1.txt  test2.txt  test4.txt
[root@centos7-02 /]# ls !cp:1
ls /data/test4.txt
/data/test4.txt
[root@centos7-02 /]# ls !cp:2
ls /tmp/test/
data  test1.txt  test2.txt  test4.txt
[root@centos7-02 /]# 

[root@centos7-02 /]# cp /data/test4.txt  /tmp/test/
cp:是否覆蓋"/tmp/test/test4.txt"? y
[root@centos7-02 /]# ls !^
ls /data/test4.txt
/data/test4.txt
[root@centos7-02 /]# cp /data/test4.txt  /tmp/test/
cp:是否覆蓋"/tmp/test/test4.txt"? y
[root@centos7-02 /]# ls !$
ls /tmp/test/
data  test1.txt  test2.txt  test4.txt
[root@centos7-02 /]# 

5.經過關鍵詞來執行以前的命令:
[root@centos7-02 /]# !ls
ls /tmp/test/
data  test1.txt  test2.txt  test4.txt
[root@centos7-02 /]# !cp
cp /data/test4.txt  /tmp/test/
cp:是否覆蓋"/tmp/test/test4.txt"? y
[root@centos7-02 /]#
相關文章
相關標籤/搜索