Hard and Softlink in CentOS

there have two types hard and soft links

//softlink as consider as a shortcuts like windows
//softlink have different inode numbers
//here if you removed source the link file will not work

[root@boobalan-aadhardigital boobalan]# cd linktest/
[root@boobalan-aadhardigital linktest]# ls
[root@boobalan-aadhardigital linktest]# touch file1
[root@boobalan-aadhardigital linktest]# ls
file1

[root@boobalan-aadhardigital linktest]#ln -s file1 file2
//softlink

[root@boobalan-aadhardigital linktest]# ls -li
total 8
8417318 -rw-r--r--. 1 root root 17 Aug  1 13:05 file1
8417321 lrwxrwxrwx. 1 root root  5 Aug  1 13:06 file2 -> file1
8438508 -rw-r--r--. 1 root root 14 Aug  1 13:11 file3

 

[root@boobalan-aadhardigital linktest]# echo this is filetest >> file1 

 

[root@boobalan-aadhardigital linktest]# pr -m -t file1 file2 

this is filetest      this is filetest

 

//pr will display the multifiles

//-m merge the file

//-t omit the headers

 

 

[root@boobalan-aadhardigital linktest]# touch file3

[root@boobalan-aadhardigital linktest]# echo this is file3 > file3

[root@boobalan-aadhardigital linktest]# diff file1 file3

1c1

< this is filetest

---

> this is file3

 

//diff will used to differentiate the file strings

 

[root@boobalan-aadhardigital linktest]# sdiff file1 file2

this is filetest      this is filetest

 

//sdiff will display the both strings

//but it will support only two files

//so pr will do more then files upto 72 columns 

 

[root@boobalan-aadhardigital linktest]# ln -s file1 file4

[root@boobalan-aadhardigital linktest]# ln -s file1 /root/boobalan/file4

//here we can not make softlink in outside the directory

 

[root@boobalan-aadhardigital linktest]# ls -li

total 8

8417318 -rw-r--r--. 1 root root 17 Aug  1 13:05 file1

8417321 lrwxrwxrwx. 1 root root  5 Aug  1 13:06 file2 -> file1

8438508 -rw-r--r--. 1 root root 14 Aug  1 13:11 file3

8417322 lrwxrwxrwx. 1 root root  5 Aug  1 13:22 file4 -> file1

 

 

[root@boobalan-aadhardigital linktest]# mv file1 file1-1

[root@boobalan-aadhardigital linktest]# ls -li

total 8

8417318 -rw-r--r--. 1 root root 17 Aug  1 13:05 file1-1

8417321 lrwxrwxrwx. 1 root root  5 Aug  1 13:06 file2 -> file1

8438508 -rw-r--r--. 1 root root 14 Aug  1 13:11 file3

8417322 lrwxrwxrwx. 1 root root  5 Aug  1 13:22 file4 -> file1

 

//here if your delete your source file the link file got red hightlighted. you cant view the content 

[root@boobalan-aadhardigital linktest]# pr -m -t file1 file2 file3 file4

pr: file1: No such file or directory

pr: file2: No such file or directory

pr: file4: No such file or directory

this is file3

 

[root@boobalan-aadhardigital linktest]# mv file1-1 file1

[root@boobalan-aadhardigital linktest]# pr -m -t file1 file2 file3 file4

this is filetest  this is filetest  this is file3     this is filetest

 

//here you will get back

 

[root@boobalan-aadhardigital ~]# ln -s /root/boobalan/ /root/boobalan/linktest/

 

[root@boobalan-aadhardigital ~]# ls -li /root/boobalan/linktest/

total 8

8401941 lrwxrwxrwx. 1 root root 15 Aug  1 13:56 boobalan -> /root/boobalan/

8417318 -rw-r--r--. 1 root root 17 Aug  1 13:05 file1

8417321 lrwxrwxrwx. 1 root root  5 Aug  1 13:06 file2 -> file1

8438508 -rw-r--r--. 1 root root 14 Aug  1 13:11 file3

8417322 lrwxrwxrwx. 1 root root  5 Aug  1 13:22 file4 -> file1

 

//here you were getting a boobalan directory inside the linktest folder

 

unlink

 

[root@boobalan-aadhardigital linktest]# unlink file2

[root@boobalan-aadhardigital linktest]# ls -li

total 8

8401941 lrwxrwxrwx. 1 root root 15 Aug  1 13:56 boobalan -> /root/boobalan/

8417318 -rw-r--r--. 1 root root 17 Aug  1 13:05 file1

8438508 -rw-r--r--. 1 root root 14 Aug  1 13:11 file3

8417322 lrwxrwxrwx. 1 root root  5 Aug  1 13:22 file4 -> file1

[root@boobalan-aadhardigital linktest]# unlink file4 

[root@boobalan-aadhardigital linktest]# ls -li

total 8

8401941 lrwxrwxrwx. 1 root root 15 Aug  1 13:56 boobalan -> /root/boobalan/

8417318 -rw-r--r--. 1 root root 17 Aug  1 13:05 file1

