APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

The lowly ls command

© April 2011 Tony Lawrence

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)

Extended Permissions

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:

ls -lut

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.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Useful ls commands for troubleshooting


Inexpensive and informative Apple related e-books:

Take Control of the Mac Command Line with Terminal, Second Edition

Take Control of iCloud, Fifth Edition

El Capitan: A Take Control Crash Course

Take Control of Parallels Desktop 12

Take Control of Automating Your Mac

More Articles by © Tony Lawrence

OSR5 has some interesting variants on the basic ls command. For example, l (el) alone displays symbolic link names with a trailing @ symbol, unlike ls -l, which displays symlinks with a "pointer" to the linked file. You can get this behavior with less typing by creating a symbolic link to l in any convenient subdirectory that is in your PATH, such as /usr/local/bin. Try this:

$ cd /usr/local/bin
$ ln -s /bin/l ll
$ l ll
-r-xr-xr-t 1 bin bin 43660 Jul 28 2000 ll@
$ ll ll
lrwxrwxrwx 1 user group 6 Nov 24 2003 ll -> /bin/l

There are others you can try: lc, for example displays filenames in columns, equivalent to ls -c. man ls will tell all. <Smile>


Wed Apr 20 15:21:59 2011: 9460   TonyLawrence


This page is a major update/rewrite of an older article - the comment above was at that older article.


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird. (Donald Knuth)

Linux posts

Troubleshooting posts

This post tagged:





Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode