Linux文件管理基礎(chǔ)

一、列出目錄內(nèi)容

  • 列出當(dāng)前目錄的內(nèi)容或指定目錄
  • 用法:ls [options] [ files_or_dirs ]

ls -a 包含隱藏文件

ls -l 顯示額外的信息

ls -R 目錄遞歸通過

ls -ld 目錄和符號(hào)鏈接信息

ls -1 文件分行顯示

ls –S 按從大到小排序

ls –t 按mtime排序

ls –u 配合-t選項(xiàng),顯示并按atime從新到舊排序

ls –U 按目錄存放順序顯示

ls –X 按文件后綴排序

示例:

[root@centos7 ~]# touch Desktop/young  //在Desktop文件夾下創(chuàng)建一個(gè)新的空文件
[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個(gè)時(shí)間戳:
  • access time:訪問時(shí)間,atime,讀取文件內(nèi)容
  • modify time: 修改時(shí)間, mtime,改變文件內(nèi)容(數(shù)據(jù))
  • change time: 改變時(shí)間, 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ù)改變時(shí)間
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   #時(shí)間與上面的時(shí)間不一樣了
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    /三個(gè)時(shí)間狀態(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)頁(yè)端上關(guān)掉用戶的atime-讀取時(shí)間操作會(huì)大大提升服務(wù)器的性能

三、文件通配符

   當(dāng)在使用命令行時(shí),有很多時(shí)間都用來查找你所需要的文件,如 ls,find等。shell提供了一系列完整的字符串模式匹配規(guī)則,或者稱之為元字符,當(dāng)shell遇到上述字符時(shí),就會(huì)把它們當(dāng)作特殊字符,而不是文件名中的普通字符,這樣用戶就可以用它們來匹配相應(yīng)的文件名,這可以稱為通配符。

通配符與正則表達(dá)式是有區(qū)別的,簡(jiǎn)單來說:通配符是用來通配的,正則表達(dá)式是用來匹配字符串的;在文本過濾工具里,都是用正則表達(dá)式,比如像awk,sed,等,是針對(duì)文件的內(nèi)容的。而通配符多用在文件名上,比如查找find,ls,cp,等等。其次,shell對(duì)通配符與正則表達(dá)式的出了有不同,“ ”內(nèi)一般為通配符(是shell本身提取處理),‘ ’內(nèi)一般為正則表達(dá)式(shell會(huì)將其中的數(shù)據(jù)傳遞給其它命令處理)。

