CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / ln

ln

Create hard links and symbolic links

Intermediatefilesysteminodedeploys

ln gives a file another name. A hard link is a second name for the same inode; a symbolic link (-s) is a tiny file that stores a path. Symlinks are everywhere in operations: 'current' release folders, sites-enabled in nginx, /etc/alternatives, and versioned binaries.

Syntaxln [OPTION]... TARGET LINK_NAME ln -s TARGET LINK_NAME

Watch it run

You want a short, stable name for a file that lives deep in a directory tree.

devops@devops-lab: ~/app — zsh — 96×28
➜ ~/app
Step 1 of 5
ln -s config/app.yaml app.yaml && ls -l app.yaml

The l type and the arrow mark a symlink. Its size, 15, is the length of the stored path 'config/app.yaml'.

Anatomy of a command

$
prefixsudo

Root is needed because the link is created in /opt/app, a root-owned directory.

How it works

A directory entry is just a name pointing to an inode number. A hard link (ln with no -s) calls link(), which adds a second entry pointing to the same inode and increments its link count; both names are equal, there is no 'original'. The data survives until the link count reaches 0 and no process has the file open. Because inode numbers are only unique within one filesystem, hard links can't cross filesystems (EXDEV), and they aren't allowed on directories to keep the tree free of loops. A symbolic link (ln -s) calls symlink(), which creates a new inode of type link whose content is the target path, stored as text and not checked at creation. Every time a program opens the symlink, the kernel reads that text and resolves it, relative to the link's own directory if it doesn't start with /. So symlinks can point across filesystems and to directories, and they break (dangle) when the target is moved or deleted. ln -sf replaces a link by unlinking and recreating it, which is not atomic.

A hard link is another name for the same inode; a symlink is a separate file that stores a path and is resolved by name.

inode no.block mapapp.yamldirectory entryapp-hard.yamlhard linkcurrent.yamlsymlinkInode 1048624links: 1Data blocksport: 8080 ...Inode 1048700text: app.yaml
1/6
A file is a name plus an inode

The directory entry app.yaml holds an inode number. The inode holds owner, mode, size, the link count and where the data blocks are.

← → to step

Options you'll actually use

-sCreate a symbolic link instead of a hard link
-fRemove an existing destination first
-nTreat a symlink-to-directory LINK_NAME as a file (replace it)
-TAlways treat LINK_NAME as a file name, never a directory
-rMake the symlink's target relative to the link's location
-vPrint each link created
-iAsk before replacing an existing destination
readlink -f LINKShow the fully resolved target of a link

Common mistakes

ln -s current releases/v42 (arguments swapped)

ln takes TARGET first and the new LINK_NAME second, like cp. Swapped, you create a link named v42 inside releases pointing at 'current'.

ln -s releases/v42 current (think: ln -s WHAT WHERE)
ln -sf releases/v42 current when current links to a directory

ln follows the existing link and creates releases/v41/v42 inside the old release; current doesn't change.

ln -sfn releases/v42 current (or ln -s ... tmp && mv -T tmp current for an atomic switch)
ln -s app.yaml /etc/app/app.yaml from inside your project directory

A relative target is resolved from the link's directory (/etc/app), not from where you ran ln, so the link dangles.

ln -s "$PWD/app.yaml" /etc/app/app.yaml or ln -sr
chmod 600 on a symlink to protect the target

Symlink permissions are always rwxrwxrwx and ignored; chmod on a link changes the target's mode.

Set permissions on the target file itself

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a symlink ~/conf pointing at /etc/app/app.conf, and read the config through it.

  2. 2

    Make a hard link ~/data/orders-link.csv to ~/data/orders.csv and prove both names share one inode.

  3. 3

    Find the broken symlink in ~/app and see what it points to.

  4. 4

    Create ~/rel/v1 and ~/rel/v2, point ~/rel/current at v1, then switch it to v2 correctly.

Related commands

lsList directory contentsstatShow a file's inode: size, permissions, owner and timestampsfindSearch the filesystem by name, size, age, owner and morermRemove files and directoriesmvMove or rename files and directories
PreviousstatNextcat
Linux / File & Directory Management / ln

