Links in Linux,Difference between soft link and hard link,what is link in linux,uses of link,

Soft link files:

[root@vasanthra ~]# pwd

/root

[root@vasanthra ~]# cat > file1

hi this is soft link file

[root@vasanthra ~]# ln -s file1 file2

[root@vasanthra ~]# ls -l file1 file2

-rw-r–r– 1 root root 26 Jun  4 12:00 file1

lrwxrwxrwx 1 root root  5 Jun  4 12:01 file2 -> file1

link file has full permission and l added to mean its a link file. Link file has only name of the linked file ,it acts as a pointer to the linked file not the actual content of the linked file.file1 is file size is 26 whereas the file2’s filesize is 5.

create link for file3 ,use file2 as a linked file

[root@vasanthra ~]# ln -s file2 file3

[root@vasanthra ~]# ls -l file1 file2 file3

-rw-r–r– 1 root root 26 Jun  4 12:00 file1

lrwxrwxrwx 1 root root  5 Jun  4 12:01 file2 -> file1

lrwxrwxrwx 1 root root  5 Jun  4 12:01 file3 -> file2

[root@vasanthra ~]# ln -s file1 file4

[root@vasanthra ~]# ls -l file1 file2 file3 file4

-rw-r–r– 1 root root 26 Jun  4 12:00 file1

lrwxrwxrwx 1 root root  5 Jun  4 12:01 file2 -> file1

lrwxrwxrwx 1 root root  5 Jun  4 12:01 file3 -> file2

lrwxrwxrwx 1 root root  5 Jun  4 12:05 file4 -> file1

[root@vasanthra ~]# cat file1

hi this is soft link file

[root@vasanthra ~]# cat file2

hi this is soft link file

[root@vasanthra ~]# cat file3

hi this is soft link file

[root@vasanthra ~]# cat file4

hi this is soft link file

remove the file1 you cannot view the content of the file2 and file3

[root@vasanthra ~]# rm -rf file1

[root@vasanthra ~]# cat file2

cat: file2: No such file or directory

[root@vasanthra ~]# cat file3

cat: file3: No such file or directory

[root@vasanthra ~]# cat file4

cat: file4: No such file or directory

[root@vasanthra ~]# ls -l  file2 file3 file4

lrwxrwxrwx 1 root root 5 Jun  4 12:01 file2 -> file1

lrwxrwxrwx 1 root root 5 Jun  4 12:01 file3 -> file2

lrwxrwxrwx 1 root root 5 Jun  4 12:05 file4 -> file1

Hard link

[root@vasanthra ~]# rm -rf file*

[root@vasanthra ~]# cat > file1

hi this is hard link.

[root@vasanthra ~]# ls -l file1

-rw-r–r– 1 root root 22 Jun  4 12:09 file1

link count of the file is 1.if you make the hard link the to another file link count is increased by one.

[root@vasanthra ~]# ln file1 file2

[root@vasanthra ~]# ls -l file1 file2

-rw-r–r– 2 root root 22 Jun  4 12:09 file1

-rw-r–r– 2 root root 22 Jun  4 12:09 file2

[root@vasanthra ~]# ln file1 file3

[root@vasanthra ~]# ls -l file1 file2 file3

-rw-r–r– 3 root root 22 Jun  4 12:09 file1

-rw-r–r– 3 root root 22 Jun  4 12:09 file2

-rw-r–r– 3 root root 22 Jun  4 12:09 file3

[root@vasanthra ~]# ln file2 file4

[root@vasanthra ~]# ls -l file1 file2 file3 file4

-rw-r–r– 4 root root 22 Jun  4 12:09 file1

-rw-r–r– 4 root root 22 Jun  4 12:09 file2

-rw-r–r– 4 root root 22 Jun  4 12:09 file3

-rw-r–r– 4 root root 22 Jun  4 12:09 file4

if you remove the file1, you can able to view the other linked files.

[root@vasanthra ~]# rm -rf file1

[root@vasanthra ~]# cat file1

cat: file1: No such file or directory

[root@vasanthra ~]# cat file2

hi this is hard link.

[root@vasanthra ~]# cat file3

hi this is hard link.

[root@vasanthra ~]# cat file4

hi this is hard link.

[root@vasanthra ~]# rm -rf file2

[root@vasanthra ~]# cat file4

hi this is hard link.

soft link for directory:

you can create a soft link for directory same as files.

[root@vasanthra ~]# pwd

/root

[root@vasanthra ~]# mkdir dir1

[root@vasanthra ~]# ln -s dir1 dir2

[root@vasanthra ~]# ls -ld dir1 dir2

drwxr-xr-x 2 root root 4096 Jun  4 12:16 dir1

lrwxrwxrwx 1 root root    4 Jun  4 12:17 dir2 -> dir1

[root@vasanthra ~]# cd dir1

[root@vasanthra dir1]# ls

[root@vasanthra dir1]# cat > a

hi iam the file in soft link directory.

[root@vasanthra dir1]# cd ..

[root@vasanthra ~]# cd dir2

[root@vasanthra dir2]# ls

a

[root@vasanthra dir2]# cat a

hi iam the file in soft link directory.

[root@vasanthra dir2]# cat > b

iam the second file in soft link dir.i am in the dir2 directory.

[root@vasanthra dir2]# cd ..

[root@vasanthra ~]# cd dir1

[root@vasanthra dir1]# ls

a  b

[root@vasanthra dir1]# cat b

iam the second file in soft link dir.i am in the dir2 directory.

