Linux每天學習一個命令之cp命令

來源:互聯網
上載者:User

標籤:ati   ash   div   連結   pre   cal   span   cto   檔案的   

最近學習Linux中的複製檔案命令cp,把學習記錄如下
cp [選項] source(來源檔案) destination(目的檔案)cp [選項] source1 source2 source3 source4 source5 ...directory
cp命令可以複製檔案或檔案夾,後面可以接多個源檔案;當有多個源檔案時,最後一個參數是要存放的目的目錄,若目的目錄不存在,不是目的目錄都會報錯。如下面所示:
  1. [[email protected] ~]# ll
  2. 總用量 8
  3. -rw-------. 1 root root 1585 4月 29 09:11 anaconda-ks.cfg
  4. -rw-r--r--. 1 root root 1633 4月 29 17:03 initial-setup-ks.cfg
  5. [[email protected] ~]# cp /etc/asound.conf /etc/bashrc anaconda-ks.cfg
  6. cp: 目標"anaconda-ks.cfg" 不是目錄
  7. [[email protected] ~]# cp /etc/asound.conf /etc/bashrc tmp
  8. cp: 目標"tmp" 不是目錄

下面來看看選項參數:-a:相當於-dr-d:若檔案是連結檔案,則會複製連結檔案而不是檔案本身-f:表示強制複製-l:進行連結的複製,不是文檔本身的複製-p;連同文檔的屬性(許可權,使用者、時間)一起複製過去,而不是使用複製時的預設的屬性-r:遞迴地進行複製,對檔案夾複製時使用的選項-s:複製成為符號連結-u:將源檔案與目的檔案進行對比,若有更新才進行複製
下面來看些例子:
例子一:把test複製為test_root
  1. [[email protected] dream]# ll
  2. 總用量 0
  3. -rw-rw-r--. 1 dream dream 0 5月 11 09:58 test
  4. -rw-rw-r--. 1 dream dream 0 5月 11 09:58 test.log
  5. [[email protected] dream]# cp test test_root
  6. [[email protected] dream]# ll
  7. 總用量 0
  8. -rw-rw-r--. 1 dream dream 0 5月 11 09:58 test
  9. -rw-rw-r--. 1 dream dream 0 5月 11 09:58 test.log
  10. -rw-r--r--. 1 root root 0 5月 11 09:58 test_root

例子二:把etc目錄複寫到./tmp目錄下
  1. [[email protected] dream]# cp -r /etc/ ./tmp/

例子三:把/etc/rc.local連結檔案複製到目前的目錄並保留連結,不複製檔案本身
  1. [[email protected] dream]# cp /etc/rc.local ./
  2. [[email protected] dream]# ll
  3. 總用量 4
  4. -rw-r--r--. 1 root root 473 5月 11 10:11 rc.local
  5. [[email protected] dream]# cp -d /etc/rc.local ./
  6. [[email protected] dream]# ll
  7. 總用量 0
  8. lrwxrwxrwx. 1 root root 13 5月 11 10:13 rc.local -> rc.d/rc.local

看完上面幾個例子,可以看出,在進行複製檔案的時候,目的檔案的屬性會隨著當前命令的執行者發生變化;若需要保留原來的屬性,則可以使用-p選項參數
  1. [[email protected] dream]# ll
  2. 總用量 0
  3. -rw-rw-r--. 1 dream dream 0 5月 11 09:58 test
  4. [[email protected] dream]# cp -p test ./test.tmp
  5. [[email protected] dream]# ll
  6. 總用量 0
  7. -rw-rw-r--. 1 dream dream 0 5月 11 09:58 test
  8. -rw-rw-r--. 1 dream dream 0 5月 11 09:58 test.tmp

總結:cp命令複製檔案時會與命令的執行者有關;當要複製多個檔案時,最後一個參數一定要是目錄

Linux每天學習一個命令之cp命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.