* 匹配零個(gè)或多個(gè)字符
? 匹配任何單個(gè)字符
~ 當(dāng)前用戶家目錄
~mage 用戶mage家目錄
~+ 當(dāng)前工作目錄
~- 前一個(gè)工作目錄
[0-9] 匹配數(shù)字范圍
[a-z]:字母
[A-Z]:字母
[xxx] 匹配列表中的任何的一個(gè)字符
[!xxx]  or  [^xxx]匹配列表中的所有字符以外的字符
預(yù)定義的字符類:man 7 glob
[:digit:]:任意數(shù)字,相當(dāng)于0-9
[:lower:]:任意小寫字母
[:upper:]: 任意大寫字母
[:alpha:]: 任意大小寫字母
[:alnum:]:任意數(shù)字或字母
[:blank:]:水平空白字符
[:space:]:水平或垂直空白字符
[:punct:]:標(biāo)點(diǎn)符號(hào)
[:print:]:可打印字符
[:cntrl:]:控制(非打?。┳址?[:graph:]:圖形字符
[:xdigit:]:十六進(jìn)制字符

四、創(chuàng)建空文件 和 刷新時(shí)間

1,touch命令:touch [OPTION]... FILE...

  • -a 僅改變 atime和ctime
  • -m 僅改變 mtime和ctime
  • -t [[CC]YY]MMDDhhmm[.ss]
  • 指定atime和mtime的時(shí)間戳
  • -c 如果文件不存在,則不予創(chuàng)建
    注:當(dāng)文件已經(jīng)存在的情況下,再touch一遍只會(huì)刷新時(shí)間

2,>>命令,與touch命令作用相同,但是比touch要危險(xiǎn)一些,在原文件已存在的情況下用>>再創(chuàng)建一邊會(huì)損壞原先的文件

五、復(fù)制文件和目錄cp

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

cp SRC DEST

  • SRC是文件:
    如果目標(biāo)不存在:新建DEST,并將SRC中內(nèi)容填充至DEST中
    如果目標(biāo)存在:
    如果DEST是文件:將SRC中的內(nèi)容覆蓋至DEST中,基于安,建議為cp命令使用-i選項(xiàng)
    如果DEST是目錄:在DEST下新建與原文件同名的文件,并將SRC中內(nèi)容填充至新文件中
  • SRC是目錄:此時(shí)使用選項(xiàng):-r
    如果DEST不存在:則創(chuàng)建指定目錄,復(fù)制SRC目錄中所有文件至DEST中;
    如果DEST存在:如果DEST是文件:報(bào)錯(cuò);如果DEST是目錄:則復(fù)制

cp SRC... DEST

SRC...:多個(gè)文件

DEST必須存在,且為目錄,其它情形均會(huì)出錯(cuò)

cp常用選項(xiàng)

-i:覆蓋前提示 –n:不覆蓋,注意兩者順序

-r, -R: 遞歸復(fù)制目錄及內(nèi)部的所有內(nèi)容

-a: 歸檔,相當(dāng)于-dR --preserv=all

-d:--no-dereference --preserv=links 不復(fù)制原文件,只復(fù)制鏈接名

--preserv[=ATTR_LIST]
           mode: 權(quán)限
           ownership: 屬主屬組
           timestamp:
           links
           xattr
           context
           all

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

-v: --verbose

-f: --force

-u:--update 只復(fù)制源比目標(biāo)更新文件或目標(biāo)不存在的文件

--backup=numbered 目標(biāo)存在,覆蓋前先備份加數(shù)字后綴

六、Inode索引節(jié)點(diǎn)

inode(index node)表中包含文件系統(tǒng)所有文件列表
一個(gè)節(jié)點(diǎn) (索引節(jié)點(diǎn))是在一個(gè)表項(xiàng),包含有關(guān)文件的信息( 元數(shù)據(jù) ),包
括:

  • 文件類型,權(quán)限,UID,GID
  • 鏈接數(shù)(指向這個(gè)文件名路徑名稱個(gè)數(shù))
  • 該文件的大小和不同的時(shí)間戳
  • 指向磁盤上文件的數(shù)據(jù)塊指針
  • 有關(guān)文件的其他數(shù)據(jù)
  • 文件引用一個(gè)是 inode號(hào)
  • 人是通過文件名來引用一個(gè)文件,一個(gè)目錄是目錄下的文件名和文件inode號(hào)之間的映射
[root@centos7 ~]# df  //查看磁盤存儲(chǔ)空間
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é)點(diǎn)數(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的 命令:
分配一個(gè)空閑的inode號(hào),在inode表中生成新條目
在目錄中創(chuàng)建一個(gè)目錄項(xiàng),將名稱與inode編號(hào)關(guān)聯(lián)
拷貝數(shù)據(jù)生成新的文件

2、rm和inode

rm 命令:
鏈接數(shù)遞減,從而釋放的inode號(hào)可以被重用
把數(shù)據(jù)塊放在空閑列表中
刪除目錄項(xiàng)
數(shù)據(jù)實(shí)際上不會(huì)馬上被刪除,但當(dāng)另一個(gè)文件使用數(shù)據(jù)塊時(shí)將被覆蓋。

3、mv和inode

  • 如果mv命令的目標(biāo)和源在相同的文件系統(tǒng),作為mv 命令
    用新的文件名創(chuàng)建對(duì)應(yīng)新的目錄項(xiàng)
    刪除舊目錄條目對(duì)應(yīng)的舊的文件名
    不影響inode表(除時(shí)間戳)或磁盤上的數(shù)據(jù)位置:沒有數(shù)據(jù)被移動(dòng)!
  • 如果目標(biāo)和源在一個(gè)不同的文件系統(tǒng), mv相當(dāng)于cp和rm
  • 如果mv命令的目標(biāo)和源在相同的文件系統(tǒng),作為mv 命令
    用新的文件名創(chuàng)建對(duì)應(yīng)新的目錄項(xiàng)
    刪除舊目錄條目對(duì)應(yīng)的舊的文件名
    不影響inode表(除時(shí)間戳)或磁盤上的數(shù)據(jù)位置:沒有數(shù)據(jù)被移動(dòng)!
    如果目標(biāo)和源在一個(gè)不同的文件系統(tǒng), mv相當(dāng)于cp和rm

七、硬鏈接和軟連接

1,硬鏈接

創(chuàng)建硬鏈接會(huì)增加額外的記錄項(xiàng)以引用文件

對(duì)應(yīng)于同一文件系統(tǒng)上一個(gè)物理文件

每個(gè)目錄引用相同的inode號(hào)

創(chuàng)建時(shí)鏈接數(shù)遞增

刪除文件時(shí):rm 命令遞減計(jì)數(shù)的鏈接,文件要存在,至少有一個(gè)鏈接數(shù),當(dāng)鏈接數(shù)為零時(shí),該文件被刪除,不能跨越驅(qū)動(dòng)器或分區(qū).

語法: ln filename [linkname ]

2,符號(hào)(或軟)鏈接

一個(gè)符號(hào)鏈接指向另一個(gè)文件

ls - l的 顯示鏈接的名稱和引用的文件

一個(gè)符號(hào)鏈接的內(nèi)容是它引用文件的名稱

可以對(duì)目錄進(jìn)行

可以跨分區(qū)

指向的是另一個(gè)文件的路徑;其大小為指向的路徑字符串的長(zhǎng)度;不增加或減
少目標(biāo)文件inode的引用計(jì)數(shù);

語法:ln -s filename ? ? [linkname]

3,區(qū)別:

   (1)跨分區(qū)
   (2)inode number
   (3)鏈接數(shù)
   (4)目錄
    (5)刪除
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢(mèng)閱讀 100,671評(píng)論 9 468
  • 前言: linux文件系統(tǒng)是一個(gè)倒置樹結(jié)構(gòu),從"/"開始,文件命名區(qū)分大小寫,遵循FHS(Filesystem H...
    哈嘍別樣閱讀 246評(píng)論 0 0
  • 一個(gè)基本的計(jì)算機(jī)系統(tǒng)由“硬件”和“軟件”組成,一臺(tái)Linux設(shè)備,主要的組成如下圖所示: 一般情況下,我們所說的L...
    時(shí)待吾閱讀 1,795評(píng)論 0 16
  • 從母親的子宮里哭著爬出來看到的笑臉都是空洞的啟蒙四周都是絕壁沒有一個(gè)支點(diǎn)架設(shè)一座橋梁到不了另外一座孤島尋找或者自成...
    靜聽旁白閱讀 258評(píng)論 0 3
  • 我和這個(gè)世界不熟。這并非是我安靜的原因。我依舊有很多問題,問南方問故里,問希望,問距離。 我和這個(gè)世界不熟。這并非...
    Alice王志榮閱讀 267評(píng)論 0 0

友情鏈接更多精彩內(nèi)容