Skip to content
Search

Ln

lastmod 2023-02-28created 2015-04-207 edits

ln, short for Link, is a command that creates link files in the Linux file system. Linux has two kinds of link files: symbolic links and hard links.

Syntax

$ ln [옵션] {원본파일} {대상파일}

It simply links to point at the original file, like the 'shortcut' commonly used in Microsoft's Windows systems; since it only points to the original file, it is unrelated to the original file's size. Also, with a symbolic link, if the original file is deleted and no longer exists, the link file blinks to indicate that its original file is missing.

ln -s {원본 파일} {새로만들 심볼릭 링크 파일}

One of the confusing parts when reading it with man:

$ ln --help
Usage: ln [OPTION]... [-T] TARGET LINK_NAME
  or:  ln [OPTION]... TARGET
  or:  ln [OPTION]... TARGET... DIRECTORY
  or:  ln [OPTION]... -t DIRECTORY TARGET...
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist.  Symbolic links
can hold arbitrary text; if later resolved, a relative link is
interpreted in relation to its parent directory.

Just look at the first line.

In the 1st form, create a link to TARGET with the name LINK_NAME.

Creates a link to TARGET named LINK_NAME.

It is the same file existing under a different name from the original, and can be described as a different file with the same content as the original. With a hard link, the original file and the link file are two different files, so even if one is deleted, the other remains. Also, with a hard link, when the content of the original file changes, the content of the link file changes automatically as well.

ln hard_source hard_link 

Flags

  • --backup[=CONTROL]: if the target file already exists, makes a backup file and then creates the link file
  • -b: when creating the link file, if the target file already exists, makes a backup file and then creates the link file
  • -d: allows creating hard link files for directories. However, even when run with Root privileges, it is likely to fail due to system permission restrictions. (-F and --directory are the same as -d)
  • -f: if the target file exists, deletes it and creates the link file
  • -i: if the target file exists, asks for confirmation before deleting it (same as --interactive)
  • -s: creates a symbolic link file
  • -S: lets you specify the desired suffix when creating a backup file
  • -t, --target-directory=DIRECTORY: specifies the directory in which to create the link file

Favorite site

References


  1. Kimjinkyung.tistory.com_-_Symbolic_link_and_Hard_link.pdf ↩

author Zer0 sitemap