ll View the information meaning of each column of file properties , The difference and usage of hard and soft links , Application scenarios



Linux File properties and types


[[email protected] ~]# ll anaconda-ks.cfg
file type      jurisdiction        Number of hard connections           File size           File creation , Modification time
-         rw-------.  1    root root   1379          3 month   12 17:43 anaconda-ks.cfg
[[email protected] ~]# ll -d zls
file type    jurisdiction         Number of hard connections    Users    User group   File size      The creation and modification time of the file      
d        rwxr-xr-x.   2     root     root     6            3 month   12 18:00 
 
file name
zls
-: Ordinary documents
d: Directory file
rw- --- --- : jurisdiction
rwx r-x r-x : jurisdiction
r: read  read
w: Write  write
x: Executive authority   execute


Linux Types of system files


ll  see
file #  View file type
- Ordinary documents
d  directory  Directory file
c char  Character device file
b block  Block device file ( A device for storing data )
s socket, Secure socket file
l link  Soft link file ( Shortcut )
p pipe  Pipeline files
Be careful : Judging file type by color or suffix is not necessarily accurate .


System link file

A document is divided into two parts :

  • Real data  block
  • Metadata inode

  • block: The space occupied by real data , Default one block 4k, Use the following command to view

    df -h
  • inode: The space that metadata takes up , Describe file properties .

    df -i #  see inode usage
    ls -i #  see file inode


simulation inode Full scene


[[email protected] dev]# dd if=/dev/zero of=/opt/disk bs=1K count=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.00210524 s, 498 MB/s
[[email protected] dev]# ll /opt/disk 
-rw-r--r-- 1 root root 1048576 Mar 25 22:22 /opt/disk
[[email protected] dev]# mkfs.ext4 -i 1024 /opt/disk 
mke2fs 1.42.9 (28-Dec-2013)
/opt/disk is not a block special device.
Proceed anyway? (y,n) [[email protected] dev]# mkfs.ext4 -i 1024 /opt/disk
mke2fs 1.42.9 (28-Dec-2013)
/opt/disk is not a block special device.
Proceed anyway? (y,n) y
Filesystem too small for a journal
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
1024 inodes, 1024 blocks
51 blocks (4.98%) reserved for the super user
First data block=1
Maximum filesystem blocks=1048576
1 block group
8192 blocks per group, 8192 fragments per group
1024 inodes per group
Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
[[email protected] dev]# mkdir /data1
[[email protected] dev]# mount -t ext4 -o loop /opt/disk /data1
[[email protected] dev]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        28G  1.5G   27G   6% /
devtmpfs        476M     0  476M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M  7.6M  479M   2% /run
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs            98M     0   98M   0% /run/user/0
/dev/sr0        4.3G  4.3G     0 100% /mnt
/dev/loop0      891K   21K  799K   3% /data1
[[email protected] dev]# df -i
Filesystem       Inodes IUsed    IFree IUse% Mounted on
/dev/sda3      14423552 31519 14392033    1% /
devtmpfs         121779   379   121400    1% /dev
tmpfs            124487     1   124486    1% /dev/shm
tmpfs            124487   704   123783    1% /run
tmpfs            124487    16   124471    1% /sys/fs/cgroup
/dev/sda1        256000   326   255674    1% /boot
tmpfs            124487     1   124486    1% /run/user/0
/dev/sr0              0     0        0     - /mnt
/dev/loop0         1024    11     1013    2% /data1
[[email protected] dev]# cd /data1/
#  Try to create 1024 Files to 1014 You can't create it when you're in the middle of it .
[[email protected] data1]# touch file{1..1024}
touch: cannot touch ‘file1014’: No space left on device
touch: cannot touch ‘file1015’: No space left on device
touch: cannot touch ‘file1016’: No space left on device
touch: cannot touch ‘file1017’: No space left on device
touch: cannot touch ‘file1018’: No space left on device
touch: cannot touch ‘file1019’: No space left on device
touch: cannot touch ‘file1020’: No space left on device
touch: cannot touch ‘file1021’: No space left on device
touch: cannot touch ‘file1022’: No space left on device
touch: cannot touch ‘file1023’: No space left on device
touch: cannot touch ‘file1024’: No space left on device
#  Disk space is not full
[[email protected] data1]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        28G  1.5G   27G   6% /
devtmpfs        476M     0  476M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M  7.6M  479M   2% /run
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs            98M     0   98M   0% /run/user/0
/dev/sr0        4.3G  4.3G     0 100% /mnt
/dev/loop0      891K   46K  774K   6% /data1
# inode It's already %100
[[email protected] data1]# df -i
Filesystem       Inodes IUsed    IFree IUse% Mounted on
/dev/sda3      14423552 31519 14392033    1% /
devtmpfs         121779   379   121400    1% /dev
tmpfs            124487     1   124486    1% /dev/shm
tmpfs            124487   704   123783    1% /run
tmpfs            124487    16   124471    1% /sys/fs/cgroup
/dev/sda1        256000   326   255674    1% /boot
tmpfs            124487     1   124486    1% /run/user/0
/dev/sr0              0     0        0     - /mnt
/dev/loop0         1024  1024        0  100% /data1
[[email protected] data1]#


