標籤:ati ash div 連結 pre cal span cto 檔案的
最近學習Linux中的複製檔案命令cp,把學習記錄如下
cp [選項] source(來源檔案) destination(目的檔案)cp [選項] source1 source2 source3 source4 source5 ...directory
cp命令可以複製檔案或檔案夾,後面可以接多個源檔案;當有多個源檔案時,最後一個參數是要存放的目的目錄,若目的目錄不存在,不是目的目錄都會報錯。如下面所示:
[[email protected] ~]# ll總用量 8-rw-------. 1 root root 1585 4月 29 09:11 anaconda-ks.cfg-rw-r--r--. 1 root root 1633 4月 29 17:03 initial-setup-ks.cfg[[email protected] ~]# cp /etc/asound.conf /etc/bashrc anaconda-ks.cfg cp: 目標"anaconda-ks.cfg" 不是目錄[[email protected] ~]# cp /etc/asound.conf /etc/bashrc tmpcp: 目標"tmp" 不是目錄
下面來看看選項參數:-a:相當於-dr-d:若檔案是連結檔案,則會複製連結檔案而不是檔案本身-f:表示強制複製-l:進行連結的複製,不是文檔本身的複製-p;連同文檔的屬性(許可權,使用者、時間)一起複製過去,而不是使用複製時的預設的屬性-r:遞迴地進行複製,對檔案夾複製時使用的選項-s:複製成為符號連結-u:將源檔案與目的檔案進行對比,若有更新才進行複製
下面來看些例子:
例子一:把test複製為test_root
[[email protected] dream]# ll總用量 0-rw-rw-r--. 1 dream dream 0 5月 11 09:58 test-rw-rw-r--. 1 dream dream 0 5月 11 09:58 test.log[[email protected] dream]# cp test test_root[[email protected] dream]# ll總用量 0-rw-rw-r--. 1 dream dream 0 5月 11 09:58 test-rw-rw-r--. 1 dream dream 0 5月 11 09:58 test.log-rw-r--r--. 1 root root 0 5月 11 09:58 test_root
例子二:把etc目錄複寫到./tmp目錄下
[[email protected] dream]# cp -r /etc/ ./tmp/
例子三:把/etc/rc.local連結檔案複製到目前的目錄並保留連結,不複製檔案本身
[[email protected] dream]# cp /etc/rc.local ./[[email protected] dream]# ll總用量 4-rw-r--r--. 1 root root 473 5月 11 10:11 rc.local[[email protected] dream]# cp -d /etc/rc.local ./[[email protected] dream]# ll總用量 0lrwxrwxrwx. 1 root root 13 5月 11 10:13 rc.local -> rc.d/rc.local
看完上面幾個例子,可以看出,在進行複製檔案的時候,目的檔案的屬性會隨著當前命令的執行者發生變化;若需要保留原來的屬性,則可以使用-p選項參數
[[email protected] dream]# ll總用量 0-rw-rw-r--. 1 dream dream 0 5月 11 09:58 test[[email protected] dream]# cp -p test ./test.tmp[[email protected] dream]# ll總用量 0-rw-rw-r--. 1 dream dream 0 5月 11 09:58 test-rw-rw-r--. 1 dream dream 0 5月 11 09:58 test.tmp
總結:cp命令複製檔案時會與命令的執行者有關;當要複製多個檔案時,最後一個參數一定要是目錄
Linux每天學習一個命令之cp命令