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.说明

各目录的容量和与.的总量一般是不相等的,因为只统计了目录。

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


猜你喜欢

转载自blog.51cto.com/5482173/2490762
今日推荐