一堕澄、列出目錄內(nèi)容
- 列出當前目錄的內(nèi)容或指定目錄
- 用法:
ls [options] [ files_or_dirs ]
ls -a 包含隱藏文件
ls -l 顯示額外的信息
ls -R 目錄遞歸通過
ls -ld 目錄和符號鏈接信息
ls -1 文件分行顯示
ls –S 按從大到小排序
ls –t 按mtime排序
ls –u 配合-t選項业舍,顯示并按atime從新到舊排序
ls –U 按目錄存放順序顯示
ls –X 按文件后綴排序
示例:
[root@centos7 ~]# touch Desktop/young //在Desktop文件夾下創(chuàng)建一個新的空文件
[root@centos7 ~]# ls -R //目錄遞歸顯示
.:
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates who.out
Desktop Downloads Music Public Videos
./Desktop:
young
./Documents:
./Downloads:
./Music:
./Pictures:
./Public:
./Templates:
./Videos:
[root@centos7 ~]#
[root@centos7 ~]# ls -l Desktop //顯示的是Desktop目錄下的文件的屬性
total 0
-rw-r--r--. 1 root root 0 Mar 10 21:29 young
[root@centos7 ~]# ls -ld Desktop //顯示Desktop本身的屬性
drwxr-xr-x. 2 root root 19 Mar 10 21:29 Desktop
[root@centos7 ~]# ls -Sl //按從大到小排序
total 12
-rw-r--r--. 1 root root 1953 Mar 6 21:24 initial-setup-ks.cfg
-rw-------. 1 root root 1922 Mar 6 20:08 anaconda-ks.cfg
-rw-r--r--. 1 root root 250 Mar 9 10:46 who.out
drwxr-xr-x. 2 root root 19 Mar 10 21:29 Desktop
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Documents
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Downloads
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Music
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Pictures
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Public
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Templates
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Videos
[root@centos7 ~]# ll -S //與上面命令作用相同
total 12
-rw-r--r--. 1 root root 1953 Mar 6 21:24 initial-setup-ks.cfg
-rw-------. 1 root root 1922 Mar 6 20:08 anaconda-ks.cfg
-rw-r--r--. 1 root root 250 Mar 9 10:46 who.out
drwxr-xr-x. 2 root root 19 Mar 10 21:29 Desktop
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Documents
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Downloads
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Music
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Pictures
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Public
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Templates
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Videos
[root@centos7 ~]# ls -1
anaconda-ks.cfg
Desktop
Documents
Downloads
initial-setup-ks.cfg
Music
Pictures
Public
Templates
Videos
who.out
二倦始、查看文件狀態(tài)
stat
- 文件:metadata, data
3個時間戳: - access time:訪問時間驰坊,atime谊迄,讀取文件內(nèi)容
- modify time: 修改時間, mtime惩琉,改變文件內(nèi)容(數(shù)據(jù))
- change time: 改變時間, ctime豆励,元數(shù)據(jù)發(fā)生改變
示例:
[root@centos7 ~]# chown young Music //改變Music文件夾的所有者,用chown命令
[root@centos7 ~]# ll
total 12
-rw-------. 1 root root 1922 Mar 6 20:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root 19 Mar 10 21:29 Desktop
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Documents
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Downloads
-rw-r--r--. 1 root root 1953 Mar 6 21:24 initial-setup-ks.cfg
drwxr-xr-x. 2 young root 6 Mar 6 22:34 Music
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Pictures
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Public
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Templates
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Videos
-rw-r--r--. 1 root root 250 Mar 9 10:46 who.out
[root@centos7 ~]# ll --time=ctime //查看元數(shù)據(jù)改變時間
total 12
-rw-------. 1 root root 1922 Mar 6 20:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root 19 Mar 10 21:29 Desktop
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Documents
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Downloads
-rw-r--r--. 1 root root 1953 Mar 6 21:24 initial-setup-ks.cfg
drwxr-xr-x. 2 young root 6 Mar 10 21:44 Music #時間與上面的時間不一樣了
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Pictures
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Public
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Templates
drwxr-xr-x. 2 root root 6 Mar 6 22:34 Videos
-rw-r--r--. 1 root root 250 Mar 9 10:46 who.out
[root@centos7 ~]# stat Music /三個時間狀態(tài)一起查看
File: ‘Music’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 802h/2050d Inode: 35968019 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 1000/ young) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-03-10 21:45:31.568590791 +0800
Modify: 2018-03-06 22:34:44.021897018 +0800
Change: 2018-03-10 21:44:29.518592258 +0800
Birth: -
PS:在網(wǎng)頁端上關掉用戶的atime-讀取時間操作會大大提升服務器的性能
三瞒渠、文件通配符
當在使用命令行時良蒸,有很多時間都用來查找你所需要的文件,如 ls,find等伍玖。shell提供了一系列完整的字符串模式匹配規(guī)則嫩痰,或者稱之為元字符,當shell遇到上述字符時窍箍,就會把它們當作特殊字符串纺,而不是文件名中的普通字符,這樣用戶就可以用它們來匹配相應的文件名椰棘,這可以稱為通配符造垛。
通配符與正則表達式是有區(qū)別的,簡單來說:通配符是用來通配的晰搀,正則表達式是用來匹配字符串的五辽;在文本過濾工具里,都是用正則表達式外恕,比如像awk杆逗,sed,等鳞疲,是針對文件的內(nèi)容的罪郊。而通配符多用在文件名上,比如查找find尚洽,ls悔橄,cp,等等腺毫。其次癣疟,shell對通配符與正則表達式的出了有不同,“ ”內(nèi)一般為通配符(是shell本身提取處理)潮酒,‘ ’內(nèi)一般為正則表達式(shell會將其中的數(shù)據(jù)傳遞給其它命令處理)睛挚。
* 匹配零個或多個字符
? 匹配任何單個字符
~ 當前用戶家目錄
~mage 用戶mage家目錄
~+ 當前工作目錄
~- 前一個工作目錄
[0-9] 匹配數(shù)字范圍
[a-z]:字母
[A-Z]:字母
[xxx] 匹配列表中的任何的一個字符
[!xxx] or [^xxx]匹配列表中的所有字符以外的字符
預定義的字符類:man 7 glob
[:digit:]:任意數(shù)字,相當于0-9
[:lower:]:任意小寫字母
[:upper:]: 任意大寫字母
[:alpha:]: 任意大小寫字母
[:alnum:]:任意數(shù)字或字母
[:blank:]:水平空白字符
[:space:]:水平或垂直空白字符
[:punct:]:標點符號
[:print:]:可打印字符
[:cntrl:]:控制(非打蛹崩琛)字符
[:graph:]:圖形字符
[:xdigit:]:十六進制字符
四扎狱、創(chuàng)建空文件 和 刷新時間
1侧到,touch命令:touch [OPTION]... FILE...
- -a 僅改變 atime和ctime
- -m 僅改變 mtime和ctime
- -t [[CC]YY]MMDDhhmm[.ss]
- 指定atime和mtime的時間戳
- -c 如果文件不存在,則不予創(chuàng)建
注:當文件已經(jīng)存在的情況下淤击,再touch一遍只會刷新時間
2匠抗,>>命令,與touch命令作用相同污抬,但是比touch要危險一些汞贸,在原文件已存在的情況下用>>再創(chuàng)建一邊會損壞原先的文件
五、復制文件和目錄cp
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
cp SRC DEST
- SRC是文件:
如果目標不存在:新建DEST壕吹,并將SRC中內(nèi)容填充至DEST中
如果目標存在:
如果DEST是文件:將SRC中的內(nèi)容覆蓋至DEST中,基于安,建議為cp命令使用-i選項
如果DEST是目錄:在DEST下新建與原文件同名的文件删铃,并將SRC中內(nèi)容填充至新文件中 - SRC是目錄:此時使用選項:
-r
如果DEST不存在:則創(chuàng)建指定目錄耳贬,復制SRC目錄中所有文件至DEST中;
如果DEST存在:如果DEST是文件:報錯猎唁;如果DEST是目錄:則復制
cp SRC... DEST
SRC...:多個文件
DEST必須存在咒劲,且為目錄,其它情形均會出錯
cp常用選項
-i:覆蓋前提示 –n:不覆蓋诫隅,注意兩者順序
-r, -R: 遞歸復制目錄及內(nèi)部的所有內(nèi)容
-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
-f: --force
-u:--update 只復制源比目標更新文件或目標不存在的文件
--backup=numbered 目標存在逐纬,覆蓋前先備份加數(shù)字后綴
六蛔屹、Inode索引節(jié)點
inode(index node)表中包含文件系統(tǒng)所有文件列表
一個節(jié)點 (索引節(jié)點)是在一個表項,包含有關文件的信息( 元數(shù)據(jù) )豁生,包
括:
- 文件類型兔毒,權限,UID甸箱,GID
- 鏈接數(shù)(指向這個文件名路徑名稱個數(shù))
- 該文件的大小和不同的時間戳
- 指向磁盤上文件的數(shù)據(jù)塊指針
- 有關文件的其他數(shù)據(jù)
- 文件引用一個是 inode號
- 人是通過文件名來引用一個文件育叁,一個目錄是目錄下的文件名和文件inode號之間的映射
[root@centos7 ~]# df //查看磁盤存儲空間
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 48803552 3407640 45395912 7% /
devtmpfs 747420 0 747420 0% /dev
tmpfs 762136 0 762136 0% /dev/shm
tmpfs 762136 9236 752900 2% /run
tmpfs 762136 0 762136 0% /sys/fs/cgroup
/dev/sda3 19520512 32944 19487568 1% /data
/dev/sda1 508580 154420 354160 31% /boot
tmpfs 152428 24 152404 1% /run/user/0
/dev/sr0 8490330 8490330 0 100% /run/media/root/CentOS 7 x86_64
[root@centos7 ~]# df -i //查看磁盤節(jié)點數(shù)量
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda2 24413696 120727 24292969 1% /
devtmpfs 186855 402 186453 1% /dev
tmpfs 190534 1 190533 1% /dev/shm
tmpfs 190534 585 189949 1% /run
tmpfs 190534 16 190518 1% /sys/fs/cgroup
/dev/sda3 9765376 14 9765362 1% /data
/dev/sda1 256000 327 255673 1% /boot
tmpfs 190534 17 190517 1% /run/user/0
/dev/sr0 0 0 0 - /run/media/root/CentOS 7 x86_64
[root@centos7 ~]#
1、cp和inode
在 CP的 命令:
分配一個空閑的inode號芍殖,在inode表中生成新條目
在目錄中創(chuàng)建一個目錄項豪嗽,將名稱與inode編號關聯(lián)
拷貝數(shù)據(jù)生成新的文件
2、rm和inode
rm 命令:
鏈接數(shù)遞減豌骏,從而釋放的inode號可以被重用
把數(shù)據(jù)塊放在空閑列表中
刪除目錄項
數(shù)據(jù)實際上不會馬上被刪除龟梦,但當另一個文件使用數(shù)據(jù)塊時將被覆蓋。
3窃躲、mv和inode
- 如果mv命令的目標和源在相同的文件系統(tǒng)变秦,作為mv 命令
用新的文件名創(chuàng)建對應新的目錄項
刪除舊目錄條目對應的舊的文件名
不影響inode表(除時間戳)或磁盤上的數(shù)據(jù)位置:沒有數(shù)據(jù)被移動! - 如果目標和源在一個不同的文件系統(tǒng)框舔, mv相當于cp和rm
- 如果mv命令的目標和源在相同的文件系統(tǒng)蹦玫,作為mv 命令
用新的文件名創(chuàng)建對應新的目錄項
刪除舊目錄條目對應的舊的文件名
不影響inode表(除時間戳)或磁盤上的數(shù)據(jù)位置:沒有數(shù)據(jù)被移動赎婚!
如果目標和源在一個不同的文件系統(tǒng), mv相當于cp和rm
七樱溉、硬鏈接和軟連接
1挣输,硬鏈接
創(chuàng)建硬鏈接會增加額外的記錄項以引用文件
對應于同一文件系統(tǒng)上一個物理文件
每個目錄引用相同的inode號
創(chuàng)建時鏈接數(shù)遞增
刪除文件時:rm 命令遞減計數(shù)的鏈接,文件要存在,至少有一個鏈接數(shù),當鏈接數(shù)為零時福贞,該文件被刪除,不能跨越驅動器或分區(qū).
語法: ln filename [linkname ]
2撩嚼,符號(或軟)鏈接
一個符號鏈接指向另一個文件
ls - l的 顯示鏈接的名稱和引用的文件
一個符號鏈接的內(nèi)容是它引用文件的名稱
可以對目錄進行
可以跨分區(qū)
指向的是另一個文件的路徑;其大小為指向的路徑字符串的長度挖帘;不增加或減
少目標文件inode的引用計數(shù)完丽;
語法:ln -s filename ? ? [linkname]
3,區(qū)別:
(1)跨分區(qū)
(2)inode number
(3)鏈接數(shù)
(4)目錄
(5)刪除