I want to determine all broken symlinks within a directory.
In ignorance of a better solution, I tried this simple bash one-liner (bash version is 4.2.46, which may be worth mentioning because it's a bit dated):
for f in $(ls); do test -L "$f" || continue; test -e "$f" || echo "$f deadlink"; done
Unfortunately this does not work. I tried nailing down the error and if I run this
for f in $(ls); do test -L $f && echo "$f is a valid symlink"; done
no output is produced, despite existing and valid symlinks. If I run the same command with $f
replaced with the actual file name, it works. Also f=filename; test -L $f && echo "$f is a valid symlink";
works as expected, so the problem seems to be the loop.
What am I missing?
$(ls)
with$(ls --color=never)
, it works as well (alternatively:$(\ls)
to avoid alias expansion). Thanks for the idea of using a simple glob as well. Question answered. – andreee Mar 03 '23 at 12:47