[LINUX] Hard links and symbolic links

Notes on studying hard links and symbolic links

Environment: CentOS7

Terms that appear

--i node (index node) Data for managing files (can a corresponding inode be created when creating a file?) The size, date and time, location of data on the disc, etc. are written

--i node number Number to identify the inode

--Hard link Mechanism for linking multiple file names to one inode

--Symbolic link Mechanism for assigning aliases to files

Hard link

Create a file and a hard link for that file ↓

# echo hello > text  //"hello"Create a file named text

# ln text text.hard  //test hard link in test file.Created with the name hard

# ls
text  text.hard  //text and text.Confirm that hard is made

Check the contents with the cat command ↓

# cat text  //Check the contents of the text file
hello

# cat text.hard  //text.Check the contents of the hard file
hello

If you check the inode number with ls -i, you can see that the same inode number is associated ↓

# ls -i 
1331910 text  1331910 text.hard

Checking the file attributes with the stat command is exactly the same ↓

# stat text
  File: 'text'
  Size: 6         	Blocks: 8          IO Block: 4096   regular file
Device: 6dh/109d	Inode: 1331910     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-15 02:59:31.305456000 +0000
Modify: 2020-11-15 02:57:30.713465000 +0000
Change: 2020-11-15 02:58:14.329159000 +0000
 Birth: -

# stat text.hard
  File: 'text.hard'
  Size: 6         	Blocks: 8          IO Block: 4096   regular file
Device: 6dh/109d	Inode: 1331910     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-15 02:59:31.305456000 +0000
Modify: 2020-11-15 02:57:30.713465000 +0000
Change: 2020-11-15 02:58:14.329159000 +0000
 Birth: -

There is no problem even if you delete the original file with a hard link ↓

# ls
text  text.hard  //Make sure you have two files

# rm text
rm: remove regular file 'text'? y  //Delete the original file text

# ls      
text.hard  //Confirm that text has been deleted

# cat text.hard  //text.Check inside hard
hello

Symbolic link

Create a file and a symbolic link for that file ↓

# echo hello2 > text2  //"hello"Create a file named text2 with

# ln -s text2 text.sym  //test the symbolic link of the test file.Created with the name sym

# ls
text.sym  text2  //text and text.Confirm that sym is created

Check the contents with the cat command ↓

# cat text2
hello2

# cat text.sym 
hello2

If you check the inode number with ls -i, you can see that different inode numbers are associated with each other ↓

# ls -i 
1331913 text.sym  1331912 text2

Checking the file attributes with the stat command shows different results ↓

# stat text2
  File: 'text2'
  Size: 7         	Blocks: 8          IO Block: 4096   regular file
Device: 6dh/109d	Inode: 1331912     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-15 05:29:34.474558000 +0000
Modify: 2020-11-15 05:26:48.352110000 +0000
Change: 2020-11-15 05:26:48.352110000 +0000
 Birth: -

# stat text.sym
  File: 'text.sym' -> 'text2'
  Size: 5         	Blocks: 0          IO Block: 4096   symbolic link
Device: 6dh/109d	Inode: 1331913     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-15 05:27:06.504110000 +0000
Modify: 2020-11-15 05:27:04.357110000 +0000
Change: 2020-11-15 05:27:04.357110000 +0000
 Birth: -

If the original file disappears with a symbolic link, it will not work ↓

# ls
text.sym  text2  //Make sure you have two files

# rm text2
rm: remove regular file 'text2'? y  //Delete the original file text2

# ls
text.sym   //Confirm that text2 has been deleted

# cat text.sym 
cat: text.sym: No such file or directory   //text.When I try to check inside hard, it says that there are no files


//If you look at the stat command, you can see that the file was missing when you searched for the deleted text2 "File": 'text.sym' -> 'text2'」
# stat text.sym
  File: 'text.sym' -> 'text2'
  Size: 5         	Blocks: 0          IO Block: 4096   symbolic link
Device: 6dh/109d	Inode: 1331913     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-15 05:27:06.504110000 +0000
Modify: 2020-11-15 05:27:04.357110000 +0000
Change: 2020-11-15 05:27:04.357110000 +0000
 Birth: -

Recommended Posts

Hard links and symbolic links
Differences between symbolic links and hard links
About symbolic links
I investigated the behavior of the difference between hard links and symbolic links
Notes playing with symbolic links
What is the difference between a symbolic link and a hard link?
How to create / delete symbolic links
Personal notes and links about machine learning ① (Machine learning)