天天看點

linux ls cp結合使用,Linux基礎系列4(ls,cp指令詳解)

Linux系統操作離不開指令,基礎指令更顯得尤為重要,下面幾個讓我們來一一嘗試

ls:列出目前目錄的内容或指定目錄

用法:ls [options] [files_or_dirs]

ls -a包含隐藏檔案

ls -l顯示額外的資訊

ls -R目錄遞歸通過

ls -ld目錄和符号連結資訊

ls -1 檔案分行顯示

ls �S 按從大到小排序

ls �u 配合-t選項,顯示并按atime從新到舊排序

ls �U 不排序按目錄存放順序顯示

ls -a 列出目錄下的所有檔案或目錄包括隐藏檔案

[[email protected] /]# ls -a

.             bin   etc   lib64  opt   run   sys      tmp

..            boot  home  media  proc  sbin  test     usr

.autorelabel  dev   lib   mnt    root  srv   testdir  var

ls -R

[[email protected] /]# ls -R /home

/home:

mageedu  shui  xiaoshui

/home/mageedu:

/home/shui:

/home/xiaoshui:

text

/home/xiaoshui/text:

ls -1

[[email protected] /]# ls -1 /home/

mageedu

shui

xiaoshui

ls -S 按照所占記憶體從大到小進行排序

[[email protected] /]# 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

ls -tu 按照atime(通路時間進行排序)

[[email protected] /]# 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指令,copy即複制的意思,強大且常用的一條指令,總結一下,可以長來看看

cp [OPTION]... [-T] SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... -t DIRECTORY SOURCE...

SOURCE是檔案:

如果目标不存在:建立DEST,并将SOURCE中内容填充至DEST中

[[email protected] ~]# ls

1  100  12  anaconda-ks.cfg  history.1  history_log  test  text

[[email protected] ~]# cp /etc/issue /root/xiaoshui

[[email protected] ~]# ls

1  100  12  anaconda-ks.cfg  history.1  history_log  test  text  xiaoshui

DEST中

如果目标存在:

如果DEST是檔案:将SOURCE中的内容覆寫至DEST中:

[[email protected] ~]# cat /root/xiaoshui

hello,world

[[email protected] ~]# cp /etc/issue /root/xiaoshui

cp: overwrite ‘/root/xiaoshui’? y

[[email protected] ~]# cat /root/xiaoshui

\S

Kernel \r on an \m

Mage Education Learning Services

http://www.magedu.com

tty is \l

hostname is \n

current time is \t

如果DEST是目錄:在DEST下建立與原檔案同名的檔案,并将SOURCE中内容填充至新檔案中:

[[email protected] ~]# ls -l /root/test/

total 0

[[email protected] ~]# cp /etc/issue /root/test/

[[email protected] ~]# ls -l /root/test/

total 4

-rw-r--r-- 1 root root 123 Jul 28 20:32 issue

cp SOURCE... DEST

SOURCE...:多個檔案

DEST必須存在,且為目錄,其它情形均會出錯;

[[email protected] ~]# ls /root/text/

f1  f2  f3  f4  f5

[[email protected] ~]# cp /root/text/* /root/text1

cp: target ‘/root/text1’ is not a directory

[[email protected] ~]# cp /root/text/* /root/test/issue

cp: target ‘/root/test/issue’ is not a directory

[[email protected] ~]# mkdir text1

[[email protected] ~]# cp /root/text/* /root/text1

[[email protected] ~]# ls text1/

f1  f2  f3  f4  f5

如上所示,當SOURCE為多個檔案時候,DEST不存在時或者為一個檔案時都會報錯,建立一個新的text1新的目錄,cp成功

cp SOURCE DEST

SOURCE是目錄:此時使用選項:-r

如果DEST不存在:則建立指定目錄,複制SRC目錄中所有檔案至DEST中;

[[email protected] ~]# ls

1  100  12  anaconda-ks.cfg  history.1  history_log  test  text  text1  xiaoshui

[[email protected] ~]# ls test

f1  f2  f3  f4  f5  issue

[[email protected] ~]# cp /root/test/ /root/newtest

cp: omitting directory ‘/root/test/’

[[email protected] ~]# cp -r /root/test/ /root/newtest

[[email protected] ~]# ls

1  100  12  anaconda-ks.cfg  history.1  history_log  newtest  test  text  text1  xiaoshui

[[email protected] ~]# ls /root/newtest/

f1  f2  f3  f4  f5  issue

如果DEST存在:

如果DEST是檔案:報錯

[[email protected] ~]# ls

1  100  12  anaconda-ks.cfg  history.1  history_log  newtest  test  text  text1  xiaoshui

[[email protected] ~]# cp -r /root/test/ history.1

cp: cannot overwrite non-directory ‘history.1’ with directory ‘/root/test/’

如果DEST是目錄:複制成功

[[email protected] ~]# cp -r /root/test/  /tmp

[[email protected] ~]# ls /tmp

backup2016-07-28  fstab     ks-script-q9VyUw  nihao      systemd-private-0c68803c30a44dc89ab2a6d7cabf159a-cups.service-Jvhybl  test  yum.log

error.txt         issue.sh  ks-script-SYqw2e  right.txt  systemd-private-ac493ffd025a47b69ca2ea90b081675f-cups.service-uthsZ2  ts

-i:互動式

-r, -R: 遞歸複制目錄及内部的所有内容;

-a: 歸檔,相當于-dR--preserv=all

