Fundamentals

본문 바로가기
사이트 내 전체검색


RedHat Linux >> Fundamentals of the GNU/Linux
[목차]
제3장 리눅스 명령어

    13. 파일 링크하기 - ln


앞에서 파일에 대하 언급할 때 파일마다 고유번호를 가진다고 했습니다. 이렇게 파일마다 다른 번호를 inode라는 용어로 정의합니다. 'ls' 명령어에 'i' 옵션을 붙이면 이 번호가 화면에 출력될 것입니다.

[root@leelab /]# ls -i

20481 bin/ 28673 lib/ 40961 root/

22529 boot/ 11 lost+found/ 43009 sbin/

4097 etc/ 32769 mnt/ 45057 usr/

26625 home/ 1 proc/ 8193 var/

파일만 inode를 가진 것이 아니라 디렉토리도 inode를 가지고 있을 것입니다. 리눅스에서는 모든 디바이스나 디렉토리를 파일 개념으로 인식하기 때문에 전부 inode를 가지게 됩니다.

지금 inode에 대해 언급하는 이유는 이제부터 배울 'ln'이라는 명령어가 이 inode와 연관이 있기 때문입니다. inode가 같은 파일이 있다면 파일 이름이 다르더라고 같은 파일이 됩니다. 짐작이 잘 가지 않는다면 간단한 예를 보도록 합시다.

[root@leelab /]# ls

bin/ etc/ lost+found/ proc/ test.txt var/

boot/ home/ misc/ root/ tmp/

dev/ lib/ mnt/ sbin/ usr/

[root@leelab /]# cat test.txt

this is test file

this is text file

[root@leelab /]# ln test.txt test1.txt

[root@leelab /]# ls

bin/ etc/ lost+found/ proc/ test.txt usr/

boot/ home/ misc/ root/ test1.txt var/

dev/ lib/ mnt/ sbin/ tmp/

[root@leelab /]#

보는 바와 같이 'ln'이라는 명령어로 test1.txt 파일을 생성했습니다. 생성할 파일을 마지막 아규먼트로 지정하면 되거든요. 그러면 파일의 inode를 확인해 봅시다.

[root@leelab /]# ls -i

20481 bin/ 11 lost+found/ 79 test.txt

22529 boot/ 436229 misc/ 79 test1.txt

2049 dev/ 32769 mnt/ 6145 tmp/

4097 etc/ 1 proc/ 45057 usr/

26625 home/ 40961 root/ 8193 var/

28673 lib/ 43009 sbin/

[root@leelab /]#

정말로 두 파일의 inode가 같죠? 이 두 파일은 같은 파일입니다.

물론 파일의 내용도 다음과 같이 당연히 같습니다.

[root@leelab /]# cat test1.txt

this is test file

this is text file

[root@leelab /]#

그러면 나중에 만든 test1.txt 파일을 필자가 한번 수정해 보도록 하겠습니다.

[root@leelab /]# cat test1.txt

this is text file

this is text file

[root@leelab /]#

수정한 다음에 그 내용을 출력하고 있습니다. 이번에는 수정을 하지 않은 test.txt 파일의 내용을 확인해 보도록 하겠습니다.

[root@leelab /]# cat test.txt

this is text file

this is text file

[root@leelab /]#

정말로 수정이 되었죠? 신기하군요. 그러면 이번에는 test1.txt 파일을 삭제해 보겠습니다.

[root@leelab /]# rm test1.txt

rm: remove `test1.txt'? y

[root@leelab /]# ls

bin/ etc/ lost+found/ proc/ test.txt var/

boot/ home/ misc/ root/ tmp/

dev/ lib/ mnt/ sbin/ usr/

[root@leelab /]# cat test.txt

this is text file

this is text file

[root@leelab /]#

삭제하는 것은 별로 영향을 받지 않는다는 것을 알 수 있습니다. 그러면 그 반대의 경우를 보도록 합시다.

[root@leelab /]# ls

bin/ etc/ lost+found/ proc/ test.txt usr/

boot/ home/ misc/ root/ test1.txt var/

dev/ lib/ mnt/ sbin/ tmp/

[root@leelab /]# rm test.txt

rm: remove `test.txt'? y

[root@leelab /]# cat test1.txt

this is text file

this is text file

[root@leelab /]#

