Apparently I am not allowed to comment, so here as an answer:
Since a
is the link,
we need the recursive option on the dereferenced directory...
rm -rf "$(readlink -f "a")"; rm -f "a"
Jim's answer explains how to test for a symlink: by using test
's -L
test.
But testing for a "hard link" is, well, strictly speaking not what you want. Hard links work because of how Unix handles files: each file is represented by a single inode. Then a single inode has zero or more names or directory entries or, technically, hard links (what you're calling a "file").
Thankfully, the stat
command, where available, can tell you how many names an inode has.
So you're looking for something like this (here assuming the GNU or busybox implementation of stat
):
if [ "$(stat -c %h -- "$file")" -gt 1 ]; then
echo "File has more than one name."
fi
The -c '%h'
bit tells stat
to just output the number of hardlinks to the inode, i.e., the number of names the file has. -gt 1
then checks if that is more than 1.
Note that symlinks, just like any other files, can also be linked to several directories so you can have several hardlinks to one symlink.
Best Answer
.
is actually the current working directory in either case; it has nothing to do with the directory holding the script: