linuxsymlink-traversal

How to determine if a linux symbolic link points to a directory or a file


I have a directory that has symbolic links - some of them point to files and some of them to directories - how do I identify the ones poiting to directory in a shell script ( without any prejudice to names offcourse)


Solution

  • use ls -L option to follow symlinks

    This is the script that I used to differentiate between directories with contents from files/empty directories ( this will work only if directory has some contents -- in my case I am anyway interested in those directories that have some content so I am happy - but do suggest better options if any

    cd dir
    for i in `ls `
    do
          if [ 1 -lt   `ls -l -L $i  | wc -l`  ]
          then
                  echo "$i is a non empty directory" 
          else
                  echo "$i is either an empty directory or a  file"
          fi
    done