0

I am not a powere shell script programmer this is probably basic thnk but I need to know how can I check if the provided path is a symlink or a regular path ?

I have a path /user/home/san/drive2

where drive2 is a symlink to

/drive at root level, so using what command in shell script I can check that the path is a symlink or regular path ?

Also, one can encounter, symlink at any path so how can we check recursively after each path separator whether it is a symlink or not ?

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
  • possible duplicate of [How to check if symlink exists](http://stackoverflow.com/questions/5767062/how-to-check-if-symlink-exists) – shx2 Apr 11 '14 at 05:30

2 Answers2

0

Look out the lstat() function.

In this check st_mode field: S_ISLNK(m)

Or you can do something like

if [ -L /path/to/file ]; then
  echo "is a symlink!"
else
  echo "not a symlink! maybe a directory or regular file, or does not exist"
end
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
0

You can use readlink -f /user/home/san/drive2 to get the canonical path behind. That is, the actual path without any symlink. A corollary is that if the result of readlink -f is any different from the argument you've given to it, then there's a symlink somewhere in the original path.

Arkanosis
  • 2,229
  • 1
  • 12
  • 18