Finish the hard chain and soft chain in 10 minutes

The content arrangement of soft chain and hard chain is to prepare for the content of subsequent pnpm.

Example:

cd /Users/ligang/Documents/github/practice/links/sources
# Create corresponding test files in the test directory
touch test.js && echo "console.log('links')" > test.js

inode

View file information (inode)

ls -li test.js
8643193659 -rw-r--r--  1 ligang  staff  21  8 30 17:08 test.js

inode value

File type permissions

Link count

File owner

File Groups

size

modification date

name

8643193659

-rw-r–r--

1

ligang

staff

21

8 30 17:08

test.js

Inode is the "inode", which stores the meta information of the file. It is a data structure in UNIX operating system, which contains some important information related to each file in the file system.

hard links

Create hard chain

ln ./sources/test.js test-hard-links.js

View file information (inode)

ls -li test-hard-links.js
8643193659 -rw-r--r--  2 ligang  staff  21  8 30 17:08 test-hard-links.js

It has the same inode value as the original file and points to a block of the physical hard disk.

Report to test hard links JS additional content

echo "console.log('test hard links')" >> test-hard-links.js
cat sources/test.js

console.log('links')
console.log('test hard links')

Original file test JS synchronously updated the content.

Delete the original file test js

rm sources/test.js
cat test-hard-links.js

console.log('links')
console.log('test hard links')

Files that pass through the hard chain are not affected.

Report to test hard links again JS write content (the original file has been deleted)

echo "console.log('test hard links again')" >> test-hard-links.js
ll sources/test.js

ls: sources/test.js: No such file or directory

Only test hard links JS write, the original file has been deleted and will not be processed

soft/symbolic links

Create soft chain

ln -s sources/test.js test-soft-links.js

View file information (inode)

ls -li test-soft-links.js
8643223807 lrwxr-xr-x  1 ligang  staff  15  8 31 13:27 test-soft-links.js -> sources/test.js

Its inode value is different from that of the original file.

Report to test soft links JS additional content

echo "console.log('soft hard links')" >> test-soft-links.js
cat sources/test.js

console.log('links')
console.log('soft hard links')

Original file test JS synchronously updated the content.

Delete the original file test js

rm sources/test.js
cat test-soft-links.js

cat: test-soft-links.js: No such file or directory

report errors! There is no corresponding file or directory.

Report to test soft links again JS write content (the original file has been deleted)

echo "console.log('test soft links again')" >> test-soft-links.js
ll sources/test.js

-rw-r--r--  1 ligang  staff    37B  8 31 13:35 sources/test.js

cat sources/test.js
cat test-soft-links.js

console.log('test soft links again')

Only test hard links JS, the original file is recreated, and test soft links JS and sources / test JS content has become the latest modified content (consistent with sources/test.js).

difference

Hard chain creates aliases for file content; The soft chain creates an alias for the file name.

hard links

symbolic links

Execute command

ln

ln -s

inode

Same as the original (is an additional name of the original document)

Different from the original (alias of the original file)

The original file was deleted

Still valid

invalid

file system

Original file systems only (cannot cross file systems)

Can span different file systems

Link directory

I won't support it

support

Creating a hard link actually creates a new entry for the resources of the original file in the memory, so the hard link and the original file actually point to the same resource (the same inode) in the memory; while the soft link establishes a point, that is, the content in the linked file is a pointer to the original file, and they are two files.

Supplement - view hard chain files

ln sources/test.js test-hard-links.js
ll -li sources/test.js

8643224333 -rw-r--r--  2 ligang  staff    37B  8 31 13:36 sources/test.js

According to the above description, the link count is 2. How to view it?

Method 1: find the same inode

find . -inum 8643224333

./test-hard-links.js
./sources/test.js

Method 2: find the same filename

find . -samefile ./sources/test.js

./test-hard-links.js
./sources/test.js

above. For the current directory, you can expand or narrow the search range. If global lookup is required, it can be set to /.

Added by arcticwolf on Wed, 05 Jan 2022 13:58:29 +0200