본문 바로가기
my_lesson/_Linux

Linux - The Difference Between Soft(symbolic) and Hard Links

by boolean 2016. 7. 8.
728x90

What are they?

Symbolic links are like shortcuts or references to the actual file or directory

싱볼릭 링크는 실행파일 이나 디렉토리 로 접근하기위한 최단경로 또는 참조영역과도 같다.

Most of the time these links are transparent when working with them through other programs. 

다른 프로그램을 통해 그들과 작업할때 대부분의 경우 이렇한 링크들은 투명하다.

For example you could have a link to a directory to one hard drive inside a directory that lies on a different hard drive and an application will still treat it the same.

예를 들어, 당신은 다른 하드 드라이브에 놓여있는 디렉토리 안에 어떤 하드 드라이브의 디렉토리에 대한 링크를 가질 수 있고 응용 프로그램은 여전히 그것을 동일하게 처리합니다.

Symbolic links are used all the time to link libraries and make sure files are in consistent places without moving or copying the original. 

심볼 링크 라이브러리를 연결하고 이동하거나 원본을 복사하지 않고  파일이 일치하는지 확인하기 위해 모든 시간을 사용한다. 

Links are often used to “store” multiple copies of the same file in different places but still reference to one file.

링크는 종종 다른 장소에서 같은 파일의 여러 복사본 저장소를 사용하지만 여전히 하나의 파일에 참조된다.

Creating a Symbolic Link

To create a symbolic link in Linux we use this syntax:

ln -s /path/to/original/ /path/to/linkName

What happens if I edit the link? Any modifications to the linked file will be changed on the original file.
What happens if I delete the link? If you delete the link the original file is unchanged. It will still exist.
What happens if I delete the original file but not the link? The link will remain but will point to a file that does not exist. This is called an orphaned or dangling link.

Creating a Hard Link

You can create a hard link like so:

ln /path/to/original.file /path/to/link.file

The Difference Between Soft and Hard Links

Hard links

  • Only link to a file not a directory
  • Can not reference a file on a different disk/volume
  • Links will reference a file even if it is moved
  • Links reference inode/physical locations on the disk

Symbolic (soft) links

  • Can link to directories
  • Can reference a file/folder on a different hard disk/volume
  • Links remain if the original file is deleted
  • Links will NOT reference the file anymore if it is moved
  • Links reference abstract filenames/directories and NOT physical locations. They are given their own inode

Practice Makes Perfect

To really grasp the concept of symbolic links lets give it a shot.

Go into the tmp directory:

cd /tmp

Make a directory

mkdir original

Copy over the host.conf file or any file to test with.

cd original
cp /etc/host.conf .

List the contents and take note of the inode (first column)

ls -ila

Create a symbolic link to host.conf called linkhost.conf

ln -s host.conf linkhost.conf

Now do list out the inodes

ln -ila

Notice how the inode for the link is different.

Now create a hard link to the same file

ln host.conf hardhost.conf

Now list the inoes one more time

ln -ila

Notice how the inode numbers are exactly the same for the hard link and the actual file.

Lets try some file operations now

Open up linkhost.conf and edit it and save it

Now look in host.conf and notice that the changes were made

Lets move host.conf now and see if it causes any problems

mv host.conf ../

Uh oh, now when we list the directory our link turned red lets see what is in side it

cat linkhost.conf
cat: linkhost.conf: No such file or directory

It looks like our symbolic link is now broken as it linked to a file name and not the inode. What about our hard link?

cat hardhost.conf

Looks like our hard link still works even though we moved the original file. This is because the hard link was linked to the inode the physical reference on the hard drive where the file resides. The soft link (symbolic link) on the other hand was linked to the abstract file name and was broken the moment we moved the file.

This leads to an interesting question. What happens if I delete a hard link? Even though the hard link is referenced to the physical location of the file on the hard drive though an inode, removing a hard link will not delete the original file.

Symbolic links will remain intact but will point to a non existent file.

출처 : http://www.nixtutor.com/freebsd/understanding-symbolic-links/

댓글