如何确定linux符号链接是指向目录还是​​文件

时间:2023-01-07 16:26:07

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)

我有一个带有符号链接的目录 - 其中一些指向文件,其中一些指向目录 - 如何识别在shell脚本中存在于目录中的那些(不会对名称偏离任何偏见)

1 个解决方案

#1


1  

use ls -L option to follow symlinks

使用ls -L选项来跟踪符号链接

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

#1


1  

use ls -L option to follow symlinks

使用ls -L选项来跟踪符号链接

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