brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -lname finds symlinks by where they point

Find
~/tips/tip-find-lname-symlink-target

find / -xdev -lname '/mnt/olddisk/*' -ls 2>/dev/null

Lists the symbolic links that point to a certain path, for example before you move a folder.

Before you move or rename a folder, it pays to know which symlinks point into it. They’ll break, and find can list them.

find / -xdev -lname '/mnt/olddisk/*' -ls 2>/dev/null

-lname matches the target of a link, the text that ls -l shows after the arrow, against a pattern. Normal files never match. -ilname does the same without caring about upper and lower case.

The pattern works on the text that is stored in the link, not on the real path. That’s the catch. A link created as ln -s ../a/f.txt stores the relative text ../a/f.txt, so a pattern with the full path won’t find it. For example, -lname '*f.txt' finds a relative link that a pattern with the absolute path would miss. When you’re not sure how the links were made, I’d use a wide pattern like '*olddisk*', which catches both kinds.

-xdev keeps the search on one filesystem, and the redirect hides permission errors. If you want the links whose target is already gone, that’s a different test: -xtype l.

Further reading: find(1) manual page, GNU findutils manual

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link