8438508 -rw-r--r--. 1 root root 14 Aug  1 13:11 file3

 

 

hard link

 

//hard link shows how links point to the directory

//hard link have same inode numbers

//here if your remove source file also work

#ln sorucefile destfile

 

[root@boobalan-aadhardigital boobalan]# cd harlinktest/

[root@boobalan-aadhardigital harlinktest]# ls

[root@boobalan-aadhardigital harlinktest]# touch file1

[root@boobalan-aadhardigital harlinktest]# ls

file1

[root@boobalan-aadhardigital harlinktest]# ln file1 file2

 

[root@boobalan-aadhardigital harlinktest]# ls -li

total 0

808338672 -rw-r--r--. 2 root root 0 Aug  1 14:12 file1

808338672 -rw-r--r--. 2 root root 0 Aug  1 14:12 file2

[root@boobalan-aadhardigital harlinktest]# 

 

[root@boobalan-aadhardigital harlinktest]# echo this is file1 >file1

[root@boobalan-aadhardigital harlinktest]# pr -m -t file1 file2

this is file1       this is file1

[root@boobalan-aadhardigital harlinktest]# rm file1

rm: remove regular file ‘file1’? y

[root@boobalan-aadhardigital harlinktest]# pr -m -t file1 file2

pr: file1: No such file or directory

this is file1

 

[root@boobalan-aadhardigital harlinktest]# ln file1 file3

[root@boobalan-aadhardigital harlinktest]# ls -li

total 4

  8479406 drwxr-xr-x. 2 root root 22 Aug  1 14:20 dir1

269822291 drwxr-xr-x. 2 root root  6 Aug  1 14:20 dir2

537307375 drwxr-xr-x. 2 root root  6 Aug  1 14:22 dir3

808338674 -rw-r--r--. 2 root root  0 Aug  1 14:19 file1

808338672 -rw-r--r--. 1 root root 14 Aug  1 14:18 file2

808338674 -rw-r--r--. 2 root root  0 Aug  1 14:19 file3

 

//linked file are same inodes

 

-----------------------------------------------------------------

 

[root@server BOOBI]# ls -lah /var/

lrwxrwxrwx   1 root root   13 Aug  9  2018 log -> /data/var/log

 

[root@server BOOBI]# du -sh /var/log/

38G     /var/log/

[root@server BOOBI]# du -sh /data/var/log/

38G     /data/var/log/

 

[root@server BOOBI]# df -h

/dev/mapper/rhel_server-var    13G  1.6G   12G  13% /var

/dev/mapper/vg_data-lv_data           945G  128G  818G  14% /data

 

//above is the example that /var/log all files are replicated to /dat/var/log the file is present in both

places and the file size are reflect only /data that is symbolic link

 

Softlink/symbolic link

 

#ln -s (file path you want to point to) (new file path)

//(new file path) should not be there, it will create using the link command.

#unlink logstore

#unlink (new filepat)

-->example<-----

[root@server linktest]# ln -s file1 file3

//file1 is already created, and file3 is not there already it will create auto after the command

 

[root@server linktest]# ls

file1  file2  file3  log  logsource  logstore

 

[root@server linktest]# ls -lah

total 4.0K

lrwxrwxrwx 1 root    root            5 Apr 12 14:20 file3 -> file1

 

[root@server linktest]# cat file3

 tkdjfkjkfjkafjdkjfk

[root@server linktest]# cat file1

 tkdjfkjkfjkafjdkjfk

 

[root@server linktest]# du -sh file3

0       file3

[root@server linktest]# du -sh file1

4.0K    file1

 

//here the data stores in file1,but the originally created in file3

 

#ln -s (where you want to redirect ) (original paht of file/dire)->the original path is create auto

 

example 2

#systemctl stop rsyslog

#cd var

[root@server linktest]# mkdir log

[root@server linktest]# ls

file1  file2  log  logsource  logstore

[root@server linktest]# mv log /home/BOOBI/

[root@server linktest]# ls

file1  file2  logsource  logstore

[root@server linktest]# ln -s /home/BOOBI/log

or

[root@server linktest]# ln -s /home/BOOBI/log log

 

[root@server linktest]# ls

file1  file2  log  logsource  logstore

[root@server linktest]# ls -lah

total 8.0K

drwxr-xr-x 3 root    root           76 Apr 12 14:46 .

drwx------ 7 BOOBI domain users 4.0K Apr 12 12:00 ..

-rw-r--r-- 1 root    root           21 Apr 12 14:21 file1

-rw-r--r-- 1 root    root            0 Apr 12 14:19 file2

lrwxrwxrwx 1 root    root           18 Apr 12 14:46 log -> /home/BOOBI/log

 

[root@server linktest]# ls log

test  test2  test3

[root@server linktest]# du -sh log

0       log

[root@server linktest]# du -sh /home/BOOBI/log/

 

 

//unlink  #unlink log

 

--------------------------------------------------------------

 

Hard link

 

#ln (original file path) (new file path)

-------------------------------------------------

 


Post a Comment

0 Comments