출처 : 유튜브 따라하면서 배우는 it - 리눅스 기초
링크란? - 바로가기와 비슷한 개념
- 특정 파일 또는 디렉토리에 접근을 쉽게 할 수 있도록 하는 방법
- 파일 시스템이 물리적인 장치인 하드 디스크 상에 저장되어 있는 특정 파일의 위치를 가리키는 것
링크의 종류
1. 하드링크
- 특정 파일 또는 디렉토리에 접근을 쉽게 할 수 있도록 하는 방법
- 파일 시스템이 물리적인 장치인 하드 디스크 상에 저장되어 있는 특정 파일의 위치를 가리키는 것
1-1. 하드링크 실습
- 내 이름 이니셜으로 된 폴더에 vi origin으로 파일을 하나 생성한다 (내용: 이니셜)
- cat로 확인
- ln origin origin_hl 로 하드링크 생성
- vi origin으로 내용 수정 후 확인(cat origin), 하드링크에도 변동이 있는지 확인(cat origin_hl)
- => 원본 파일도, 하드링크 파일에도 수정된 것이 똑같이 적용된다.


- 어디로 접근을 하든 디스크 상에 접근하는 것은 똑같음
1-2. inode로 확인하기

- 디스크상의 주소를 가리키는 옵션 -il
- ls -il 명령으로 확인하니,

1-3. 복사본과 하드링크 비교하기
- 복사본 심볼릭 링크를 만들어보자.

- origin과 origin_cp의 inode 값이 다르다는 것을 알 수 있다.
- 원본파일을 다시 한 번 수정해본다.
(vi origin 하여, bsj 123 뒤에 456이라고 추가)

- 하드링크 파일은 똑같이 변동되는 것을 확인할 수 있었으나 (똑같은 파일),
origin_cp
의 파일은 바뀌지 않았다. 다른 파일이라는 것을 알 수 있다.
2. 심볼릭 링크
- 윈도우의 바로 가기 개념
- 윈도우의 바로가기 개념과 비슷하며 실질적인 디스크 상의 파일을 가리키는 것이 아니라 파일 시스템 상의 특정 파일을 가리키는 것
- 간접적으로 한 번 거쳐서 가리킴

/bin(링크 파일) -> usr/bin (원본파일)

직접적으로 찾아가는 게 아니라, 간접적으로 찾아감
링크 생성 명령어 - ln
ln [옵션][원본] [링크]
- 옵션을 지정하지 않고 링크 파일을 생성하면
하드 링크
를 생성할 수 있다.
- 옵션으로
-s
를 지정하고 링크 파일을 생성하면 심볼릭 링크
를 생성할 수 있다.
2-1. 심볼릭 링크 파일 생성해보기


inode 번호가 다르다는 것을 알 수 있다.
ln -s origin origin_sl
로 심볼릭 링크 생성하기!
2-2. 원본 수정 후 심볼릭 링크 파일과 비교하기

vi origin 으로 변경하였다.
1) cat origin : 원본 파일 변경 확인

2) origin_cp : 변경 안됨

3) origin_hl : 변경됨

origin_sl : 변경됨
- inode 값은 다르지만, 타고 들어가서 간접적으로 바뀐다.
- 윈도우의 단축 아이콘들 (속성 보면 원본의 경로가 뜨듯)

2-3. 원본이 삭제가 된다면?

- 빨간색으로 삭제를 표시하는 것을 알 수 있다.
- 1) 심볼릭 링크로 파일을 열어보면? : 찾을 수 없음

- 2) 하드 링크 파일 열어보면? : 그대로 나옴

2-4. 절대경로로 입력해보자
- 새로 다시 시작해본다. 절대경로로 입력하기 전!


- / 슬래시가 안붙으면 상대경로
- /bsj/origin 절대경로로
무슨 차이가 있는걸까?
2-5. 상대 경로의 심볼릭 링크를 다른 곳으로 옮겨본다면?

- 절대경로인 경우엔 origin 파일을 잘 찾아간다.
- origin으로 찾으면 현재 디렉토리에서이기 때문에 못 찾는 것이다.

2-6. 원본을 못읽던 상대경로 심볼링 링크 디렉토리로 옮겨주면?

- 이번엔 절대경로로 작성된 파일의 경로를 읽을 수 없게 된다. 음메이징