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
Niets past bij dit filter.
Voorbeelden 11
$ als gewone gebruiker# als root (of met sudo)
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.confThe
land the arrow show the link and its target.2Make a hard link
ln data.txt data-copy.txt ls -li data.txt data-copy.txtSame inode number, link count 2. Both names are the same file.
3Replace an existing link (switch a release)
ln -sfn /srv/app/releases/43 /srv/app/current-freplaces,-ntreats the old link as a file (not as the folder it points to).4Make a relative link that survives moving the tree
ln -sr /srv/app/shared/config.yml /srv/app/current/config.ymlThe link stores a path like ../shared/config.yml.
5Link a program into your PATH
ln -s /opt/tool/bin/tool ~/.local/bin/toolNow you can run
toolfrom anywhere.6Find broken links
find /etc -xtype lLinks whose target is gone. Clean them after removing software.
7Show where a link points
readlink -f /usr/bin/python3-ffollows all links to the final file.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/logsThe app still writes to the old path. Check SELinux labels on the new place.
9Make several links in a folder
ln -s -t /usr/local/bin /opt/tools/bin/*One link per program in /usr/local/bin.
10See how many names a file has
stat -c '%h %n' /etc/passwd%h= the number of hard links.11Show each link that is made
ln -sv /etc/os-release ~/os '/home/student/os' -> '/etc/os-release'Clear output for scripts and logs.
Niets past bij dit filter.
Let op
- The order is TARGET then LINK. Swapping them is the most common mistake.
- A relative target in
ln -sis relative to the link's folder, not to your current folder. Use-ror 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.