Linux-文件系統管理-df-du-ln

4.文件系統

本章同步視頻:https://edu.51cto.com/sd/e4874

4.5 文件系統管理

4.5.1 df - report file system disk space usage

1.語法

df [OPTION]... [FILE]...

-h, --human-readable   print sizes in human readable format (e.g., 1K 234M 2G)

-H, --si    likewise, but use powers of 1000 not 1024

-i, --inodes     list inode information instead of block usage

2.用法

[root@localhost ~]# df

Filesystem            1K-blocks    Used Available Use% Mounted on

/dev/mapper/rhel-root  10229760 4194692   6035068  42% /

/dev/mapper/rhel-home   2037760   33032   2004728   2% /home

/dev/sda1                508588  121208    387380  24% /boot

[root@localhost ~]# df -h       #以可讀的方式顯示,即GB

Filesystem             Size  Used Avail Use% Mounted on

/dev/mapper/rhel-root  9.8G  4.1G  5.8G  42% /

/dev/mapper/rhel-home  2.0G   33M  2.0G   2% /home

/dev/sda1              497M  119M  379M  24% /boot

[root@localhost ~]# df -H      #以1000換算

Filesystem             Size  Used Avail Use% Mounted on

/dev/mapper/rhel-root   11G  4.3G  6.2G  42% /

/dev/mapper/rhel-home  2.1G   34M  2.1G   2% /home

/dev/sda1              521M  125M  397M  24% /boot

[root@localhost ~]# df -i      #顯示inode使用情況

Filesystem              Inodes  IUsed    IFree IUse% Mounted on

/dev/mapper/rhel-root 10240000 152278 10087722    2% /

/dev/mapper/rhel-home  2048000     31  2047969    1% /home

/dev/sda1               512000    328   511672    1% /boot

[root@localhost ~]# df -h /dev/sda1    #查看某一特定文件系統

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda1       497M  119M  379M  24% /boot

4.5.2 du - estimate file space usage

1.語法

du [OPTION]... [FILE]...

2.用法

[root@localhost tmp]# du     #查看當前目錄容量

0 ./dir

24 .

[root@localhost tmp]# du --inode    #查看當前目錄的inode使用情況

4 ./dir

21 .

[root@localhost tmp]# du -s    #顯示當前目錄的總量

24 .

[root@localhost tmp]# du -s /etc/      #顯示/etc目錄的總量

33680 /etc/

3.說明

各目錄的容量和與.的總量一般是不相等的,因爲只統計了目錄。

本章同步視頻:https://edu.51cto.com/sd/e4874

4.5.3 ln - make links between files

1.語法

ln [OPTION]... [-T] TARGET LINK_NAME

-s  :如果不加任何參數就進行連結,那就是hard link,至於 -s 就是symbolic link

-f  :如果 目標檔 存在時,就主動的將目標檔直接移除後再建立!

2.Hard Link

(1)Hard Link

[root@localhost tmp]# ls     #查看當前文件

dir  mbr  mbr.l  passwd  root  test

[root@localhost tmp]# ln mbr mbr.l     #建立硬鏈接

[root@localhost tmp]# ll -i mbr*      #查看源文件和硬鏈接

7562 -rw-r--r--. 2 root root 512 Mar 18 21:57 mbr

7562 -rw-r--r--. 2 root root 512 Mar 18 21:57 mbr.l

#注:注意inode號。

(2)硬鏈接注意事項

l  源文件被刪除,hard link仍然可用。

[root@localhost tmp]# rm -f mbr    #刪除源文件

[root@localhost tmp]# ll mbr*

-rw-r--r--. 1 root root 512 Mar 18 21:57 mbr.l

lrwxrwxrwx. 1 root root   3 Mar 20 12:23 mbr.s -> mbr

[root@localhost tmp]# cat mbr.l    #硬鏈接仍可打開

l  不能跨 Filesystem;

l  不能 link 目錄。

[root@localhost tmp]# ln dir dir.l

ln: ‘dir’: hard link not allowed for directory

3.Symbolic Link

(1)Symbolic Link

[root@localhost tmp]# ln -s mbr mbr.s

[root@localhost tmp]# ll -i mbr*

7562 -rw-r--r--. 2 root root 512 Mar 18 21:57 mbr

7562 -rw-r--r--. 2 root root 512 Mar 18 21:57 mbr.l

7595 lrwxrwxrwx. 1 root root   3 Mar 20 12:23 mbr.s -> mbr

(2)軟連接注意事項

l  當來源檔被刪除之後,symbolic link 就會失效。

[root@localhost tmp]# rm -f mbr   #刪除源文件

[root@localhost tmp]# ll mbr*

-rw-r--r--. 1 root root 512 Mar 18 21:57 mbr.l

lrwxrwxrwx. 1 root root   3 Mar 20 12:23 mbr.s -> mbr

[root@localhost tmp]# cat mbr.s    #軟連接無法打開

cat: mbr.s: No such file or directory

l  Symbolic Link 與 Windows 的快捷方式可以給他劃上等號。

4.軟連接與硬鏈接對比

                                              image.png

6.鏈接數

(1)文件的鏈接數

[root@localhost tmp]# touch hard

[root@localhost tmp]# ll hard

-rw-r--r--. 1 root root 0 Mar 20 12:42 hard

#創建一個文件,其鏈接數是1。

[root@localhost tmp]# ln -s hard hard.s

[root@localhost tmp]# ll hard

-rw-r--r--. 1 root root 0 Mar 20 12:42 hard

#建立一個軟連接,其鏈接數不增加

[root@localhost tmp]# ln hard hard.l

[root@localhost tmp]# ll hard*

-rw-r--r--. 2 root root 0 Mar 20 12:42 hard

-rw-r--r--. 2 root root 0 Mar 20 12:42 hard.l

lrwxrwxrwx. 1 root root 4 Mar 20 12:42 hard.s -> hard

#建立一個硬鏈接,源文件和硬鏈接的鏈接數+1,軟鏈接的鏈接數不增加。

#結論:文件的鏈接數是其硬鏈接的數量+1

(2)目錄的鏈接數

[root@localhost tmp]# mkdir dir

[root@localhost tmp]# ll -d dir

drwxr-xr-x. 2 root root 6 Mar 20 12:36 dir

[root@localhost tmp]# ll -a dir

total 4

drwxr-xr-x. 2 root root    6 Mar 20 12:36 .

drwxrwxrwt. 9 root root 4096 Mar 20 12:36 ..

#創建一個新目錄,它的鏈接數是2,因爲它下面有.和..

[root@localhost tmp]# mkdir dir/dir1

[root@localhost tmp]# ll -d dir

drwxr-xr-x. 3 root root 17 Mar 20 12:38 dir

#創建一個子目錄,父目錄的鏈接數+1

[root@localhost tmp]# touch dir/aaa

[root@localhost tmp]# ll -d dir/

drwxr-xr-x. 3 root root 27 Mar 20 12:39 dir/

#創建一個子文件,父目錄鏈接數不會增加

[root@localhost tmp]# mkdir dir/dir1/dir2

[root@localhost tmp]# ll -d dir/

drwxr-xr-x. 3 root root 27 Mar 20 12:39 dir/

#創建一個“孫”目錄,“爺”目錄的鏈接數不會增加,但父目錄的鏈接數會+1。

#結論:目錄的鏈接數是其子目錄數+2

本章同步視頻:https://edu.51cto.com/sd/e4874


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章