Solved

symbolic link and hard link

Posted on 2009-04-09
11
502 Views
Last Modified: 2012-08-13
can someone give me a brief and simple explanation what these two means? I've read lots of page in the web, but can't seem to understand it..
0
Comment
Question by:kuntilanak
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 24107721
Hi kuntilanak,

Both types of links point to another object, usually a regular file. The subtle difference is in the way the link is recorded.

The recording of a symbolic (or soft) link contains a pointer to another object. A hard link contains the same file mapping as does the other object. The user calls them both links, but the hard link is really not a link. It is a duplication of the entry that describes the file. This allows just a single copy of a file to be accessed by different names. Following a sym link is a two step process -- read the sym link entry, then read the entry that it points to. Reading a hard link entry is a one step process -- read the hard link, it IS a description of the file.

Most users never have reason to create hard links. (In today's environment, creating a soft link should be the default behavior of the *ln* command.)

The reason for there even being a hard link is historical. A lot of programs have common code bases with only small changes to logic. The hard link allows a single program to be coded to perform all of the related activites. When a program is run, the file name (and full path) can be passed to the program. C does this all the time. The program can examine the name of the file that was run and determine what logic to execute.

I'm not aware of any hard links in RHEL. There probably are some, but I don't know of them so I'll make up an example.

The ls command is an executable file. The ll command is usually an alias that passes specific parameters to ls. One could also code the ll behavior into ls, create a hard link to the ls executable called ll, and have the ls/ll program determine the calling name and hence the behavior.


Hope this wasn't too confusing. :)

Kent
0
 

Author Comment

by:kuntilanak
ID: 24107800
hmm...but from what I understand hard links points to the same i-nodes... I don't get what that really means
0
 
LVL 45

Expert Comment

by:Kdo
ID: 24107990

An i-node is the entry in the catalog (directory) that describes the file.  It contains the file name, device (where applicable), starting address, length, creation date, etc.

A hard link results in two i-nodes that describe the exact same file.  A sym link is an i-node that points to the i-node that describes the file.


Kent
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:kuntilanak
ID: 24108146
ok.. so when creating hard links it's the i-node that are duplicated but not the actual file it self right? they still point to the same exact file and not the duplicate?
0
 
LVL 45

Expert Comment

by:Kdo
ID: 24108193
Correct.  

A hard link is an exact duplicate of the i-node, except for the file name.  Both point to the physical file.

A sym link points to the i-node that describes the file.


0
 

Author Comment

by:kuntilanak
ID: 24108290
so in UNIX every file/directory has an i-node? if that's so how do you differentiate between what's a file and what's a directory?
0
 
LVL 84

Expert Comment

by:ozo
ID: 24109517
the directory entry has a flag indicating the file type
0
 

Author Comment

by:kuntilanak
ID: 24109565
so say if it's a directory the inode has a flag bit set to 1 and if it's a file it's 0,. something like that?
0
 
LVL 84

Expert Comment

by:ozo
ID: 24109610
    struct dirent {
             u_int32_t d_fileno;             /* file number of entry */
             u_int16_t d_reclen;             /* length of this record */
             u_int8_t  d_type;               /* file type, see below */
             u_int8_t  d_namlen;             /* length of string in d_name */
     #ifdef _POSIX_SOURCE
             char    d_name[255 + 1];        /* name must be no longer than this */
     #else
     #define MAXNAMLEN       255
             char    d_name[MAXNAMLEN + 1];  /* name must be no longer than this */
     #endif
     };
     /*
      * File types
      */
     #define DT_UNKNOWN       0
     #define DT_FIFO          1
     #define DT_CHR           2
     #define DT_DIR           4
     #define DT_BLK           6
     #define DT_REG           8
     #define DT_LNK          10
     #define DT_SOCK         12
     #define DT_WHT          14
0
 

Author Comment

by:kuntilanak
ID: 24109627
and which flags shows it's a directory and which one tells that it's a file
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24112375
I have a comment on Kdo comment # 24108193:

when a hard link is created the i-node is note duplicated. It is just referenced to by the new file name in the dir table

http://linuxgazette.net/105/pitcher.html
http://www.maxi-pedia.com/difference+between+symbolic+link+and+hard+link
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question