Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

I have some software that, among other things, needs to:

  • Assess a file's rwxrwxrwx permissions;
  • Work under every possible flavor of Unix and Linux you can find in the wild.

Currently, it does that running the ls -l command. If the file is a symlink, I have to get the permissions of the target file. The -L switch works nicely for that.

The question: Are there flavors of Unix in which I run the risk of that switch not being available? If so, which ones? (If they're rare enough and old enough, I might just be able to ignore the problem.)

share|improve this question
up vote 2 down vote accepted

Well, it's in the GNU, FreeBSD, OpenBSD, and general BSD ls implementations, so I'd say it's pretty likely to be anywhere.

share|improve this answer
I think I remember it being in System V from 20 years ago too. – Mark Ransom Nov 29 '11 at 17:05
Also present on SunOS 5.7 (which is often very good at not having useful switches). – Ulrich Schwarz Nov 30 '11 at 5:55

Don't parse ls if you can help it. Depending on what you're doing, the test utility might be a better alternative.

test -r filename will return success if the file is readable with the current user's permissions, and will follow symbolic links automatically. Likewise, -w and -x will tell you if the file is writable or executable. The test command is specified by POSIX, and so ought to work with any modern UNIX.

If you really need to know the specific permissions of the file, ls might be the most portable way, as long as you only use it on one file at a time. In that case, the -L and -l flags are also specified by POSIX, so you should be reasonably safe. (See "Don't Parse ls" for a little more commentary on the portabilty of the output fields of ls -l.)

Another option might be to write a short C program to take advantage of the stat(2) system call, which will be available on every UNIX under the sun. If this is a small part of a larger piece of software that's already being compiled from C, this might be the best option.

share|improve this answer
i have tried the test program on my system and it seems that it isn't work, it doesn't output anything (even not to sterr). Archlinux – Hanan N. Nov 29 '11 at 21:50
While “don't parse the output of ls” is good advice in general, the specific case of parsing ls -l on a single file when you're after the permissions or link count is safe. – Gilles Nov 29 '11 at 23:20
@HananN. test doesn't produce any output unless you make a syntax error. It returns information through its exit status: 0 if the test is true, ≥1 if the test is false. – Gilles Nov 29 '11 at 23:21

It is in SUSv3 (Posix 2001) but not in SUSv2 (alias Unix98).

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.