본문 바로가기

리눅스-기초명령어/파일 시스템 관리

ln

하드링크 또는 심볼릭 링크 파일을 만든다.

 

사용법

ln [옵션] [원본파일] [링크 파일명] 

 

옵션

옵션 설명
-b 같은 이름의 파일이 존재한다면 백업파일을 생성한다.
-f 같은 이름의 파일이 존재한다면 대상파일을 삭제한다.
-i 같은 이름의 파일이 존재한다면 삭제 여부를 확인한다.
-S 같은 이름의 파일이 존재한다면 백업파일의 접미사(suffix)를 지정하여 생성한다.
-s 심볼릭 링크파일을 생성한다.
-d 디렉토리의 하드링크를 생성한다. root권한으로만 가능하지만 root권한으로 실행하더라도 시스템 권한제한으로 인해 실패할수도 있다. 

 

예시

  • 링크 만들기(하드링크)
[root@Linux ~]# echo hello > hello.txt
# hello 내용을 가진 hello.txt 파일 생성
[root@Linux ~]# ln hello.txt hello_hard.txt
[root@Linux ~]# ls -li
total 4
135767191 -rw-r--r--. 1 root root   4 Apr 26 15:21 happy.txt
134326662 -rw-r--r--. 2 root root   6 Apr 26 14:47 hello_hard.txt
134326662 -rw-r--r--. 2 root root   6 Apr 26 14:47 hello.txt
...

>> 원본 파일과 링크 파일이 동일한 inode를 지닌다. 사실상 원본파일이 하나더 생긴다고 볼수있다.

 

원본 파일이 삭제되거나 변경 되었을 경우

[root@Linux ~]# rm -rf hello.txt
[root@Linux ~]# cat hello_hard.txt
hello

>> 원본 파일이 삭제되어도 링크 파일이 동일한 inode를 지니고 있으므로 데이터에 접근 가능



  1. 링크 만들기(소프트링크)
[root@Linux ~]# echo happy > happy.txt
# happy.txt 안에 happy라는 내용 입력
[root@Linux ~]# ln -s happy.txt happy_soft.txt
[root@Linux ~]# ls -li
total 4
135729146 lrwxrwxrwx. 1 root root   9 Apr 26 16:14 happy_soft.txt -> happy.txt
135767191 -rw-r--r--. 1 root root   4 Apr 26 15:21 happy.txt
...

>> 원본 파일과 링크 파일이 다른 i-node를 가짐(소프트링크).

 

원본 파일이 삭제되거나 변경되었을 경우

[root@Linux ~]# cat happy_soft
happy
[root@Linux ~]# rm -rf happy.txt
[root@Linux ~]# cat happy_soft.txt
cat: happy_soft: No such file or directory
[root@Linux ~]# ll
total 0
lrwxrwxrwx. 1 root root 9 Apr 26 15:20 happy_soft -> happy.txt
[root@Linux ~]# echo sad > happy.txt
[root@Linux ~]# cat happy_soft.txt
sad
[root@Linux ~]# ll
total 4
lrwxrwxrwx. 1 root root 9 Apr 26 15:20 happy_soft -> happy.txt
-rw-r--r--. 1 root root 4 Apr 26 15:21 happy.txt

>> 원본 파일이 삭제되면 링크 파일은 데이터에 접근 할 수 없음

 

절대 경로로 다른 디렉토리에 소프트 링크 생성하기

[root@Linux tmp]# ln -s /root/soft /tmp/abb
[root@Linux tmp]# cd /tmp
[root@Linux tmp]# ls
abb
[root@Linux tmp]# stat abb
  File: abb -> /root/soft
Size: 10        Blocks: 0          IO Block: 4096   symbolic link

>> /root/soft 디렉토리를 참조하는 /tmp/abb라는 소프트 링크파일 생성



[root@Linux test]# ln -d up uup
ln: failed to create hard link 'uup' => 'up': Operation not permitted

 

문제가 많이 생겨서 버전 업데이트 이후 사실상 없는 옵션이 되었다. 파일 시스템은 트리 구조이다. 부모 노드에서 자식 노드로 연결되는 트리구조의 특징상 자식 노드에 부모 노드의 하드링크를 생성하면서 생기는 문제때문에 모두가 쓰지 않는다. 그 외에도 파일 시스템에 루프를 발생시키거나 여러개의 모호한 부모를 갖게 되는 등의 악조건을 만들게 되어서 사용하지 않는다.

 

'리눅스-기초명령어 > 파일 시스템 관리' 카테고리의 다른 글

stat  (0) 2023.08.31
rm  (2) 2023.08.31
mv  (1) 2023.08.31
CP  (0) 2023.08.31
mkdir  (0) 2023.08.31