Bash Script - readlink-all.sh - Follow Filesystem Links

SlowCoder

Gold Member
Gold Supporter
Joined
May 2, 2022
Messages
455
Reaction score
316
Credits
3,611
Sometimes I need to know where a filesystem link points. Sometimes I find that a link points to a link, which then points to a link, and so on, until the final destination file or directory is reached.

The command readlink is good for outputting the next link in a chain of links, or to the final destination file/directory, using the -f option. But why doesn't it show us the whole chain?

So I wrote this quick-n-dirty script to output all links in the chain:
Bash:
if ! [[ -e $1 ]]; then exit; fi

CUR_LINK=$1
DEST="$(readlink -f $1)"
while [[ $CUR_LINK != $DEST ]]; do
    echo "$CUR_LINK"
    CUR_LINK="$(readlink $CUR_LINK)"
done
echo "$DEST"

I suspect most people won't want or need this. But I'm posting my random scripts in the prospect that people may learn or be encouraged to write their own scripts.
 


Hmm...

I wonder if it's worth making a stickied thread - "Linux.org User-created Helpful Shell Scripts"?

People searching would still find the posts just fine. People browsing could browse the thread.
 
Hmm...

I wonder if it's worth making a stickied thread - "Linux.org User-created Helpful Shell Scripts"?

People searching would still find the posts just fine. People browsing could browse the thread.
Creating a single general thread and stickying it would be very cool. I could just do my normal posts, and link them in that thread.
 
Also, feel free to move your existing scripts to the new thread. Giving it some content to get it started may make more folks notice and want/remember to contribute.
 

Members online


Latest posts

Top