[cs631apue] Output Sorting

Jan Schaumann jschauma at stevens.edu
Mon Oct 9 16:59:01 EDT 2017


Jason G Ajmo <jajmo at stevens.edu> wrote:
 
> So when -t is supplied, it sorts the operands (argv) by most recent
> modified. But when given -tu, does it sort the operands by last
> access? Does just -u do the same?

The way I read the manual page, '-u' only affects '-t' or '-l'.  If '-t'
is given (i.e. '-tu' or '-t -u'), then it sorts operands (as well as
directory entries encountered) by the last modified time rather than by
last access time ('-t' only); if neither is given, then '-u' doesn't do
anything.

It's possible (and reasonable) to interpret the manual page to mean that
'-u' without either '-t' nor '-l' should sort files by last modification
time.

When in doubt, compare to the reference implementation you have at hand,
i.e. the ls(1) on your NetBSD system:

$ echo foo >1
$ sleep 60          # not really necessary, but easier for humans
$ echo bar >2       # both a and m time of '2' is newer than '1'
$ cat 1 >/dev/null  # a-time is now newer than '2'
$ ls -lu [12]
-rw-------  1 jschauma  wheel  4 Oct  9 16:44 /tmp/1
-rw-------  1 jschauma  wheel  4 Oct  9 16:43 /tmp/2
$ ls [12]
1 2               # default: sorted lexicographically
$ ls -u [12]
1 2               # no change
$ ls -t [12]
2 1               # m-time of '2' is newer
$ ls -tu [12]
1 2               # now we're sorting by a-time
$ cat 2 >/dev/null  # a-time of '1' is now newer
$ ls -tu [12]
2 1               # as expected
$ ls -l [12]        # '-l' uses m-time to display, sort by name
-rw-------  1 jschauma  wheel  4 Oct  9 16:42 1
-rw-------  1 jschauma  wheel  4 Oct  9 16:43 2
$ ls -lt [12]       # explicitly request sorting by m-time
-rw-------  1 jschauma  wheel  4 Oct  9 16:43 2
-rw-------  1 jschauma  wheel  4 Oct  9 16:42 1
$ ls -ltu [12]      # explicitly request sorting & displaying a-time
-rw-------  1 jschauma  wheel  4 Oct  9 16:48 2
-rw-------  1 jschauma  wheel  4 Oct  9 16:44 1
$ cat 1 >/dev/null  # update a-time for '1'
$ ls -ltu [12]      # again request sorting & displaying a-time
-rw-------  1 jschauma  wheel  4 Oct  9 16:51 1
-rw-------  1 jschauma  wheel  4 Oct  9 16:48 2
$ cat 2 >/dev/null  # update a-time for '2' one more
$ ls -lu [12]       # sort by name, but display a-time
-rw-------  1 jschauma  wheel  4 Oct  9 16:51 1
-rw-------  1 jschauma  wheel  4 Oct  9 16:52 2
$ ls -lt [12]       # sort by m-time
-rw-------  1 jschauma  wheel  4 Oct  9 16:43 2
-rw-------  1 jschauma  wheel  4 Oct  9 16:42 1
$ ls -l [12]        # sort by name, display (default) m-time
-rw-------  1 jschauma  wheel  4 Oct  9 16:42 1
-rw-------  1 jschauma  wheel  4 Oct  9 16:43 2

> Furthermore, when supplying -t -c or -u does the sort order of the
> output change? Or does that only apply if -t is supplied too?

When supplying '-t', you are requesting sorting by time first (entries
that have identical m-times are then sorted lexicographically).  '-c' or
'-u' then modify which time is being sorted by.

But, as shown above (and as noted in the manual page), '-u' or '-c' can
also modify the timestamp that is being displayed.  The only time a
timestamp is displayed is when '-l' is given, in which case the
corresponding time is used, but sorting is only changed if in addition
to the '-l' flag the '-t' flag is also given.

Hope this makes some sense.

-Jan


More information about the cs631apue mailing list