ln

Create hard links and symbolic links

Intermediatefilesysteminodedeploys

ln gives a file another name. A hard link is a second name for the same inode; a symbolic link (-s) is a tiny file that stores a path. Symlinks are everywhere in operations: 'current' release folders, sites-enabled in nginx, /etc/alternatives, and versioned binaries.

Syntaxln [OPTION]... TARGET LINK_NAME ln -s TARGET LINK_NAME

Watch it run

You want a short, stable name for a file that lives deep in a directory tree.

devops@devops-lab: ~/app — zsh — 96×28
➜ ~/app
Step 1 of 5
ln -s config/app.yaml app.yaml && ls -l app.yaml

The l type and the arrow mark a symlink. Its size, 15, is the length of the stored path 'config/app.yaml'.

Anatomy of a command

$
prefixsudo

Root is needed because the link is created in /opt/app, a root-owned directory.

How it works

A directory entry is just a name pointing to an inode number. A hard link (ln with no -s) calls link(), which adds a second entry pointing to the same inode and increments its link count; both names are equal, there is no 'original'. The data survives until the link count reaches 0 and no process has the file open. Because inode numbers are only unique within one filesystem, hard links can't cross filesystems (EXDEV), and they aren't allowed on directories to keep the tree free of loops. A symbolic link (ln -s) calls symlink(), which creates a new inode of type link whose content is the target path, stored as text and not checked at creation. Every time a program opens the symlink, the kernel reads that text and resolves it, relative to the link's own directory if it doesn't start with /. So symlinks can point across filesystems and to directories, and they break (dangle) when the target is moved or deleted. ln -sf replaces a link by unlinking and recreating it, which is not atomic.

A hard link is another name for the same inode; a symlink is a separate file that stores a path and is resolved by name.

inode no.block mapapp.yamldirectory entryapp-hard.yamlhard linkcurrent.yamlsymlinkInode 1048624links: 1Data blocksport: 8080 ...Inode 1048700text: app.yaml
1/6
A file is a name plus an inode

The directory entry app.yaml holds an inode number. The inode holds owner, mode, size, the link count and where the data blocks are.

← → to step

Options you'll actually use

-sCreate a symbolic link instead of a hard link
-fRemove an existing destination first
-nTreat a symlink-to-directory LINK_NAME as a file (replace it)
-TAlways treat LINK_NAME as a file name, never a directory
-rMake the symlink's target relative to the link's location
-vPrint each link created
-iAsk before replacing an existing destination
readlink -f LINKShow the fully resolved target of a link

Common mistakes

ln -s current releases/v42 (arguments swapped)

ln takes TARGET first and the new LINK_NAME second, like cp. Swapped, you create a link named v42 inside releases pointing at 'current'.

ln -s releases/v42 current (think: ln -s WHAT WHERE)
ln -sf releases/v42 current when current links to a directory

ln follows the existing link and creates releases/v41/v42 inside the old release; current doesn't change.

ln -sfn releases/v42 current (or ln -s ... tmp && mv -T tmp current for an atomic switch)
ln -s app.yaml /etc/app/app.yaml from inside your project directory

A relative target is resolved from the link's directory (/etc/app), not from where you ran ln, so the link dangles.

ln -s "$PWD/app.yaml" /etc/app/app.yaml or ln -sr
chmod 600 on a symlink to protect the target

Symlink permissions are always rwxrwxrwx and ignored; chmod on a link changes the target's mode.

Set permissions on the target file itself

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a symlink ~/conf pointing at /etc/app/app.conf, and read the config through it.

  2. 2

    Make a hard link ~/data/orders-link.csv to ~/data/orders.csv and prove both names share one inode.

  3. 3

    Find the broken symlink in ~/app and see what it points to.

  4. 4

    Create ~/rel/v1 and ~/rel/v2, point ~/rel/current at v1, then switch it to v2 correctly.

Related commands

lsList directory contentsstatShow a file's inode: size, permissions, owner and timestampsfindSearch the filesystem by name, size, age, owner and morermRemove files and directoriesmvMove or rename files and directories
PreviousstatNextcat