ln 명령어 (AIX 5.3·6.1)
https://archive.horanglab.com/posts/aix-cmd-ln
핵심 정리
- 파일에 새 이름(링크)을 만듭니다. 기본은 하드 링크이고,
-s를 주면 심볼릭 링크를 만듭니다. - 하드 링크는 다른 파일 시스템을 넘을 수 없고, 심볼릭 링크는 파일 시스템을 넘고 디렉터리도 가리킬 수 있습니다.
-f는 이미 있는 대상을 바꾸고,-n(기본)은 덮어쓰지 않습니다.- 대상 이름을 생략하면 현재 디렉터리에 원본과 같은 이름으로 만듭니다(
ln -s /tmp/toc). - 심볼릭 링크의 원본은 절대 경로로 쓰는 것이 안전하고, 원본이 아직 없어도 만들 수 있습니다.
- AIX 5.3과 6.1 문서의 내용이 같습니다.
| 항목 | 내용 |
|---|---|
| 버전 | AIX 5.3, AIX 6.1 (두 버전 내용 같음) |
| 원문 | IBM Commands Reference — 5.3 Volume 3, 6.1 Volume 3 |
| 파일 | /usr/bin/ln |
| 관련 명령어 | rm, link, symlink |
목적
파일을 링크합니다.
영어 원문
Links files.
구문
파일을 파일로 링크할 때:
ln [ -f | -n ] [ -s ] SourceFile [ TargetFile ]
파일 하나 이상을 디렉터리로 링크할 때:
ln [ -f | -n ] [ -s ] SourceFile ... TargetDirectory
설명
ln 명령은 SourceFile 매개변수로 지정한 파일을, TargetFile 매개변수로 지정한 파일이나 TargetDirectory 매개변수로 지정한 다른 디렉터리의 같은 파일 이름으로 링크합니다. 기본으로 하드 링크를 만들며, 심볼릭 링크를 만들려면 -s 플래그를 지정합니다.
심볼릭 링크는 파일을 간접적으로 가리키는 포인터로, 그 디렉터리 항목에는 링크된 파일의 이름이 들어 있습니다. 심볼릭 링크는 파일 시스템을 넘을 수 있고 디렉터리를 가리킬 수도 있습니다.
파일을 새 이름으로 링크할 때는 파일을 하나만 지정할 수 있습니다. 디렉터리로 링크할 때는 여러 파일을 지정할 수 있습니다.
TargetFile 매개변수는 선택입니다. 대상 파일을 지정하지 않으면 현재 디렉터리에 새 파일을 만들며, 새 파일은 SourceFile의 이름을 물려받습니다(예제 5 참고).
참고:
-s플래그 없이는 다른 파일 시스템에 걸쳐 파일을 링크할 수 없습니다.- TargetDirectory가 이미 디렉터리를 가리키는 심볼릭 링크면
ln은 기존 대상을 파일로 다룹니다. 그래서ln -fs somepath/lname symdir같은 명령은symdir의 기존 심볼릭 링크를 따라가지 않고,somepath/lname을 가리키는 새 심볼릭 링크를symdir로 만듭니다.
영어 원문
The ln command links the file designated in the SourceFile parameter to the file designated by the TargetFile parameter or to the same file name in another directory specified by the TargetDirectory parameter. By default, the ln command creates hard links. To use the ln command to create symbolic links, designate the -s flag.
A symbolic link is an indirect pointer to a file; its directory entry contains the name of the file to which it is linked. Symbolic links may span file systems and may refer to directories.
If you are linking a file to a new name, you can list only one file. If you are linking to a directory, you can list more than one file.
The TargetFile parameter is optional. If you do not designate a target file, the ln command creates a new file in your current directory. The new file inherits the name of the file designated in the SourceFile parameter. See example 5.
Notes:
You cannot link files across file systems without using the
-sflag.If TargetDirectory is already a symbolic link to a directory, then the ln command treats the existing target as a file. This means that a command such as ln -fs somepath/lname symdir will not follow the existing symbolic link of symdir, instead it will create a new symbolic link from somepath/lname to symdir.
플래그
| 플래그 | 설명 |
|---|---|
-f |
이미 있는 대상 경로를 바꿉니다. 대상 경로가 이미 있는데 -f가 없으면, 새 링크를 만들지 않고 표준 오류에 진단 메시지를 쓴 뒤 나머지 SourceFile을 계속 링크합니다. |
-n |
링크가 이미 있는 파일이면 그 파일의 내용을 덮어쓰지 않습니다. -f가 이 플래그보다 우선합니다. 기본 동작입니다. |
-s |
심볼릭 링크를 만듭니다(아래 설명). |
-s(심볼릭 링크)
- 심볼릭 링크에는 링크된 파일의 이름이 들어 있습니다. 링크를 열면 가리키는 파일이 쓰입니다.
- 심볼릭 링크에
stat을 부르면 가리키는 파일의 정보를 돌려주며, 링크 자체의 정보는lstat으로 얻습니다. 심볼릭 링크의 내용은readlink로 읽습니다. - 심볼릭 링크는 파일 시스템을 넘을 수 있고 디렉터리를 가리킬 수 있습니다.
- 참고:
-s의 SourceFile은 절대 경로로 지정해야 합니다. 그렇지 않으면 SourceFile과 TargetFile이 다른 디렉터리에 있을 때 예상하지 못한 결과가 날 수 있습니다. 원본 파일은 심볼릭 링크를 만들기 전에 없어도 됩니다.
영어 원문
-f— Causes the ln command to replace any destination paths that already exist. If a destination path already exists and the-fflag is not specified, the ln command writes a diagnostic message to standard error without creating a new link and continues to link the remaining SourceFiles.-n— Specifies that if the link is an existing file, do not overwrite the contents of the file. The-fflag overrides this flag. This is the default behavior.-s— Causes the ln command to create symbolic links. A symbolic link contains the name of the file to which it is linked. The referenced file is used when an open operation is performed on the link. A stat call on a symbolic link returns the linked-to file; an lstat call must be done to obtain information about the link. The readlink call may be used to read the contents of a symbolic link. Symbolic links can span file systems and refer to directories. Note: Absolute path names must be used when specifying the SourceFile parameter for the-sflag. If the absolute path name is not designated, unexpected results may occur when the SourceFile and the TargetFile parameters are located in different directories. The source file does not need to exist before creating the symbolic link.
종료 상태
| 값 | 뜻 |
|---|---|
| 0 | 지정한 파일을 모두 잘 링크했습니다. |
| 0보다 큼 | 오류가 났습니다. |
영어 원문
This command returns the following exit values:
- 0 — All specified files were successfully linked.
>0— An error occurred.
예제
- 파일에 다른 링크(별칭)를 만듭니다.
chap1을 새 이름intro로 링크합니다.intro가 없으면 만들고, 있으면chap1에 대한 링크로 바꿉니다. 그러면chap1과intro두 이름이 같은 파일을 가리키며, 한쪽에서 바꾼 것이 다른 쪽에도 나타납니다.rm으로 이름 하나를 지워도 다른 이름으로 남아 있으므로 파일이 완전히 지워지지는 않습니다.
ln -f chap1 intro
- 파일을 다른 디렉터리의 같은 이름으로 링크합니다.
index가 새 이름manual/index로 링크됩니다. 예제 1의intro는 파일 이름이고, 예제 2의manual은 이미 있는 디렉터리입니다.
ln index manual
- 여러 파일을 다른 디렉터리의 이름으로 링크합니다.
chap2는/home/manual/chap2로,jim/chap3은/home/manual/chap3으로 링크됩니다.
ln chap2 jim/chap3 /home/manual
- 패턴 일치 문자와 함께 씁니다.
manual디렉터리의 모든 파일을 같은 이름으로 현재 디렉터리(.)에 링크합니다. 별표와 마침표 사이에 공백이 있어야 합니다.
ln manual/* .
- 심볼릭 링크를 만듭니다. 현재 디렉터리에
/tmp/toc를 가리키는 심볼릭 링크toc를 만듭니다./tmp/toc가 있으면cat toc가 그 내용을 보여 줍니다.
ln -s /tmp/toc toc
TargetFile 없이 같은 결과를 얻으려면 다음과 같이 입력합니다.
ln -s /tmp/toc
영어 원문
- To create another link (alias) to a file, enter:
ln -f chap1 intro
This links chap1 to the new name, intro. If intro does not already exist, the file name is created. If intro does exist, the file is replaced by a link to chap1. Then both the chap1 and intro file names will refer to the same file. Any changes made to one also appear in the other. If one file name is deleted with the rm command, the file is not completely deleted since it remains under the other name.
- To link a file to the same name in another directory, enter:
ln index manual
This links index to the new name, manual/index.
Note: intro in example 1 is the name of a file; manual in example 2 is a directory that already exists.
- To link several files to names in another directory, enter:
ln chap2 jim/chap3 /home/manual
This links chap2 to the new name /home/manual/chap2 and jim/chap3 to /home/manual/chap3.
- To use the ln command with pattern-matching characters, enter:
ln manual/* .
This links all files in the manual directory into the current directory, . (dot), giving them the same names they have in the manual directory.
Note: You must type a space between the asterisk and the period.
- To create a symbolic link, enter:
ln -s /tmp/toc toc
This creates the symbolic link, toc, in the current directory. The toc file points to the /tmp/toc file. If the /tmp/toc file exists, the cat toc command lists its contents.
To achieve identical results without designating the TargetFile parameter, enter:
ln -s /tmp/toc
파일
| 파일 | 설명 |
|---|---|
/usr/bin/ln |
ln 명령이 들어 있습니다. |
관련 명령어: rm, link, symlink
영어 원문
- /usr/bin/ln — Contains the ln command.
Related information:
- rm command
- link command
- symlink command
- Linking for Programmers
리눅스(GNU)와 다른 점
| 항목 | AIX 5.3·6.1 | GNU ln |
|---|---|---|
-n |
기존 파일을 덮어쓰지 않음(기본 동작) | 디렉터리를 가리키는 링크를 일반 파일처럼 다룸 |
| 디렉터리 심볼릭 링크 대상 | 대상을 파일로 다룸(참고 2) | -n이나 -T를 줘야 같은 동작 |
AIX 5.3·6.1 문서에는 -r(상대 경로 링크), -v, -b(백업), -T 같은 GNU 플래그가 없습니다.