Linux 操作離不開指令,而基礎指令用得最頻繁。本文用實際輸出示範 ls 與 cp 的各種行為。
ls [options] [files_or_dirs] -a 包含隱藏檔 -l 顯示額外資訊 -R 目錄遞廻 -ld 顯示目錄與符號連結本身 -1 分行顯示 -S 由大到小排序 -u 配合 -t,依 atime 由新到舊排序 -U 不排序,依目錄存放順序
[root@localhost /]# ls -a . .. .autorelabel bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test testdir tmp usr var
[root@localhost /]# ls -R /home /home: mageedu shui xiaoshui /home/mageedu: /home/shui: /home/xiaoshui: text /home/xiaoshui/text:
[root@localhost /]# ls -1 /home/ mageedu shui xiaoshui
[root@localhost /]# ls -Sl /root total 16 -rw------- 1 root root 5237 Jul 28 12:23 history.1 -rw-------. 1 root root 2633 Jul 25 20:42 anaconda-ks.cfg -rw------- 1 root root 119 Jul 26 14:28 history_log drwxr-xr-x 2 root root 6 Jul 27 15:11 test drwxrwxr-x 2 xiaoshui xiaoshui 6 Jul 27 15:15 text -rw-r--r-- 1 root root 0 Jul 26 17:32 1 -rw-r--r-- 1 root root 0 Jul 26 17:32 100 -rw-r--r-- 1 root root 0 Jul 26 17:32 12
[root@localhost /]# ls -ltu /root total 16 drwxr-xr-x 2 root root 6 Jul 28 20:03 test drwxrwxr-x 2 xiaoshui xiaoshui 6 Jul 28 20:03 text -rw------- 1 root root 5237 Jul 28 12:23 history.1 -rw-r--r-- 1 root root 0 Jul 27 09:13 1 -rw-r--r-- 1 root root 0 Jul 26 17:32 12 -rw-r--r-- 1 root root 0 Jul 26 17:32 100 -rw------- 1 root root 119 Jul 26 14:28 history_log -rw-------. 1 root root 2633 Jul 25 20:42 anaconda-ks.cfg
cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE...
目標不存在時,建立 DEST 並把 SOURCE 內容填入:
[root@localhost ~]# cp /etc/issue /root/xiaoshui [root@localhost ~]# ls 1 100 12 anaconda-ks.cfg history.1 history_log test text xiaoshui
目標存在且為檔案時,覆蓋並提示確認:
[root@localhost ~]# cat /root/xiaoshui hello,world [root@localhost ~]# cp /etc/issue /root/xiaoshui cp: overwrite ‘/root/xiaoshui’? y [root@localhost ~]# cat /root/xiaoshui \S Kernel \r on an \m Mage Education Learning Services https://www.magedu.com tty is \l hostname is \n current time is \t
目標存在且為目錄時,在該目錄下建立同名檔案:
[root@localhost ~]# cp /etc/issue /root/test/ [root@localhost ~]# ls -l /root/test/ total 4 -rw-r--r-- 1 root root 123 Jul 28 20:32 issue
此時 DEST 必須存在且為目錄,否則報錯:
[root@localhost ~]# cp /root/text/* /root/text1 cp: target ‘/root/text1’ is not a directory [root@localhost ~]# mkdir text1 [root@localhost ~]# cp /root/text/* /root/text1 [root@localhost ~]# ls text1/ f1 f2 f3 f4 f5
目標不存在時,建立該目錄並複製全部內容:
[root@localhost ~]# cp /root/test/ /root/newtest cp: omitting directory ‘/root/test/’ [root@localhost ~]# cp -r /root/test/ /root/newtest [root@localhost ~]# ls /root/newtest/ f1 f2 f3 f4 f5 issue
目標存在且為檔案時報錯;為目錄時複製成功:
[root@localhost ~]# cp -r /root/test/ history.1 cp: cannot overwrite non-directory ‘history.1’ with directory ‘/root/test/’ [root@localhost ~]# cp -r /root/test/ /tmp
-i 互動式
-r,-R 遞廻複製目錄及內部所有內容
-a 歸檔,等同 -dR --preserve=all
-d --no-dereference --preserve=links,只複製連結名稱
--preserve[=ATTR_LIST]:mode 權限、ownership 屬主屬組、timestamp 時間戳、
links、xattr、context、all
-p 等同 --preserve=mode,ownership,timestamp
-v 顯示細節
-f 強制
小結:cp 的行為完全取決於「來源是檔還是目錄」以及「目標存在與否、是檔還是目錄」,把這幾種組合記清楚,日常使用就不會出錯。