The lowly ls command has a lot of flags, perhaps more than any other
Unix command. The man page covers them all (or does it? - read on!), but how many of us have read
that man page from start to finish?
That man page may not even have been
read at all - if you cut your teeth on Solaris or SCO Unix, did you bother to
read up on "ls" when you switched to Linux or Mac OS X? Perhaps not, but perhaps you should.
"ls" flags and options vary widely
Although there is great commonality across Unixish operating systems, there are important differences. An old
Unix hand switching to Linux or Mac OS X will find that most things are
exactly as they expect, but if they don't bother to read the man pages, they won't know about things they perhaps would find useful.
On Linux, you also need to check "info ls" (or "pinfo" if you prefer). That's because "info" may give you more information or tell you about features "man" omits entirely. As an example, compare these parts from "man ls" and "info" ls
on this Linux system:
(from "man ls")
with -l, print the author of each file
(from "info ls")
List each file's author when producing long format directory
listings. In GNU/Hurd, file authors can differ from their owners,
but in other operating systems the two are the same.
On Mac, info seems to use the man pages - "info -w ls" returns "*manpages*, so there's no difference.
Unless you happen to be running GNU Hurd, you aren't likely to care about
"--author". However, there are more options of more value.
One flag I was late to discover is "-h" (human readable). This
is common now, but it did not exist when I first learned Unix. It
is used with "-l":
People like commas in file sizes. The default "ls" shows sizes
without commas so that other programs can perform simple integer math without
stripping commas. If you WANT commas, you can get them on Linux (but not Mac OS X as of this time) with:
That's a single quote followed by a numeric one, all enclosed in double quotes - wouldn't a "," have been a bit more inuitive?
Unfortunately, you can't combine -h or --si with that (at least not as I write this).
(Warning - that adds significant time to a directory listing - you might think your system has hung if you try it on a big directory).
That's why you need "info" or "pinfo"
Again, you'd never know that if you hadn't read "info ls" - "man ls" doesn't mention any of that. Who knows what useful things await you? You may be piping ls to awk or Perl to get results you could have with just a flag you never knew about!
By the way, a fun place to see who had what when is the Free BSD man pages. You can check the man page over quite a span of operating system releases, including all the way back to SunOS 4.1.3! (which is where I shapened some of my Unix teeth)
Most operating systems today have more than traditional Unix permissions available. Mac OS X uses ACL's and "ls -l" will add a "+" to indicate their presence (but does not otherwise list them; use 'ls -le' to actually see them).
OS X also has "extended attributes" which show up with "-l@". That's not the same as the ACL info. It indicates the presence of metadata instead.
Linux ls shows SELinux contexts with ls -Z and also adds that "+" to indicate the presence of
ACL's, but lacks the "-e" to display them (use "getfacl").
Common troubleshooting use
Some things I find useful include:
This tells you when a file was last used:
apl$ ls -lut t
-rwxr-xr-x 1 apl staff 31 16 Nov 08:02 t
apl$ cat t > /dev/null
apl$ ls -lut t
-rwxr-xr-x 1 apl staff 31 16 Nov 08:16 t
This can be very useful in Troubleshooting.
This is, of course, file system dependent: Mac OS X's native file
system didn't update this originally.
"ls -lct" shows the time the inode was changed - creation time
or a change in ownership or permissions.
Just "ls -t" sorts by time modified (most recent) which also can
provide useful debugging information.
"ls -b" is useful for showing non-printable characters in file
names. "ls -ld dirname" shows the perms and ownership of dirname
rather than its contents. "ls -li" shows inode numbers; useful for
identifying hard links (find can track down other files with the
same inode number).