Softlink and Hardlink in linux

Softlink: It is also called symlink . Softlink is similar to shortcut file which create in windows world. we create softlink to point original file with shortcut which tells the actual location of file.
Soft link file and orignal file has different inode no. also when we delete original file soflink file automatically fail.

Hardlink: It is copy of original file when any changes done in hardlink file same data update in orignal file. In hardlink inode number remain same. when hardlink file deleted noting will happen to the original file.

Create Solftlink file

# mkdir soft-testing
# cd soft-testing
# touch file1

# ls -i
24538503 file1

Example to create softlink of any file

Syntax:
#ln -s  original-file  softlink-file
# ln -s file1 file2

# ls -i
24538503 file1  24538504 file2
or,

[root@mail1 testing]# ls -il
total 0
24538503 -rw-r--r-- 1 root root 0 Oct 31 22:58 file1
24538504 lrwxrwxrwx 1 root root 5 Oct 31 23:01 file2 -> file1

Note: as seen both file has different inode no . where file1 is original file.

Now test by writing someting in file2
# echo "test" >file2
#cat file2
test

#cat file1
test

Delete original file
#rm -rf file1

now see the output of softlink file
# cat file2
cat: file2: No such file or directory

Example to create softlink of any directory 
Create directory  /data1/data2/data3 and make some  files inside the directory
# mkdir -p /data1/data2/data3
# cd /data1/data2/data3
# touch a{1..10}

Now create softlink of directory /data1/data2/data3  to /root/data3
# ln -s /data1/data2/data3 /root/data3

Softlink of directory successfully created now you can also access your files and directory using this short location /root/data3
# ls -ld /root/data3
lrwxrwxrwx 1 root root 18 Aug 12 06:04 /root/data3 -> /data1/data2/data3

you can now also access you files and directing softlink created directory
# cd /root/data3
# ls
a1  a10  a2  a3  a4  a5  a6  a7  a8  a9

Create hardlink
#mkdir hard-testing
#cd hard-testing
# touch test1

create hardlink of test1 to test2
Syntax:

#ln  original-file  hardlink-file
Ex:

#ln test1 test2
# ls -i
24538504 test1  24538504 test2

Note: both file have different inode no. you can test by deleting one of file will not impact to any .

How to remove / unlink softlink or hardlink

#rm -rf  linkname
or,
#unlink linkname

Ex:
# unlink test2

Share on Google Plus

About Penguin Technology

I am a passionate cloud and DevOps professional specializing in Linux and open-source solutions. Through this blog, I share my knowledge and experience with the community, offering tips and insights on cloud technologies and DevOps practices.
    Blogger Comment

1 comments:

  1. I gathered as much knowledge regarding to the Linux, web sever and database server from your site.Keep on updating with more interesting topic.
    Moreover to recover the lost file from Linux Click on:
    android data recovery review
    launch android data recovery
    backup and restore android apps and data
    android backup app data
    recover lost files
    diskdigger pro file recovery


    ReplyDelete