I need to make a list of all direct symbolic links in a directory, i.e. symbolic links that point to another file which is not a symbolic link.
I tried to do it this way:
for i in $(ls -1A); do
first_type=$(ls -l $i | cut -b 1)
if [ $first_type == 'l' ]
then
next=$(ls -l $i | awk '{print $NF}')
next_type=$(ls -l $next | cut -b 1)
if [ $next_type != 'l' ]
then
#Some code
fi
fi
done
But in this case, the script skips files whose names have spaces/tabs/newlines (including at the start and end of the file name). Is there any way to solve this?
I working on Solaris 10. There is no readlink
or stat
command. The find
command does not have -printf
.
next=$(ls -l $i | grep '.*-> ' | sed 's/.*-> //')
gives desired result, but it looks not optimal – iRomul Apr 22 '15 at 23:50grep
and usesed -n 's/.*-> //p'
. Unfortunately it still fails if the filename contains->
or a newline. – Chris Davies Apr 23 '15 at 09:35ln -s /etc/hosts sym ; perl -e 'print readlink,"\n" foreach @ARGV' sym
will give you approximations toreadlink
– Chris Davies Apr 24 '15 at 10:44