-d:--no-dereference --preserv=links 不複制原檔案,隻複制連結名

--preserv[=ATTR_LIST]

mode: 權限

ownership: 屬主屬組

timestamp:

links

xattr

context

all

-p: 等同--preserv=mode,ownership,timestamp

-v: --verbose

在root使用者下系統已經定義别名cp=cp -i,可以使用\cp使用指令。在普通使用者下cp即cp

[[email protected] test]# alias

alias cp='cp -i'

[[email protected] test]# cp ../1 /root/test/

cp: overwrite ‘/root/test/1’? y

[[email protected] test]#

[[email protected] ~]$ cp f1 ./text/f1

[[email protected] ~]$ cp f1 ./text/f1

[[email protected] ~]$ cp  -i f1 ./text/f1

cp: overwrite ‘./text/f1’? y

[[email protected] ~]$

-f  普通使用者家目錄中出現root的檔案時,cp如果不加-f,是無法覆寫目标檔案issue的,會報錯。如果加上-f ,沒有問題,不過這不是覆寫,而是将目标檔案删除,在重新建立一個名稱相同的檔案,如下所示。

[[email protected] ~]$ ls -l

total 4

-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:10 f1

-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:11 f2

-rw-r--r-- 1 root     root     123 Jul 29 09:56 issue

drwxrwxr-x 2 xiaoshui xiaoshui  24 Jul 28 21:14 text

drwxrwxr-x 2 xiaoshui xiaoshui  15 Jul 28 21:12 text2

[[email protected] ~]$ cp /etc/fstab /home/xiaoshui/issue

cp: cannot create regular file ‘/home/xiaoshui/issue’: Permission denied

[[email protected] ~]$ cp -f /etc/fstab /home/xiaoshui/issue

[[email protected] ~]$ ls

f1  f2  issue  text  text2

[[email protected] ~]$ ls -l

total 4

-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:10 f1

-rw-rw-r-- 1 xiaoshui xiaoshui   0 Jul 28 21:11 f2

-rw-r--r-- 1 xiaoshui xiaoshui 595 Jul 29 09:57 issue

drwxrwxr-x 2 xiaoshui xiaoshui  24 Jul 28 21:14 text

drwxrwxr-x 2 xiaoshui xiaoshui  15 Jul 28 21:12 text2

-r, -R: 遞歸複制目錄及内部的所有内容,當複制的内容存在目錄時,此時需要加上-r,R選項

[[email protected] ~]$ cp /home/xiaoshui/ /tmp/

cp: omitting directory ‘/home/xiaoshui/’

[[email protected] ~]$ cp -R /home/xiaoshui/ /tmp

[[email protected] ~]$ ls /tmp/

backup2016-07-28  f2     issue.sh          right.txt                                                             test

clear             fstab  ks-script-q9VyUw  systemd-private-0c68803c30a44dc89ab2a6d7cabf159a-cups.service-Jvhybl  ts

error.txt         home   ks-script-SYqw2e  systemd-private-ac493ffd025a47b69ca2ea90b081675f-cups.service-uthsZ2  xiaoshui

f1                issue  nihao             systemd-private-ef1ab9dc007d41718d97c88fdff754b8-cups.service-McXBL9  yum.log

-d:--no-dereference --preserv=links 不複制原檔案,隻複制連結名,如果不加-d,則複制的内容是軟連結指向的原檔案,檔案名稱為軟連結的名字,如下所示:

不加-d前

[[email protected] ~]# ls -l  /etc/redhat-release

lrwxrwxrwx. 1 root root 14 Jul 25 20:07 /etc/redhat-release -> centos-release

[[email protected] ~]# cp /etc/redhat-release  newtest/

[[email protected] ~]# ls -l newtest/

total 4

-rw-r--r-- 1 root root 38 Jul 29 10:34 redhat-release

加上-d後

lrwxrwxrwx. 1 root root 14 Jul 25 20:07 /etc/redhat-release -> centos-release

[[email protected] ~]# cp -d /etc/redhat-release new2

[[email protected] ~]# ls new2

redhat-release

[[email protected] ~]# ls -l new2

total 0

lrwxrwxrwx 1 root root 14 Jul 29 10:38 redhat-release -> centos-release

[[email protected] ~]#

--preserv[=ATTR_LIST]

mode: 權限

ownership: 屬主屬組

timestamp:

links

xattr

context

all

目前隻學到ownership和timestamp,是以後面的幾個參數先不管,--preserv=ownership是指保持屬主屬組的,timestamp是保持其時間戳的,若想都保留,中間使用“,”隔開

如果不加--perserv,發現屬組屬主和時間戳都改變了

[[email protected] ~]# ls /home/xiaoshui/f1 -l

-rw-rw-r-- 1 xiaoshui xiaoshui 0 Jul 29 10:53 /home/xiaoshui/f1

[[email protected] ~]# cp /home/xiaoshui/f1 new2/

[[email protected] new2]# ls -l

total 0

-rw-r--r-- 1 root root  0 Jul 29 10:56 f1

加上--preserv=ownership,timestamp,發現屬主屬組和時間戳都保留下來了

[[email protected] new2]# ls -l

total 0

-rw-rw-r-- 1 xiaoshui xiaoshui  0 Jul 29 10:53 f1

-p: 等同--preserv=mode,ownership,timestamp

-v 詳細資訊

多總結多練習,才能達到熟練掌握并應用的程度。