역시 영향을 받지 않는군요. 일단 'ln' 명령어로 같은 파일을 생성하면 수정할 때 같이 수정되지만, 삭제할 때는 아무런 영향이 없다는 것을 알 수 있습니다. 위와 같이 'ln'이라는 명령어로 연결된 새로운 파일을 생성하는 것을 파일링크라는 용어로 표현합니다. 특히 inode를 같게 만드는 위와 같은 파일링크를 하드링크라고 합니다. 하드링크로 파일을 생성하면 inode가 같은 동일한 파일과 마찬가지로 되기 때문에 파일의 크기 또한 같습니다.

[root@leelab /]# ls -l test*

-rw-r--r-- 2 root root 36 jan 14 00:46 test.txt

-rw-r--r-- 2 root root 36 jan 14 00:46 test1.txt

[root@leelab /]#

이번에는 파일링크 중 또 다른 심벌릭 링크에 대한 것을 알아보겠습니다. 역시 이것도 같은 파일을 생성하는 것과 같습니다. 그러나 inode는 다릅니다. 그러니 완벽하게 같은 파일은 아니죠. 그러면 왜 같은 파일이라는 말을 사용할까요? 그것은 간간한 예를 보면 금방 알 수 있습니다.

[root@leelab /]# ls

bin/ etc/ lost+found/ proc/ test.txt var/

boot/ home/ misc/ root/ tmp/

dev/ lib/ mnt/ sbin/ usr/

[root@leelab /]# ln -s test.txt test1.txt

[root@leelab /]# cat test.txt

this is test file

this is text file

[root@leelab /]# cat test1.txt

this is test file

this is text file

[root@leelab /]#

'ln'명령어에 's' 옵션을 붙여서 사용했죠? 이 옵션을 붙이면 심벌릭 링크된 파일을 생성하는 것입니다. 역시 파일의 내용은 같군요. 그러면 심벌릭 링크된 파일의 내용을 필자가 한번 바꿔보겠습니다.

[root@leelab /]# cat test1.txt

this is text file

this is text file

[root@leelab /]# cat test.txt

this is text file

this is text file

[root@leelab /]#

test1.txt 파일의 내용을 바꾸기만 했는데 tesst.txt 파일의 내용도 바뀌었죠? 여기까지는 하드링크 때와 크게 달라진 점을 찾을 수 없을 것입니다. 그러면 파일의 inode를 확인해 보기 바랍니다.

[root@leelab /]# ls -i

20481 bin/ 11 lost+found/ 79 test.txt

22529 boot/ 436229 misc/ 80 test1.txt@

2049 dev/ 32769 mnt/ 6145 tmp/

4097 etc/ 1 proc/ 45057 usr/

26625 home/ 40961 root/ 8193 var/

28673 lib/ 43009 sbin/

[root@leelab /]#

앞에서 언급함 것과 마찬가지로 inode는 다르군요. 그러면 분명히 서로 다른 파일인데 어떻게 하나의 파일을 수정하면 다른 파일도 같이 수정될까요? 그것은 간단합니다. 즉 test1.txt 파일은 단지 test.txt 파일을 가리키는 역할만 하는 것입니다. 즉 test1.txt 파일을 수정했다는 것은 test1.txt 파일이 가리키는 파일을 수정한 것과 마찬가지라는 것입니다.

[root@leelab /]# ls -l test*

-rw-r--r-- 1 root root 36 jan 14 00:56 test.txt

lrwxrwxrwx 1 root root 8 jan 14 00:54 test1.txt -> test.txt

[root@leelab /]#

파일의 크기가 다른 것을 알 수 있을 것입니다. 심벌릭 링크된 파일이 훨씬 크기가 작죠? 단지 가리키기만 하고 그것에 대한 정보만 가지고 있기 때문에 파일 크기가 작은 것입니다.

이번에는 심벌릭 링크된 파일을 삭제해 보겠습니다.

[root@leelab /]# rm test1.txt

rm: remove `test1.txt'? y

[root@leelab /]# cat test.txt

this is text file

this is text file

[root@leelab /]#

별다른 반응은 없죠? 이번에는 그 반대의 경우를 생각해 봅시다.

[root@leelab /]# rm test.txt

rm: remove `test.txt'? y

[root@leelab /]# cat test1.txt

cat: test1.txt: 그런 파일이나 디렉토리가 없음

[root@leelab /]#

아마 여러분도 짐작을 하고 있을 것입니다. 가리키고 있는 파일이 삭제되어 존재하지 않은데 그것을 가리키는 파일이 의미가 있겠습니까? 당연히 파일을 찾을 수 없다는 메시지가 나오는 것입니다. 이제 하드링크와 심벌릭 링크의 차이점을 알겠죠?

[목차]

개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.