Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Recently the script was working fine, but from some days I'm receiving such message, while running the readlink -f "$0" command:

readlink: illegal option -- f
usage: readlink [-n] [file ...]

I was running the following code to debug:


RLPATH=`which readlink`
RLOUT=`readlink -f -- "${0}"`
DIROUT=`dirname -- ${RLOUT}`

echo "dir: ${DIR}"
echo "path: ${PATH}"
echo "path to readlink: ${RLPATH}"
echo "readlink output: ${RLOUT}"
echo "dirname output: ${DIROUT}"


# ./
readlink: illegal option -- f
usage: readlink [-n] [file ...]
usage: dirname string [...]
dir: /home/svr
path: /sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
path to readlink: /usr/bin/readlink
readlink output:
dirname output:

What is wrong ?

share|improve this question
May be readlink binary was overwritten? Check, that stat and readlink has same inode: ls -li /usr/bin/readlink /usr/bin/stat – citrin Dec 7 '12 at 10:45
If you wan to know real directory, where script located, better to use: dirname $(realpath "$0") – citrin Dec 7 '12 at 10:47
@citrin Yes: 1083670 -r-xr-xr-x 2 root wheel 16760 May 4 2011 /usr/bin/readlink 1083670 -r-xr-xr-x 2 root wheel 16760 May 4 2011 /usr/bin/stat – Scott Dec 7 '12 at 11:04
@citrin # dirname $(realpath "$0") Illegal variable name. – Scott Dec 7 '12 at 11:52
8.2 has the -f option, so your stat binary is modified. I suggest to rebuild and install it: cd /usr/src/usr/bin/stat && make install – arved Dec 10 '12 at 11:13

This is an issue with the version of FreeBSD that you're running. Older versions do not support the -f option.

The easiest way to get around this is to use the GNU coreutils port (/usr/ports/sysutils/coreutils) that will give you "greadlink". This is what I use in my scripts.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.