Score:3

How can I quickly test a path in bash to determine if any segment of it is a symlink?

us flag

If I have a path like this with a symlink:

/this/one/two/three

Is there is a quick one-liner for determining if one more more segments in the path is a symlink? For example, I'd want to detect if this, one, two, or three in the example above was a symlink.

One way is to compare the path containing the symlink to the output of readlink -f <path>. Wondering if there is a faster way.

Score:6
cn flag

I would use:

if [[ "$my_path" != "$(realpath --canonicalize-existing $my_path)" ]];then
  echo The path $my_path is relative path or contains symlinks.
else
  echo The path $my_path is absolute.
fi
Score:0
eg flag

you can use awk and print for use in this case or other cases , it's very simple and usefully . on this case you can do bellow step : 1 = open ~/.bashrc and add bellow command end of this file

alias issymlink="sh /bin/issymlink.sh $1"

2 = create a file with bellow content in /bin/issymlink.sh

#!/bin/bash
path=$1
type=`stat $1 | awk 'FNR == 2 {print $8;}'`

if [ $type != "symbolic" ]
then
  echo The path $1 is absolute and type of this path is $type
else
  echo The path $1 is symlinks.
fi

3 - logout and login or type su in terminal and enter finally you can type issymlink PATH/TO/FILE/OR/DIRECTORY in terminal and enter to check type of file

example usage and output :

issymlink /lib
The path /lib is absolute and type of this path is directory

issymlink /etc/nginx/sites-enabled/default
The path /etc/nginx/sites-enabled/default is symlinks.
Score:-1
in flag

From: https://unix.stackexchange.com/questions/96907/how-do-i-check-if-a-file-is-a-symbolic-link-to-a-directory

What we see? [[ -L ]] - doing this job for us. Cheers.

# cat script.sh 
#!/bin/bash

/bin/rm -vfr this
/bin/mkdir -vp this/one/two/three

/bin/mv -v this/one/two/three this/one/two/real_three
/bin/ln -vfs real_three this/one/two/three
/bin/ls -altr  this/one/two/

/bin/mv -v this/one/two this/one/real_two
/bin/ln -vfs real_two this/one/two
/bin/ls -altr  this/one/two/
/bin/ls -altr  this/one/

tmp="this/one/two/three"

while [ "$tmp" != "." ]; do
        if [ -L "$tmp" ]; then
                /bin/echo "$tmp - is symlink."
        else
                /bin/echo "$tmp - is not symlink."
        fi
        tmp=$(/bin/dirname $tmp)
done

Usage:

# bash script.sh 
removed 'this/one/two'
removed directory 'this/one/real_two/real_three'
removed 'this/one/real_two/three'
removed directory 'this/one/real_two'
removed directory 'this/one'
removed directory 'this'
/bin/mkdir: created directory 'this'
/bin/mkdir: created directory 'this/one'
/bin/mkdir: created directory 'this/one/two'
/bin/mkdir: created directory 'this/one/two/three'
renamed 'this/one/two/three' -> 'this/one/two/real_three'
'this/one/two/three' -> 'real_three'
total 12
drwxr-xr-x 2 root root 4096 Aug  8 20:08 real_three
drwxr-xr-x 3 root root 4096 Aug  8 20:08 ..
lrwxrwxrwx 1 root root   10 Aug  8 20:08 three -> real_three
drwxr-xr-x 3 root root 4096 Aug  8 20:08 .
renamed 'this/one/two' -> 'this/one/real_two'
'this/one/two' -> 'real_two'
total 12
drwxr-xr-x 2 root root 4096 Aug  8 20:08 real_three
lrwxrwxrwx 1 root root   10 Aug  8 20:08 three -> real_three
drwxr-xr-x 3 root root 4096 Aug  8 20:08 .
drwxr-xr-x 3 root root 4096 Aug  8 20:08 ..
total 12
drwxr-xr-x 3 root root 4096 Aug  8 20:08 ..
drwxr-xr-x 3 root root 4096 Aug  8 20:08 real_two
lrwxrwxrwx 1 root root    8 Aug  8 20:08 two -> real_two
drwxr-xr-x 3 root root 4096 Aug  8 20:08 .
this/one/two/three - is symlink.
this/one/two - is symlink.
this/one - is not symlink.
this - is not symlink.
in flag
While this is not a bad answer, the tone is. Please rewrite your answer to be more matter-of-factly.
us flag
It's not a good answer. It's not even what I was asking.
Michael Hampton avatar
cz flag
@StevieD You asked for a way to determine if a path contained a component that was a symlink. This does that and seems to be 100% accurate. It's just way too verbose in the examples.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.