The lowly ls command
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":
-rw-r--r-- 1 apl apl 42B Jan 15 2009 where
-rw-r--r--@ 1 apl apl 486B May 26 2009 wicvk.rtf
-rw-r--r--@ 1 apl apl 4.4K Sep 23 2009 win7secrets.rtf
-rw-r--r--@ 1 apl apl 17K Apr 19 2009 winperf.rtf
lrwxr-xr-x 1 apl apl 34B Jan 12 2009 wordpress
That's helpful, though I'd rather not have the "B" (for bytes) as I can
easily think that's an "8".
An alternate is "--si", which displays in units of 1,000 rather than 1024. Note the difference:
$ ls -lh unsort
-rwxr-xr-x 1 1863165 1863165 7.7K Apr 3 12:49 unsort
$ ls -l --si unsort
-rwxr-xr-x 1 1863165 1863165 7.9k Apr 3 12:49 unsort
$ ls -l unsort
-rwxr-xr-x 1 1863165 1863165 7814 Apr 3 12:49 unsort
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:
$ ls -l --block-size="'1" unsort
-rwxr-xr-x 1 1863165 1863165 7,814 Apr 3 12:49 unsort
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).
Got something to add? Send me email.
Increase ad revenue 50-250% with Ezoic
More Articles by Tony Lawrence
Find me on Google+
© 2011-04-20 Tony Lawrence