Commands
-sfnmatters when replacing a symlink to a directory: without-n,lndrops the new link INSIDE the existing target directory (see ln).
The difference between a symlink and a hard link, when to use which, broken links.
ln -s target link
symbolic (the usual kind)
ln -sfn target linkdir
recreate a symlink (to a directory — needs -n!)
readlink -f link
the real target
find . -xtype l
broken symlinks
ln target hardlink
hard link (same filesystem, same inode)
cp -P link dst
keep the link itself when copying
cp -a src dst
archive mode: links stay as links
There are two different kinds of "link" (the command is ln):
-sfnmatters when replacing a symlink to a directory: without-n,lndrops the new link INSIDE the existing target directory (see ln).
Symlink (-s) |
Hard | |
|---|---|---|
| Points to a directory | yes | no |
| Across filesystems | yes | no |
| Target deleted | "broken" | data survives as long as any name remains |
Shown in ls -l |
-> target |
looks like a regular file |
| Own inode | yes | no (shares the file's) |
Use cases: symlink — a "current → release" deploy pattern, switching
configs, shared libraries. hard link — deduplication within a
filesystem (snapshot backups, see rsync --link-dest).
The target was deleted or moved. Find it with find . -xtype l. Recreate it
pointing at the right target (ln -sf). ls highlights broken ones in red.
cp follows a symlink by default (it copies the target). cp -P keeps
the link itself; cp -a/-d do it archive-style (links stay as links).
tar stores symlinks as-is; tar -h dereferences them.
Hard links share the same blocks — du counts them once (which is
confusing). That's normal; du --count-links counts every name.
Without -H, rsync doesn't preserve hard links (it copies the data again
for each name). For deduplicated snapshots, use --link-dest (see
rsync).
Found an inaccuracy? Report an error in the article