You could modify your script and use grep
and either readlink
or realpath
to extract the files in the desired subdirectory, e.g.:
for z in /usr/local/bin/*
do
if [ -h "${z}" ] && readlink -f "${z}" | grep -q '^/home/Steven'
then rm "${z}"
fi
done
The readlink -f
command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven'
command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.
A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f
command will resolve the link recursively, so the above command would fail if the file in the /home/Steven
directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath
command instead.