[root@vasanthra dir2]# cd ..

[root@vasanthra ~]# cd dir1

[root@vasanthra dir1]# ls

a  b

[root@vasanthra dir1]# cat b

iam the second file in soft link dir.i am in the dir2 directory.

[root@vasanthra dir1]# rm -rf a

[root@vasanthra dir1]# ls

b

[root@vasanthra dir1]# cd ..

[root@vasanthra ~]# cd dir2

[root@vasanthra dir2]# ls

b

[root@vasanthra dir2]# cat b

iam the second file in soft link dir.i am in the dir2 directory.

hard link for directory:

[root@vasanthra ~]# mkdir dir1

[root@vasanthra ~]# ln dir1 dir2

ln: `dir1′: hard link not allowed for directory

[root@vasanthra ~]#

Bcoz link count for directory should not be more than 1.

Creating  more than one symbolink for a file.

[root@vasanthra ~]# cat > file1

hi iam a softlink file.

iam going to link more than file.

[root@vasanthra ~]# ln -s file1 file2

[root@vasanthra ~]# cat > file3

iam a soft link file. iam going to link with file2, which is already linked with file1.

[root@vasanthra ~]# ln -s file3 file2

ln: creating symbolic link `file2′ to `file3′: File exists

[root@vasanthra ~]#

Hard link files to a directory

delete the previously created files bcoz iam using same file names for my example.

[root@vasanthra ~]# rm -rf file*

[root@vasanthra ~]# cat > file1

hi this is hard link file.

[root@vasanthra ~]# ln file1 file2

[root@vasanthra ~]# cat file1

hi this is hard link file.

[root@vasanthra ~]# cat file2

hi this is hard link file.

[root@vasanthra ~]# cat > file3

hi iam a hardlink file. iam going to link with file2 which is already linked to file1

.

[root@vasanthra ~]# ln file2 file1 dir1

[root@vasanthra ~]# cd dir1/

[root@vasanthra dir1]# ls

file1  file2

[root@vasanthra dir1]# cat file1

hi this is hard link file.

[root@vasanthra dir1]# cat file2

hi this is hard link file.

[root@vasanthra dir1]# cd ..

[root@vasanthra ~]# ln file3 dir1

[root@vasanthra ~]# cd dir1/

[root@vasanthra dir1]# ls

file1  file2  file3

[root@vasanthra dir1]# cat file3

hi iam a hardlink file. iam going to link with file2 which is already linked to file1

.

[root@vasanthra dir1]# ls -l

total 12

-rw-r–r– 4 root root 27 Jun  4 12:41 file1

-rw-r–r– 4 root root 27 Jun  4 12:41 file2

-rw-r–r– 2 root root 88 Jun  4 12:43 file3

[root@vasanthra dir1]# cd ..

files are existing  /root as well as /root/dir1 .

[root@vasanthra ~]# rm -rf file*

[root@vasanthra ~]# cd dir1/

[root@vasanthra dir1]# ls

file1  file2  file3

[root@vasanthra dir1]# cat file1

hi this is hard link file.

[root@vasanthra dir1]# cat file2

hi this is hard link file.

[root@vasanthra dir1]# cat file3

hi iam a hardlink file. iam going to link with file2 which is already linked to file1

.

Hard Link cross file system:

create file in / directory named as file1 , make a hard link this files to user vasu and devi.

[root@vasanthra /]# pwd

/

[root@vasanthra /]# cat > file1

hi iam a hard link file.

iam going to link with two users vasu and devi.

[root@vasanthra ~]# useradd vasu

[root@vasanthra ~]# useradd devi

[root@vasanthra ~]# passwd vasu

Changing password for user vasu.

New UNIX password:

BAD PASSWORD: it is too short

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

[root@vasanthra ~]# passwd devi

New UNIX password:

BAD PASSWORD: it is too short

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

[root@vasanthra /]# ln file1 /home/vasu/file1hl

[root@vasanthra /]# ln file1 /home/devi/file1hl

[root@vasanthra /]# su – vasu

[vasu@vasanthra ~]$ pwd

/home/vasu

[vasu@vasanthra ~]$ cat file1hl

hi iam a hard link file.

iam going to link with two users vasu and devi.

[vasu@vasanthra ~]$ exit

logout

[root@vasanthra /]# su – devi

[devi@vasanthra ~]$ cat file1hl

hi iam a hard link file.

iam going to link with two users vasu and devi.

[devi@vasanthra ~]$

soft link cannot cross file sytem:

[root@vasanthra /]# cat > file2

hi this is soft link file. iam going to link with user vasu and devi.

[root@vasanthra /]# ln -s file2 /home/vasu/file2sl

[root@vasanthra /]# ln -s file2 /home/devi/file2sl

[root@vasanthra /]# su – vasu

[vasu@vasanthra ~]$ cat file2sl

cat: file2sl: No such file or directory

[vasu@vasanthra ~]$ ls -l

total 4

-rw-r–r– 3 root root 73 Jun  4 14:39 file1hl

lrwxrwxrwx 1 root root  5 Jun  4 16:48 file2sl -> file2

[vasu@vasanthra ~]$

exit

[root@vasanthra /]# su – devi

[devi@vasanthra ~]$ cat file2sl

cat: file2sl: No such file or directory

[devi@vasanthra ~]$ ls -l

total 4

-rw-r–r– 3 root root 73 Jun  4 14:39 file1hl

lrwxrwxrwx 1 root root  5 Jun  4 16:48 file2sl -> file2

Leave a Reply