Soft link


How it was created :

ln -s  File or directory   Link name 

Soft links are equivalent to windows Shortcut to , The soft link file will inode Pointing to the source file block

1. So when we create multiple soft links to a file , In fact, there are many inode Point to the same block.

[[email protected] opt]# ln -s /opt/a.txt a1_softl
[[email protected] opt]# ln -s /opt/a.txt a2_softl
[[email protected] opt]# ln -s /opt/a.txt a3_softl
[[email protected] opt]# ll a*_so*
lrwxrwxrwx 1 root root 10 Mar 25 22:32 a1_softl -> /opt/a.txt
lrwxrwxrwx 1 root root 10 Mar 25 22:32 a2_softl -> /opt/a.txt
lrwxrwxrwx 1 root root 10 Mar 25 22:32 a3_softl -> /opt/a.txt
[[email protected] opt]# ll -i a1_softl a2_softl a3_softl a.txt
16782001 lrwxrwxrwx 1 root root 10 Mar 25 22:32 a1_softl -> /opt/a.txt
16782002 lrwxrwxrwx 1 root root 10 Mar 25 22:32 a2_softl -> /opt/a.txt
16782003 lrwxrwxrwx 1 root root 10 Mar 25 22:32 a3_softl -> /opt/a.txt
17174724 -rw-r--r-- 1 root root 13 Mar 23 16:51 a.txt
[[email protected] opt]# cat a1_softl 
aaabbbccc
[[email protected] opt]# cat a2_softl 
aaabbbccc
[[email protected] opt]# cat a3_softl 
aaabbbccc
[[email protected] opt]#

2. So when we delete the soft link file , It's just a deletion inode The direction of , Does not affect the source file .

[[email protected] opt]# rm a1_softl 
rm: remove symbolic link ‘a1_softl’? y
[[email protected] opt]# rm a2_softl 
rm: remove symbolic link ‘a2_softl’? y
[[email protected] opt]# cat a.txt 
aaabbbccc
[[email protected] opt]#

3. If we delete the source file , Then all the soft link files of this file will be invalid .

[[email protected] opt]# ll
total 264
lrwxrwxrwx 1 root root      10 Mar 25 22:32 a3_softl -> /opt/a.txt
[[email protected] opt]# rm -rf a.txt 
[[email protected] opt]# ll a3*
lrwxrwxrwx 1 root root 10 Mar 25 22:32 a3_softl -> /opt/a.txt #  Turn red 


Application scenarios of soft link


1) Software upgrade
2) Code release
3) An inconvenient Directory

4) Data rollback
5) Program read

Hard links


1) Hard link files and metadata files inode The numbers are the same and point to unity block

[[email protected] opt]# touch a.txt
[[email protected] opt]# ln a.txt a_hardl
[[email protected] opt]# ll -i
total 0
16782001 -rw-r--r-- 2 root root 0 Mar 25 22:47 a_hardl
16782001 -rw-r--r-- 2 root root 0 Mar 25 22:47 a.txt

2) Deleting the source file has no effect on the hard connection , Hard connection has the function of backup

[[email protected] opt]# echo 123tt >a_hardl 
[[email protected] opt]# ll
total 8
-rw-r--r-- 2 root root 6 Mar 25 22:49 a_hardl
-rw-r--r-- 2 root root 6 Mar 25 22:49 a.txt
[[email protected] opt]# cat a_hardl 
123tt
[[email protected] opt]# cat a.txt 
123tt
[[email protected] opt]# echo bbb >> a.txt 
[[email protected] opt]# cat a.txt 
123tt
bbb
[[email protected] opt]# cat a_hardl 
123tt
bbb
[[email protected] opt]# rm -fr a.txt 
[[email protected] opt]# cat a_hardl 
123tt
bbb

3) Directory cannot create hard link

[[email protected] opt]# ln /opt/ /aaa
ln: ‘/opt/’: hard link not allowed for directory


The difference between hard link and soft link


# 1.  The creation command is different
Hard links : ln
Soft link : ln -s
# 2.  Directory cannot create hard link , You can create soft links , Hard links cannot cross system partitions , Soft links can
[[email protected] ~]# ln /opt/a_hardl /boot/
ln: failed to create hard link ‘/boot/a_hardl’ => ‘/opt/a_hardl’: Invalid cross-device link
# 3.  Hard link file and source file inode Same number , Soft links are different
# 4.  Soft link if you delete the source file , All soft link files are invalid , Hard link if you delete the source file , Other hard link files are available