3

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.

StevieD
  • 514
  • 8
  • 24

3 Answers3

6

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
Mircea Vutcovici
  • 17,619
  • 4
  • 56
  • 83
0

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.
-1

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.
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972