Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

ln

Make links: hard links (a second name for the same data) or symbolic links (a pointer to a path).

Niveau
RHCSA-examen
Groep
Bestanden en mappen
Root nodig
Nee
Pakket
coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

  • A hard link is another directory entry for the same inode. Both names are equal; the data stays until the last name is removed. Only for files, and only inside one file system.
  • A symbolic (soft) link is a small file that contains a path. It can point to folders and to other file systems. If the target is removed, the link is broken.

Remember the order: ln -s TARGET LINKNAME — first what exists, then the new name (like cp).

Zo schrijf je het

ln [OPTION]... TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...

Opties 16

Meest gebruikt

-s, --symbolic
Make a symbolic link instead of a hard link.
-f, --force
Remove an existing target name first.
-n, --no-dereference
If LINK_NAME is a link to a folder, treat it as a normal file (use with -f to replace the link).
-r, --relative
With -s: make the link path relative to the link's place.
-i, --interactive
Ask before removing an existing name.
-v, --verbose
Print each link made.
-t, --target-directory=dir
Make the links in this folder.
-T, --no-target-directory
Treat LINK_NAME as a normal file always.
-L, --logical
For a hard link to a symlink: link to the file the symlink points to.
-P, --physical
For a hard link to a symlink: link to the symlink itself.
-d, -F, --directory
Let root try to hard link folders (almost always fails).
--backup[=<control>]
Make a backup of each existing name.
-b
Like --backup without an argument.
-S, --suffix=suffix
The ending of backup files.
--help
Show help.
--version
Show the version.

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1Make a symbolic link

    ln -s /etc/httpd/conf/httpd.conf ~/httpd.conf
    ls -l ~/httpd.conf
    lrwxrwxrwx. 1 student student 26 Oct 25 10:00 /home/student/httpd.conf -> /etc/httpd/conf/httpd.conf

    The l and the arrow show the link and its target.

  2. 2Make a hard link

    ln data.txt data-copy.txt
    ls -li data.txt data-copy.txt

    Same inode number, link count 2. Both names are the same file.

  3. 3Replace an existing link (switch a release)

    ln -sfn /srv/app/releases/43 /srv/app/current

    -f replaces, -n treats the old link as a file (not as the folder it points to).

  4. 4Make a relative link that survives moving the tree

    ln -sr /srv/app/shared/config.yml /srv/app/current/config.yml

    The link stores a path like ../shared/config.yml.

  5. 5Link a program into your PATH

    ln -s /opt/tool/bin/tool ~/.local/bin/tool

    Now you can run tool from anywhere.

  6. 6Find broken links

    find /etc -xtype l

    Links whose target is gone. Clean them after removing software.

  7. 7Show where a link points

    readlink -f /usr/bin/python3

    -f follows all links to the final file.

  8. 8Keep log files of an app on a bigger disk

    mv /var/lib/app/logs /data/app-logs && ln -s /data/app-logs /var/lib/app/logs

    The app still writes to the old path. Check SELinux labels on the new place.

  9. 9Make several links in a folder

    ln -s -t /usr/local/bin /opt/tools/bin/*

    One link per program in /usr/local/bin.

  10. 10See how many names a file has

    stat -c '%h %n' /etc/passwd

    %h = the number of hard links.

  11. 11Show each link that is made

    ln -sv /etc/os-release ~/os
    '/home/student/os' -> '/etc/os-release'

    Clear output for scripts and logs.

Let op

  • The order is TARGET then LINK. Swapping them is the most common mistake.
  • A relative target in ln -s is relative to the link's folder, not to your current folder. Use -r or an absolute path.
  • Hard links do not work across file systems ("Invalid cross-device link") or to folders.
  • A normal user cannot hard link a file of another user (kernel protection fs.protected_hardlinks).

Tips om te onthouden

  • ln -s = shortcut (soft). ln = second name (hard).
  • ln -sfn = replace a link safely.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man ln