Linux common basic commands (soft link and hard link)

Hard link: it is equivalent to multiple entries of the file. Its function is to back up the file, create a snapshot, etc

Soft link: a shortcut equivalent to windows

Command format:

ln option source file destination file

-s: Create soft link

1. Create a hard link:

ghostwu@dev:~/linux/cp$ ls
ghostwu.txt
ghostwu@dev:~/linux/cp$ ls -l
total 4
-rw-rw-r-- 1 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ ln ghostwu.txt ghostwu_hardlink
ghostwu@dev:~/linux/cp$ ls -l
total 8
-rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
-rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt

The hard link is the same as the inode node of the source file because it points to the same node:

ghostwu@dev:~/linux/cp$ ls -ihl
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt

Can view content

ghostwu@dev:~/linux/cp$ cat ghostwu.txt 
hello,my name is ghostwu
ghostwu@dev:~/linux/cp$ cat ghostwu_hardlink 
hello,my name is ghostwu

Delete the hard link without affecting the source file

ghostwu@dev:~/linux/cp$ rm ghostwu_hardlink 
ghostwu@dev:~/linux/cp$ ls -ilh
total 4.0K
9569451 -rw-rw-r-- 1 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ cat ghostwu.txt 
hello,my name is ghostwu

Restore the hard link, the same as before deleting the inode

ghostwu@dev:~/linux/cp$ ls -ilh
total 4.0K
9569451 -rw-rw-r-- 1 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ ln ghostwu.txt ghostwu_hardlink
ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt

Delete source files without affecting hard links

ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ rm ghostwu.txt 
ghostwu@dev:~/linux/cp$ ls -ilh
total 4.0K
9569451 -rw-rw-r-- 1 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
ghostwu@dev:~/linux/cp$ cat ghostwu_hardlink 
hello,my name is ghostwu

Source files can be recovered through hard links

ghostwu@dev:~/linux/cp$ ln ghostwu_hardlink ghostwu.txt
ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt

Create soft link

ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ ln -s ghostwu.txt ghostwu_softlink
ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569453 lrwxrwxrwx 1 ghostwu ghostwu 11 5 month   6 20:01 ghostwu_softlink -> ghostwu.txt
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt

The i node of the soft link is different from the source file. The file type is l

ghostwu@dev:~/linux/cp$ cat ghostwu_softlink 
hello,my name is ghostwu
ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569453 lrwxrwxrwx 1 ghostwu ghostwu 11 5 month   6 20:01 ghostwu_softlink -> ghostwu.txt
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt

Deleting soft links does not affect hard links and source files

ghostwu@dev:~/linux/cp$ rm ghostwu_softlink 
ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ cat ghostwu.txt 
hello,my name is ghostwu
ghostwu@dev:~/linux/cp$ cat ghostwu_hardlink 
hello,my name is ghostwu

After deleting the source file, the soft link cannot view the content, and the hard link will not be affected

ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569453 lrwxrwxrwx 1 ghostwu ghostwu 11 5 month   6 20:03 ghostwu_softlink -> ghostwu.txt
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ rm ghostwu.txt 
ghostwu@dev:~/linux/cp$ ls -ilh
total 4.0K
9569451 -rw-rw-r-- 1 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569453 lrwxrwxrwx 1 ghostwu ghostwu 11 5 month   6 20:03 ghostwu_softlink -> ghostwu.txt
ghostwu@dev:~/linux/cp$ cat ghostwu_softlink 
cat: ghostwu_softlink: No such file or directory
ghostwu@dev:~/linux/cp$ cat ghostwu_hardlink 
hello,my name is ghostwu

Restore the source file through hard link, and soft link can be used again

ghostwu@dev:~/linux/cp$ ls -ilh
total 4.0K
9569451 -rw-rw-r-- 1 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569453 lrwxrwxrwx 1 ghostwu ghostwu 11 5 month   6 20:03 ghostwu_softlink -> ghostwu.txt
ghostwu@dev:~/linux/cp$ ln ghostwu_hardlink ghostwu.txt
ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569453 lrwxrwxrwx 1 ghostwu ghostwu 11 5 month   6 20:03 ghostwu_softlink -> ghostwu.txt
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt
ghostwu@dev:~/linux/cp$ cat ghostwu_softlink 
hello,my name is ghostwu

Cannot create hard link for directory

ghostwu@dev:~/linux/cp$ ln /home/ghostwu/ ghostwu_home
ln: /home/ghostwu/: hard link not allowed for directory

But you can create soft links for directories

ghostwu@dev:~/linux/cp$ ln -s /home/ghostwu/ ghostwu_home
ghostwu@dev:~/linux/cp$ ls -ilh
total 8.0K
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu_hardlink
9569550 lrwxrwxrwx 1 ghostwu ghostwu 14 5 month   6 20:07 ghostwu_home -> /home/ghostwu/
9569453 lrwxrwxrwx 1 ghostwu ghostwu 11 5 month   6 20:03 ghostwu_softlink -> ghostwu.txt
9569451 -rw-rw-r-- 2 ghostwu ghostwu 25 5 month   6 19:51 ghostwu.txt

Tags: Linux snapshot Windows

Posted on Thu, 19 Mar 2020 09:18:47 -0700 by Megienos