🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
24 messages
24 total messages Started by Janis Papanagnou Sat, 14 Nov 2026 08:13
Time-stamping files
#381
Author: Janis Papanagnou
Date: Sat, 14 Nov 2026 08:13
21 lines
2116 bytes
Occasionally I'm downloading a series of files, be it episodes of
films, chapters of documents, etc., and I'm numbering the files with
a name indicating the document and containing a sequence number 1..N
and I want them also stored with ascending file system time-stamps.
Given that I trigger my downloads in parallel I have to 'touch' the
files in the correct order afterwards. So far the use case.

What I lazily did was, say,   touch name*.pdf   but while the shell's
file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
the 'touch' command seems to _operate_ arguments in *reverse* order.

It's no biggie, but I had been wondering about that behavior. I'd at
least have expected an option available with 'touch' to control that,
or that the files are handled sequentially as provided as arguments.

I had thus written my own 'touchlist' script/hack to "fix" that. But
I'd be curious about thoughts concerning that behavior of 'touch' (or
alternative approaches).

Janis
Re: Time-stamping files
#382
Author: Kenny McCormack
Date: Sat, 14 Nov 2026 09:28
30 lines
2200 bytes
In article <11d957u$1fnqs$1@dont-email.me>,
Janis Papanagnou  <janis_papanagnou+ng@hotmail.com> wrote:
...
>I had thus written my own 'touchlist' script/hack to "fix" that. But
>I'd be curious about thoughts concerning that behavior of 'touch' (or
>alternative approaches).

I don't have any direct comment on the issue, since I suspect that your
requirement is unique enough that you are just going to have to roll your
own solution (as you have done).

But I have 3 general comments/concerns:

    1) If you touch them on-masse, isn't there some risk that they all end
    up with the same timestamp - even if "touch" does re-calculate the
    current time for each file - on a sufficiently fast machine?  What is
    the granularity of file timestamps on your system?

    2) If you use the shell to expand the list, won't you fall into the old
    "10 sorts before 2" problem?

    3) I'm surprised that the files aren't already time-stamped in the same
    order in which they were downloaded.  Wouldn't that already be what you
    want?

-- 
The randomly chosen signature file that would have appeared here is more than 4-ish
lines long.  As such, it violates one or more Usenet RFCs.  In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
	http://user.xmission.com/~gazelle/Sigs/Mandela
Re: Time-stamping files
#383
Author: Janis Papanagnou
Date: Sat, 14 Nov 2026 11:10
45 lines
3268 bytes
On 2026-11-14 10:28, Kenny McCormack wrote:
> But I have 3 general comments/concerns:
> 
>      1) If you touch them on-masse, isn't there some risk that they all end
>      up with the same timestamp - even if "touch" does re-calculate the
>      current time for each file - on a sufficiently fast machine?  What is
>      the granularity of file timestamps on your system?

The granularity that 'stat' provides shows, e.g.: 13:43:00.056163913

My script uses sub-seconds to distinguish the files, so if I have 5
files they get a date/time setting of date-time.# where "#" are the
sub-seconds. The granularity depends on the number of files (I'm
evaluating "$#" and if I happen to have a list of, say, 200 files
the sub-second resolution uses three digits hh:mm:ss.###).

> 
>      2) If you use the shell to expand the list, won't you fall into the old
>      "10 sorts before 2" problem?

I'm used to number sequences of such file-sets with leading zeroes,
so I'll have with, say, 20 PDF files files name01.pdf ... name20.pdf.

> 
>      3) I'm surprised that the files aren't already time-stamped in the same
>      order in which they were downloaded.  Wouldn't that already be what you
>      want?

Yes, if they'd be strictly serialized on download. For the general
case I'm using asynchronous downloads; I start a couple downloads in
parallel and if the files are large it may happen that files with a
higher serial number will be completed first; that may happen if the
files are of different size or if one process overhauls another.

If I can script all the download then I could use the plain 'touch',
but often the sources are available through a website only, so I
trigger the downloads from a browser and cannot directly control the
arrival time.

Besides these application cases I considered it worthwhile to have a
script that just does what I want it to do; process the argument list
so that the files get sorted the same way when I'm doing a "ls -ltr".

Janis
Re: Time-stamping files
#384
Author: Christian Weisgerber
Date: Sat, 14 Nov 2026 17:35
20 lines
1755 bytes
On 2026-11-14, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

> What I lazily did was, say,   touch name*.pdf   but while the shell's
> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
> the 'touch' command seems to _operate_ arguments in *reverse* order.

I think I've been surprised by cp(1) and/or mv(1) doing that, but
not enough to verify or investigate.  I expect such behavior could
come out of fts(3), which those utilities use.

BSD touch(1) operates in argument order.  For 4.4BSD it didn't
matter, because the time was fetched once and then applied to all
arguments.  Modern descendants now use utimensat(2) with UTIME_NOW.

This is a utility whose implementation at any point in time was
strongly influenced by the then available system calls.  BSD versions
from the 1980s used an actual read-write operation.  The horror.

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de
Re: Time-stamping files
#385
Author: Brian Patrie
Date: Sun, 15 Nov 2026 10:11
35 lines
2617 bytes
Janis Papanagnou wrote:
 > Occasionally I'm downloading a series of files, be it episodes of
 > films, chapters of documents, etc., and I'm numbering the files with
 > a name indicating the document and containing a sequence number 1..N
 > and I want them also stored with ascending file system time-stamps.
 > Given that I trigger my downloads in parallel I have to 'touch' the
 > files in the correct order afterwards. So far the use case.
 >
 > What I lazily did was, say,   touch name*.pdf   but while the shell's
 > file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
 > the 'touch' command seems to _operate_ arguments in *reverse* order.
 >
 > It's no biggie, but I had been wondering about that behavior. I'd at
 > least have expected an option available with 'touch' to control that,
 > or that the files are handled sequentially as provided as arguments.
 >
 > I had thus written my own 'touchlist' script/hack to "fix" that. But
 > I'd be curious about thoughts concerning that behavior of 'touch' (or
 > alternative approaches).

The most obvious is, of course:

   for f in *
   do
     touch $f ; sleep .1
   done

If heterogeneous number widths are an issue (and there are
no screwy things like control characters in filenames,
and you have GNU ls):

   ls --sort=version | while read f
   do
     touch $f ; sleep .1
   done
Re: Time-stamping files
#386
Author: Janis Papanagnou
Date: Sun, 15 Nov 2026 11:17
65 lines
4108 bytes
On 2026-11-15 11:11, Brian Patrie wrote:
> Janis Papanagnou wrote:
>  > Occasionally I'm downloading a series of files, be it episodes of
>  > films, chapters of documents, etc., and I'm numbering the files with
>  > a name indicating the document and containing a sequence number 1..N
>  > and I want them also stored with ascending file system time-stamps.
>  > Given that I trigger my downloads in parallel I have to 'touch' the
>  > files in the correct order afterwards. So far the use case.
>  >
>  > What I lazily did was, say,   touch name*.pdf   but while the shell's
>  > file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>  > the 'touch' command seems to _operate_ arguments in *reverse* order.
>  >
>  > It's no biggie, but I had been wondering about that behavior. I'd at
>  > least have expected an option available with 'touch' to control that,
>  > or that the files are handled sequentially as provided as arguments.
>  >
>  > I had thus written my own 'touchlist' script/hack to "fix" that. But
>  > I'd be curious about thoughts concerning that behavior of 'touch' (or
>  > alternative approaches).
> 
> The most obvious is, of course:
> 
>    for f in *
>    do
>      touch $f ; sleep .1
>    done
> 
> If heterogeneous number widths are an issue (and there are
> no screwy things like control characters in filenames,
> and you have GNU ls):
> 
>    ls --sort=version | while read f
>    do
>      touch $f ; sleep .1
>    done

Thanks. - A few comments...

GNU's "ls --sort=version" answers also point 2) in Kenny's post.
(Although not necessary in my case it's good to remember it when
necessary.)

The "$f" should be quoted, at least to handle any spaces in filenames
correctly. (Quoting should also prevent issues with control characters
unless newlines spoil the 'while'-loop.) For the typical resources on
the internet we should expect spaces in filenames, but quoting should
be mandatory anyway.

The "obvious" busy-wait with 'sleep' I wanted to avoid. With larger
file lists the time may sum up annoyingly. (And why wait if you can
just instantly "touch -d ..." with an artificial sub-seconds part.
In this case you can write the date in a way that the series of files
will get the same base-date with seconds resolution and the individual
files only differ in the sub-seconds part that 'ls' will usually not
show but implicitly use to sort the files by time if requested. That
can of course also be implemented in your 'sleep'-based solution if
the constant delay is adjusted to centi- or milliseconds depending on
the number of files. Of course only if the used 'sleep' is supporting
sub-second resolution. By that we could also guarantee that the summed
up delay stays al least within 1 second which would be more acceptable
WRT "UX".)

Janis
Re: Time-stamping files
#387
Author: Geoff Clare
Date: Mon, 16 Nov 2026 13:44
28 lines
2127 bytes
Janis Papanagnou wrote:

> What I lazily did was, say,   touch name*.pdf   but while the shell's
> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
> the 'touch' command seems to _operate_ arguments in *reverse* order.

Which version of touch?

POSIX requires that touch processes the files in the order they are
given.  (XCU 1.4 Utility Description Defaults, under "OPERANDS":
"Unless otherwise stated, the standard utilities that accept operands
shall process those operands in the order specified in the command
line."  The OPERANDS section for touch does not state otherwise.)

Running GNU touch from coreutils 9.1 under strace on Linux shows it
processes the files in the correct order...

$ strace touch file? 2>&1 | grep 'file[0-9]'
execve("/bin/touch", ["touch", "file1", "file2", "file3"], 0x7fff1d997dc8) = 0
openat(AT_FDCWD, "file1", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
openat(AT_FDCWD, "file2", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 0
openat(AT_FDCWD, "file3", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 0

If your version of touch really is processing them in reverse, you
should report it as a bug.

-- 
Geoff Clare <netnews@gclare.org.uk>
Re: Time-stamping files
#388
Author: Janis Papanagnou
Date: Mon, 16 Nov 2026 15:28
56 lines
2898 bytes
On 2026-11-16 14:44, Geoff Clare wrote:
> Janis Papanagnou wrote:
> 
>> What I lazily did was, say,   touch name*.pdf   but while the shell's
>> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>> the 'touch' command seems to _operate_ arguments in *reverse* order.
> 
> Which version of touch?

$ whence touch
/usr/bin/touch

$ touch --version
touch (GNU coreutils) 9.4
...

> 
> POSIX requires that touch processes the files in the order they are
> given.  (XCU 1.4 Utility Description Defaults, under "OPERANDS":
> "Unless otherwise stated, the standard utilities that accept operands
> shall process those operands in the order specified in the command
> line."  The OPERANDS section for touch does not state otherwise.)

Thanks.

> 
> Running GNU touch from coreutils 9.1 under strace on Linux shows it
> processes the files in the correct order...
> 
> [...]
> 
> If your version of touch really is processing them in reverse, you
> should report it as a bug.

Just noticed it's even worse than that. Incidentally I was able to
cross the resolution boundary (which seems to be 8 ns on my system)
by a single touch command which was

$ touch 6 7 8 9

and the 'stat' command for these four files shows

2026-11-16 16:13:47.212143070 +0100  7
2026-11-16 16:13:47.212143070 +0100  8
2026-11-16 16:13:47.212143070 +0100  9
2026-11-16 16:13:47.211143078 +0100  6    <<< ...78 ns

$ ls -ltr 6 7 8 9
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 6
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 9
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 8
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 7


Janis
Re: Time-stamping files
#389
Author: Christian Weisgerber
Date: Mon, 16 Nov 2026 17:13
21 lines
1529 bytes
On 2026-11-16, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

> $ touch --version
> touch (GNU coreutils) 9.4
> ...

GNU touch has always processed its operands in command line order.
Well, at least since 1992, which is as far as the Git repository
goes back.  So I don't see how we can blame userland for this:

> $ touch 6 7 8 9
>
> and the 'stat' command for these four files shows
>
> 2026-11-16 16:13:47.212143070 +0100  7
> 2026-11-16 16:13:47.212143070 +0100  8
> 2026-11-16 16:13:47.212143070 +0100  9
> 2026-11-16 16:13:47.211143078 +0100  6    <<< ...78 ns

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de
Re: Time-stamping files
#390
Author: Keith Thompson
Date: Tue, 17 Nov 2026 00:01
44 lines
3210 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> Occasionally I'm downloading a series of files, be it episodes of
> films, chapters of documents, etc., and I'm numbering the files with
> a name indicating the document and containing a sequence number 1..N
> and I want them also stored with ascending file system time-stamps.
> Given that I trigger my downloads in parallel I have to 'touch' the
> files in the correct order afterwards. So far the use case.
>
> What I lazily did was, say,   touch name*.pdf   but while the shell's
> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
> the 'touch' command seems to _operate_ arguments in *reverse* order.
>
> It's no biggie, but I had been wondering about that behavior. I'd at
> least have expected an option available with 'touch' to control that,
> or that the files are handled sequentially as provided as arguments.

I haven't been able to reproduce this behavior on my system (Ubuntu).

I built coreutils 9.4 from source and ran the resulting "touch" command
with 100 arguments.  "stat" shows the timestamps (mtime) increasing in
command line order.  The timestamp increases in increments of 1
millisecond, so multiple files have the same timestamp, in groups of 14
or 15 files.

> I had thus written my own 'touchlist' script/hack to "fix" that. But
> I'd be curious about thoughts concerning that behavior of 'touch' (or
> alternative approaches).

If it were a problem, I'd probably just type

    for file in name*.pdf ; do touch "$file" ; done

perhaps with a "sleep" if I want to ensure unique timestamps
(I get 1-2 millisecond differences without a sleep on my system).
There's nothing wrong with writing a script do that, but I probably
wouldn't bother.

My system (with an ext4 filesystem) stores timestamps with nanosecond
precision (but not, apparently, nanosecond accuracy).  Others
might not.  IIRC, FAT32 has 2-second precision.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Time-stamping files
#391
Author: Kaz Kylheku
Date: Tue, 17 Nov 2026 01:05
25 lines
2076 bytes
On 2026-11-16, Geoff Clare <geoff@clare.See-My-Signature.invalid> wrote:
> Janis Papanagnou wrote:
>
>> What I lazily did was, say,   touch name*.pdf   but while the shell's
>> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>> the 'touch' command seems to _operate_ arguments in *reverse* order.
>
> Which version of touch?
>
> POSIX requires that touch processes the files in the order they are
> given.

That's a terrible requirement. It should be required to touch all
files passed as arguments in the same invocation to the same timestamp.

Which can happen anyway for small numbers of arguments on a filesystem
with sufficiently poor timestamp granularity.

POSIX's requirement does not add up to a guarantee that the objects have
increasing timestamps in the order given, so what's the point.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Re: Time-stamping files
#392
Author: Janis Papanagnou
Date: Tue, 17 Nov 2026 06:55
100 lines
5981 bytes
On 2026-11-17 01:01, Keith Thompson wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> Occasionally I'm downloading a series of files, be it episodes of
>> films, chapters of documents, etc., and I'm numbering the files with
>> a name indicating the document and containing a sequence number 1..N
>> and I want them also stored with ascending file system time-stamps.
>> Given that I trigger my downloads in parallel I have to 'touch' the
>> files in the correct order afterwards. So far the use case.
>>
>> What I lazily did was, say,   touch name*.pdf   but while the shell's
>> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>> the 'touch' command seems to _operate_ arguments in *reverse* order.
>>
>> It's no biggie, but I had been wondering about that behavior. I'd at
>> least have expected an option available with 'touch' to control that,
>> or that the files are handled sequentially as provided as arguments.
> 
> I haven't been able to reproduce this behavior on my system (Ubuntu).
> 
> I built coreutils 9.4 from source and ran the resulting "touch" command
> with 100 arguments.  "stat" shows the timestamps (mtime) increasing in
> command line order.  The timestamp increases in increments of 1
> millisecond, so multiple files have the same timestamp, in groups of 14
> or 15 files.
> 
>> I had thus written my own 'touchlist' script/hack to "fix" that. But
>> I'd be curious about thoughts concerning that behavior of 'touch' (or
>> alternative approaches).
> 
> If it were a problem, I'd probably just type
> 
>      for file in name*.pdf ; do touch "$file" ; done
> 
> perhaps with a "sleep" if I want to ensure unique timestamps
> (I get 1-2 millisecond differences without a sleep on my system).

It's unfortunately not guaranteed that you get different timestamps
that way. (On my system I get regularly clashes, i.e. files with the
same timestamps.) And 'sleep' I'd avoid for reasons given; certainly
if we can comparably simply avoid these (as I've done with my script).

> There's nothing wrong with writing a script do that, but I probably
> wouldn't bother.
> 
> My system (with an ext4 filesystem) stores timestamps with nanosecond
> precision (but not, apparently, nanosecond accuracy).  Others
> might not.  IIRC, FAT32 has 2-second precision.

Given (also) my response to Geoff with the data from 'stat' and the
"8 ns jump" of the timestamp I'm still puzzled what is going on here.
(I quote it for completeness here, and *fixed* a crucial observation
error I made; the difference is *not* 8 ns but rather already in the
ms range, .2111... vs. .2121..., which I previously missed to see.)

$ touch 6 7 8 9

and the 'stat' command for these four files shows

2026-11-16 16:13:47.211143078 +0100  6 ___________ gap of
2026-11-16 16:13:47.212143070 +0100  7             ~1 ms
2026-11-16 16:13:47.212143070 +0100  8
2026-11-16 16:13:47.212143070 +0100  9

$ ls -ltr 6 7 8 9
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 6
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 9
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 8
-rw-rw-r-- 1 xyz xyz 0 Nov 16 16:13 7

A couple things are to observe here. Files 7, 8, and 9 have the same
timestamp but are ordered sequentially anyway. "What" is responsible
for the listing order given in "ls -t" (and "ls -tr") that they are
presented in an order that cannot be explained by the timestamp alone.
Then, between _groups_ of identical timestamps, here {6} and {7, 8, 9}
respectively there is an ordering (i.e. {6} comes before {7, 8, 9}).
So one question is whether files with the same timestamps will group
correctly "in the order of the arguments" (POSIX) but only while the
timestamps are incidentally unequal.

And the new question arises - which component is responsible, if not
'touch' - and whether the file system might also play a role here.
(My file system is ZFS, if that matters.)

While I think there's a point in Kaz' view
  "It should be required to touch all files passed as arguments in
   the same invocation to the same timestamp."
I consider it nonetheless sensible to have a visible differentiation
(as POSIX seems to require) in case we explicitly want 'ls' by time.

Given all that I think that my scripting approach is sensible, to
give all files of my single touch call the same timestamp [with sec
resolution] and differentiate them anyway by setting the sub-seconds
for a visible ordering by time. (Where using explicit 'sleep' calls
I think is, for various reasons, not a good choice.)

Now, in which order does 'ls -t' sorts a set of files which have the
property of carrying the _same timestamp_ then; by name?

Janis
Re: Time-stamping files
#393
Author: Richard Harnden
Date: Tue, 17 Nov 2026 16:10
60 lines
3091 bytes
On 14/11/2026 08:13, Janis Papanagnou wrote:
> Occasionally I'm downloading a series of files, be it episodes of
> films, chapters of documents, etc., and I'm numbering the files with
> a name indicating the document and containing a sequence number 1..N
> and I want them also stored with ascending file system time-stamps.
> Given that I trigger my downloads in parallel I have to 'touch' the
> files in the correct order afterwards. So far the use case.
> 
> What I lazily did was, say,   touch name*.pdf   but while the shell's
> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
> the 'touch' command seems to _operate_ arguments in *reverse* order.
> 
> It's no biggie, but I had been wondering about that behavior. I'd at
> least have expected an option available with 'touch' to control that,
> or that the files are handled sequentially as provided as arguments.
> 
> I had thus written my own 'touchlist' script/hack to "fix" that. But
> I'd be curious about thoughts concerning that behavior of 'touch' (or
> alternative approaches).
> 
> Janis
> 

It's not shell, but maybe this does what you want ...

Starts with 'now' and zero usec, and adds 1 usec per file.
Assuming you don't really care what the timestamps are, only that they 
increase.

(I tried with 'echo * | xargs -l touch' too, but that doesn't work)

#include <time.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{
     struct timeval times[2];

     times[0].tv_sec = time(NULL);
     times[1].tv_sec = times[0].tv_sec;

     times[0].tv_usec = 0;
     times[1].tv_usec = 0;

     for (int i=1; i<argc; i++)
     {
         if ( utimes(argv[i], times) != 0 )
             return 1;

         times[0].tv_usec += 1;
         times[1].tv_usec += 1;
     }

     return 0;
}




Re: Time-stamping files
#397
Author: Keith Thompson
Date: Tue, 17 Nov 2026 21:40
81 lines
5259 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 2026-11-17 01:01, Keith Thompson wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>> Occasionally I'm downloading a series of files, be it episodes of
>>> films, chapters of documents, etc., and I'm numbering the files with
>>> a name indicating the document and containing a sequence number 1..N
>>> and I want them also stored with ascending file system time-stamps.
>>> Given that I trigger my downloads in parallel I have to 'touch' the
>>> files in the correct order afterwards. So far the use case.
>>>
>>> What I lazily did was, say,   touch name*.pdf   but while the shell's
>>> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>>> the 'touch' command seems to _operate_ arguments in *reverse* order.
>>>
>>> It's no biggie, but I had been wondering about that behavior. I'd at
>>> least have expected an option available with 'touch' to control that,
>>> or that the files are handled sequentially as provided as arguments.
>>
>> I haven't been able to reproduce this behavior on my system (Ubuntu).
>> I built coreutils 9.4 from source and ran the resulting "touch"
>> command with 100 arguments.  "stat" shows the timestamps (mtime)
>> increasing in command line order.  The timestamp increases in
>> increments of 1 millisecond, so multiple files have the same
>> timestamp, in groups of 14 or 15 files.
>> 
>>> I had thus written my own 'touchlist' script/hack to "fix" that. But
>>> I'd be curious about thoughts concerning that behavior of 'touch' (or
>>> alternative approaches).
>>
>> If it were a problem, I'd probably just type
>>      for file in name*.pdf ; do touch "$file" ; done
>> perhaps with a "sleep" if I want to ensure unique timestamps
>> (I get 1-2 millisecond differences without a sleep on my system).
>
> It's unfortunately not guaranteed that you get different timestamps
> that way. (On my system I get regularly clashes, i.e. files with the
> same timestamps.) And 'sleep' I'd avoid for reasons given; certainly
> if we can comparably simply avoid these (as I've done with my script).

I don't think you had said that you wanted unique timestamps.

What you get with `touch 0 1 2` is that 0 is no newer than 1 and
1 is no newer than 2 (where "newer" is defined by the mtime).  Any
two, or all three, can have the same timestamp if they're touched
more quickly than the clock updates.

I think you were observing the output of `ls -tr` and *incorrectly*
assuming that it indicated that, for example, 0 is newer than 1,
when in fact 0 and 1 have the same timestamp, but `ls -tr` uses
some other criterion to decide how to sort them.

Reading the GNU coreutils ls documentation, I don't see any mention
of how `ls -tr a b` orders its output when a and b have the same
timestamp.  I speculate that if the timestamps are the same, it
falls back to locale-specific alphabetical sorting -- and since
the `-r` (`--reverse`) option was given, it sorts b before a.
Someone could examine the source code.

Note that newer Ubuntu releases use uutils, the Rust reimplementation
of coreutils.  It's *supposed* to work the same way.  You might
consider other implementations as well (*BSD, busybox, toybox)
depending on how much you care about portability.

`touch a b c` appears to guarantee that the timestamps satisfy
a <= b <= c.  If you require a < b < c, you'll need to do a little
more work.

GNU coreutils touch lets you specify the timestamp you want.  A script
that touches each of a list of files with a unique timestamp could start
with the current time, and increment it by one nanosecond for each file.
This assumes the filesystem supports nanosecond-resolution timestamps
(not all do).

POSIX specifies the `-d` option to touch and allows fractional seconds,
but it doesn't specify how many digits are significant.

[...]

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Time-stamping files
#398
Author: Nuno Silva
Date: Tue, 17 Nov 2026 23:56
93 lines
5798 bytes
On 2026-11-17, Keith Thompson wrote:

> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> On 2026-11-17 01:01, Keith Thompson wrote:
>>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>>> Occasionally I'm downloading a series of files, be it episodes of
>>>> films, chapters of documents, etc., and I'm numbering the files with
>>>> a name indicating the document and containing a sequence number 1..N
>>>> and I want them also stored with ascending file system time-stamps.
>>>> Given that I trigger my downloads in parallel I have to 'touch' the
>>>> files in the correct order afterwards. So far the use case.
>>>>
>>>> What I lazily did was, say,   touch name*.pdf   but while the shell's
>>>> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>>>> the 'touch' command seems to _operate_ arguments in *reverse* order.
>>>>
>>>> It's no biggie, but I had been wondering about that behavior. I'd at
>>>> least have expected an option available with 'touch' to control that,
>>>> or that the files are handled sequentially as provided as arguments.
>>>
>>> I haven't been able to reproduce this behavior on my system (Ubuntu).
>>> I built coreutils 9.4 from source and ran the resulting "touch"
>>> command with 100 arguments.  "stat" shows the timestamps (mtime)
>>> increasing in command line order.  The timestamp increases in
>>> increments of 1 millisecond, so multiple files have the same
>>> timestamp, in groups of 14 or 15 files.
>>> 
>>>> I had thus written my own 'touchlist' script/hack to "fix" that. But
>>>> I'd be curious about thoughts concerning that behavior of 'touch' (or
>>>> alternative approaches).
>>>
>>> If it were a problem, I'd probably just type
>>>      for file in name*.pdf ; do touch "$file" ; done
>>> perhaps with a "sleep" if I want to ensure unique timestamps
>>> (I get 1-2 millisecond differences without a sleep on my system).
>>
>> It's unfortunately not guaranteed that you get different timestamps
>> that way. (On my system I get regularly clashes, i.e. files with the
>> same timestamps.) And 'sleep' I'd avoid for reasons given; certainly
>> if we can comparably simply avoid these (as I've done with my script).
>
> I don't think you had said that you wanted unique timestamps.
>
> What you get with `touch 0 1 2` is that 0 is no newer than 1 and
> 1 is no newer than 2 (where "newer" is defined by the mtime).  Any
> two, or all three, can have the same timestamp if they're touched
> more quickly than the clock updates.
>
> I think you were observing the output of `ls -tr` and *incorrectly*
> assuming that it indicated that, for example, 0 is newer than 1,
> when in fact 0 and 1 have the same timestamp, but `ls -tr` uses
> some other criterion to decide how to sort them.
>
> Reading the GNU coreutils ls documentation, I don't see any mention
> of how `ls -tr a b` orders its output when a and b have the same
> timestamp.  I speculate that if the timestamps are the same, it
> falls back to locale-specific alphabetical sorting -- and since
> the `-r` (`--reverse`) option was given, it sorts b before a.
> Someone could examine the source code.

If it doesn't fall back to that, it should, at least if I'm reading
right. IEEE 1003.1-2008 says it should sort by "filename in the
collating sequence":

«−t  Sort with the primary key being time modified (most recently
     modified first) and the secondary key being filename in the
     collating sequence. [...]»

2024 at opengroup.org has the same:
<https://pubs.opengroup.org/onlinepubs/9799919799/utilities/ls.html>

> Note that newer Ubuntu releases use uutils, the Rust reimplementation
> of coreutils.  It's *supposed* to work the same way.  You might
> consider other implementations as well (*BSD, busybox, toybox)
> depending on how much you care about portability.
>
> `touch a b c` appears to guarantee that the timestamps satisfy
> a <= b <= c.  If you require a < b < c, you'll need to do a little
> more work.
>
> GNU coreutils touch lets you specify the timestamp you want.  A script
> that touches each of a list of files with a unique timestamp could start
> with the current time, and increment it by one nanosecond for each file.
> This assumes the filesystem supports nanosecond-resolution timestamps
> (not all do).
>
> POSIX specifies the `-d` option to touch and allows fractional seconds,
> but it doesn't specify how many digits are significant.
>
> [...]

-- 
Nuno Silva
Re: Time-stamping files
#399
Author: Keith Thompson
Date: Wed, 18 Nov 2026 00:08
30 lines
2625 bytes
Nuno Silva <nunojsilva@invalid.invalid> writes:
> On 2026-11-17, Keith Thompson wrote:
[...]
>> Reading the GNU coreutils ls documentation, I don't see any mention
>> of how `ls -tr a b` orders its output when a and b have the same
>> timestamp.  I speculate that if the timestamps are the same, it
>> falls back to locale-specific alphabetical sorting -- and since
>> the `-r` (`--reverse`) option was given, it sorts b before a.
>> Someone could examine the source code.
>
> If it doesn't fall back to that, it should, at least if I'm reading
> right. IEEE 1003.1-2008 says it should sort by "filename in the
> collating sequence":
>
> «−t  Sort with the primary key being time modified (most recently
>      modified first) and the secondary key being filename in the
>      collating sequence. [...]»
>
> 2024 at opengroup.org has the same:
> <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/ls.html>
[...]

You're right.  POSIX does specifies that `ls -tr` uses the filename
in the collating sequence as the secondary key.  The GNU coreutils
documentation doesn't specify that (unless I missed something),
but it does behave that way.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Time-stamping files
#400
Author: Kaz Kylheku
Date: Wed, 18 Nov 2026 06:19
72 lines
4974 bytes
On 2026-11-17, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> On 2026-11-17 01:01, Keith Thompson wrote:
>>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>>> Occasionally I'm downloading a series of files, be it episodes of
>>>> films, chapters of documents, etc., and I'm numbering the files with
>>>> a name indicating the document and containing a sequence number 1..N
>>>> and I want them also stored with ascending file system time-stamps.
>>>> Given that I trigger my downloads in parallel I have to 'touch' the
>>>> files in the correct order afterwards. So far the use case.
>>>>
>>>> What I lazily did was, say,   touch name*.pdf   but while the shell's
>>>> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>>>> the 'touch' command seems to _operate_ arguments in *reverse* order.
>>>>
>>>> It's no biggie, but I had been wondering about that behavior. I'd at
>>>> least have expected an option available with 'touch' to control that,
>>>> or that the files are handled sequentially as provided as arguments.
>>>
>>> I haven't been able to reproduce this behavior on my system (Ubuntu).
>>> I built coreutils 9.4 from source and ran the resulting "touch"
>>> command with 100 arguments.  "stat" shows the timestamps (mtime)
>>> increasing in command line order.  The timestamp increases in
>>> increments of 1 millisecond, so multiple files have the same
>>> timestamp, in groups of 14 or 15 files.
>>> 
>>>> I had thus written my own 'touchlist' script/hack to "fix" that. But
>>>> I'd be curious about thoughts concerning that behavior of 'touch' (or
>>>> alternative approaches).
>>>
>>> If it were a problem, I'd probably just type
>>>      for file in name*.pdf ; do touch "$file" ; done
>>> perhaps with a "sleep" if I want to ensure unique timestamps
>>> (I get 1-2 millisecond differences without a sleep on my system).
>>
>> It's unfortunately not guaranteed that you get different timestamps
>> that way. (On my system I get regularly clashes, i.e. files with the
>> same timestamps.) And 'sleep' I'd avoid for reasons given; certainly
>> if we can comparably simply avoid these (as I've done with my script).
>
> I don't think you had said that you wanted unique timestamps.
>
> What you get with `touch 0 1 2` is that 0 is no newer than 1 and
> 1 is no newer than 2 (where "newer" is defined by the mtime).  Any
> two, or all three, can have the same timestamp if they're touched
> more quickly than the clock updates.

I'd much more readily get behind the requirement that 0, 1 and 2
have identical timestamps. At least, if they are each on a filesystem
supporting the same resolution of timestamp. (That is to say, the
current time is sampled once, and converted into the time stamp format
supported by each of the argument objects, which may cause it to be
truncated differently, but any two objects that are on the same,
or same kind of filesystem should end up with identical modification
times.)

touch has that -r <ref-object> option which can achieve a facsimile
of these requirements, if the script creates a temporary file,
uses it as a reference and then deletes it.

touch without the -r option would ideally behave the same way as with
-r, except using a single sample of the system time as the reference.

The one infelicity in such an requirement is that it's limited by the
argument passing limits. As soon as you use "xargs" to break up a long
touch command line, the all-stamps-same property is lost; you have to
resort to the -r <ref-object> option.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Re: Time-stamping files
#401
Author: Janis Papanagnou
Date: Wed, 18 Nov 2026 07:28
88 lines
3796 bytes
On 2026-11-17 17:10, Richard Harnden wrote:
> On 14/11/2026 08:13, Janis Papanagnou wrote:
>> [...]
>>
>> I had thus written my own 'touchlist' script/hack to "fix" that. But
>> I'd be curious about thoughts concerning that behavior of 'touch' (or
>> alternative approaches).
> 
> It's not shell, but maybe this does what you want ...
> 
> Starts with 'now' and zero usec, and adds 1 usec per file.
> Assuming you don't really care what the timestamps are, only that they 
> increase.

Thanks.

This - the C-algorithm below - is actually similar to what my shell
script does. The difference is that you're counting micro-seconds
(always as a fixed increment) where I'm adjusting it to the needs
(with the least possible accuracy); I wanted to keep the counter as
close to the seconds as possible.

For comparison, here's the Kornshell script[*] that I'm using...


     typeset -i n_files=$#
     typeset -i decimals=${#n_files}
     typeset -i serial_no=0
     typeset -Z${decimals} subseconds
     typeset time_now

     printf "%(%Y-%m-%d %H:%M:%S)T" | IFS= read time_now
     for f in "$@"
     do
         subseconds=$(( ++serial_no ))
         touch -d "${time_now}.${subseconds}" "${f}"
     done


An argument list up to 999 files will still stay in the [ms] range,
a handful of file (less than 10) will be represented by time-now.#,
100000 files will require with time-now.###### the range of [µs],
where the '#' symbols represent the sub-seconds decimals.

> [...]
Janis

[*] To rewrite, e.g. for Bash, note...

typeset -i     declares an integer variable
typeset -Z{n}  declares a numeric variable with n digits (you may
                replace that with printf '%0*d' $n $subsec in Bash)
printf "%(...)T"  is a built-in time generator/formatter (may be
                   replaced by the external 'date ...' instead)
... | read var    would not work in Bash that way, use $(...) e.g.


> 
> #include <time.h>
> #include <sys/time.h>
> 
> int main(int argc, char *argv[])
> {
>      struct timeval times[2];
> 
>      times[0].tv_sec = time(NULL);
>      times[1].tv_sec = times[0].tv_sec;
> 
>      times[0].tv_usec = 0;
>      times[1].tv_usec = 0;
> 
>      for (int i=1; i<argc; i++)
>      {
>          if ( utimes(argv[i], times) != 0 )
>              return 1;
> 
>          times[0].tv_usec += 1;
>          times[1].tv_usec += 1;
>      }
> 
>      return 0;
> }
> 
> 
> 
> 
> 
Re: Time-stamping files
#402
Author: Janis Papanagnou
Date: Wed, 18 Nov 2026 07:47
122 lines
6908 bytes
On 2026-11-17 22:40, Keith Thompson wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> On 2026-11-17 01:01, Keith Thompson wrote:
>>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>>> Occasionally I'm downloading a series of files, be it episodes of
>>>> films, chapters of documents, etc., and I'm numbering the files with
>>>> a name indicating the document and containing a sequence number 1..N
>>>> and I want them also stored with ascending file system time-stamps.
>>>> Given that I trigger my downloads in parallel I have to 'touch' the
>>>> files in the correct order afterwards. So far the use case.
>>>>
>>>> What I lazily did was, say,   touch name*.pdf   but while the shell's
>>>> file expansion lists sequentially name1.pdf name2.pdf ... name8.pdf
>>>> the 'touch' command seems to _operate_ arguments in *reverse* order.
>>>>
>>>> It's no biggie, but I had been wondering about that behavior. I'd at
>>>> least have expected an option available with 'touch' to control that,
>>>> or that the files are handled sequentially as provided as arguments.
>>>
>>> I haven't been able to reproduce this behavior on my system (Ubuntu).
>>> I built coreutils 9.4 from source and ran the resulting "touch"
>>> command with 100 arguments.  "stat" shows the timestamps (mtime)
>>> increasing in command line order.  The timestamp increases in
>>> increments of 1 millisecond, so multiple files have the same
>>> timestamp, in groups of 14 or 15 files.
>>>
>>>> I had thus written my own 'touchlist' script/hack to "fix" that. But
>>>> I'd be curious about thoughts concerning that behavior of 'touch' (or
>>>> alternative approaches).
>>>
>>> If it were a problem, I'd probably just type
>>>       for file in name*.pdf ; do touch "$file" ; done
>>> perhaps with a "sleep" if I want to ensure unique timestamps
>>> (I get 1-2 millisecond differences without a sleep on my system).
>>
>> It's unfortunately not guaranteed that you get different timestamps
>> that way. (On my system I get regularly clashes, i.e. files with the
>> same timestamps.) And 'sleep' I'd avoid for reasons given; certainly
>> if we can comparably simply avoid these (as I've done with my script).
> 
> I don't think you had said that you wanted unique timestamps.

I wanted _ascending_ timestamps.

The point is that with spurious 'sleep' delays you could get strange
results; e.g. in the meantime downloaded files could get in-between.
So the dates should be close together, and since I can do my counts
in the sub-seconds parts the *nice* _side-effect_ is that the set of
files will carry the same timestamp.

> 
> What you get with `touch 0 1 2` is that 0 is no newer than 1 and
> 1 is no newer than 2 (where "newer" is defined by the mtime).  Any
> two, or all three, can have the same timestamp if they're touched
> more quickly than the clock updates.

Yes, they can be the same or different (as said before; and see my
previously posted 'stat' output example).

> 
> I think you were observing the output of `ls -tr` and *incorrectly*
> assuming that it indicated that, for example, 0 is newer than 1,
> when in fact 0 and 1 have the same timestamp, but `ls -tr` uses
> some other criterion to decide how to sort them.

Yes and no. As seen in my previously posted data the files with the
same internal timestamp are ordered, of course not by the timestamp
but by something else; may it be the name, or the physical position
in the directory, or whatever; that order is *also* affected by the
'-r'. So me have some mixture, primary search criterion is the time,
and the secondary criterion isn't clear to me.

> 
> Reading the GNU coreutils ls documentation, I don't see any mention
> of how `ls -tr a b` orders its output when a and b have the same
> timestamp.  I speculate that if the timestamps are the same, it
> falls back to locale-specific alphabetical sorting -- and since
> the `-r` (`--reverse`) option was given, it sorts b before a.
> Someone could examine the source code.

Yes, that's one possibility. (It's just not transparent which one
it actually is.)

> 
> Note that newer Ubuntu releases use uutils, the Rust reimplementation
> of coreutils.  It's *supposed* to work the same way.  You might
> consider other implementations as well (*BSD, busybox, toybox)
> depending on how much you care about portability.

Well, I have my simple script solution. ;-)

> 
> `touch a b c` appears to guarantee that the timestamps satisfy
> a <= b <= c.  If you require a < b < c, you'll need to do a little
> more work.

Yes.

> 
> GNU coreutils touch lets you specify the timestamp you want.  A script
> that touches each of a list of files with a unique timestamp could start
> with the current time, and increment it by one nanosecond for each file.

I think I very early already mentioned that my script does something
like that (or even something better). :-)

> This assumes the filesystem supports nanosecond-resolution timestamps
> (not all do).

That's why I stay as close to the seconds as possible. (For a handful
of files one decimal is sufficient, for 999 files [ms] suffice, and
with 100000 files you enter the [µs] resolution.)

> 
> POSIX specifies the `-d` option to touch and allows fractional seconds,
> but it doesn't specify how many digits are significant.

Yes, that's what I've been using in my script. - I suppose that the
supported resolution may depend on the filesystem.

Janis
Re: Time-stamping files
#403
Author: Janis Papanagnou
Date: Wed, 18 Nov 2026 07:49
30 lines
2421 bytes
On 2026-11-18 00:56, Nuno Silva wrote:
> On 2026-11-17, Keith Thompson wrote:
>> [...]
>>
>> Reading the GNU coreutils ls documentation, I don't see any mention
>> of how `ls -tr a b` orders its output when a and b have the same
>> timestamp.  I speculate that if the timestamps are the same, it
>> falls back to locale-specific alphabetical sorting -- and since
>> the `-r` (`--reverse`) option was given, it sorts b before a.
>> Someone could examine the source code.
> 
> If it doesn't fall back to that, it should, at least if I'm reading
> right. IEEE 1003.1-2008 says it should sort by "filename in the
> collating sequence":
> 
> «−t  Sort with the primary key being time modified (most recently
>       modified first) and the secondary key being filename in the
>       collating sequence. [...]»
> 
> 2024 at opengroup.org has the same:
> <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/ls.html>

Thanks for digging that up! :-)

(Makes all further speculations unnecessary.)

Janis

> [...]
Re: Time-stamping files
#404
Author: Janis Papanagnou
Date: Wed, 18 Nov 2026 07:59
18 lines
1920 bytes
On 2026-11-18 08:47, Janis Papanagnou wrote:
> [...]
> 
> The point is that with spurious 'sleep' delays you could get strange
> results; e.g. in the meantime downloaded files could get in-between.
> So the dates should be close together, and since I can do my counts
> in the sub-seconds parts the *nice* _side-effect_ is that the set of
> files will carry the same timestamp.

What I wanted to say...

[...] will carry the _same visible_ timestamp when inspected with
the 'ls' command [while being still able to sort them by time].

Janis

> [...]
Re: Time-stamping files
#405
Author: Nuno Silva
Date: Wed, 18 Nov 2026 10:56
46 lines
3274 bytes
On 2026-11-18, Keith Thompson wrote:

> Nuno Silva <nunojsilva@invalid.invalid> writes:
>> On 2026-11-17, Keith Thompson wrote:
> [...]
>>> Reading the GNU coreutils ls documentation, I don't see any mention
>>> of how `ls -tr a b` orders its output when a and b have the same
>>> timestamp.  I speculate that if the timestamps are the same, it
>>> falls back to locale-specific alphabetical sorting -- and since
>>> the `-r` (`--reverse`) option was given, it sorts b before a.
>>> Someone could examine the source code.
>>
>> If it doesn't fall back to that, it should, at least if I'm reading
>> right. IEEE 1003.1-2008 says it should sort by "filename in the
>> collating sequence":
>>
>> «−t  Sort with the primary key being time modified (most recently
>>      modified first) and the secondary key being filename in the
>>      collating sequence. [...]»
>>
>> 2024 at opengroup.org has the same:
>> <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/ls.html>
> [...]
>
> You're right.  POSIX does specifies that `ls -tr` uses the filename
> in the collating sequence as the secondary key.  The GNU coreutils
> documentation doesn't specify that (unless I missed something),
> but it does behave that way.

I think you didn't miss anything (here it's apparently missing from both
the man and info online manuals), and if a web version of the uutils
manpage is accurate, uutils also doesn't document this properly...
<https://uutils.org/coreutils/docs/utils/ls.html>:

    "-t Sort by modification time (the ‘mtime’ in the inode), newest
        first."

BSD ls has different wording but mentions it (leaves out the part about
symbolic links, although maybe that's covered by the entry for -L?):
from <https://man.netbsd.org/ls.1>:

    "-t Sort by time modified (most recently modified first) before
        sorting the operands by lexicographical order."

-- 
Nuno Silva
Re: Time-stamping files
#408
Author: Keith Thompson
Date: Wed, 18 Nov 2026 23:19
80 lines
4879 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 2026-11-17 22:40, Keith Thompson wrote:
[...]
>> I don't think you had said that you wanted unique timestamps.
>
> I wanted _ascending_ timestamps.
>
> The point is that with spurious 'sleep' delays you could get strange
> results; e.g. in the meantime downloaded files could get in-between.
> So the dates should be close together, and since I can do my counts
> in the sub-seconds parts the *nice* _side-effect_ is that the set of
> files will carry the same timestamp.
>
>> What you get with `touch 0 1 2` is that 0 is no newer than 1 and
>> 1 is no newer than 2 (where "newer" is defined by the mtime).  Any
>> two, or all three, can have the same timestamp if they're touched
>> more quickly than the clock updates.
>
> Yes, they can be the same or different (as said before; and see my
> previously posted 'stat' output example).

Hmm.  I would interpret "_ascending_ timestamps" to mean a < b < c.
If all you need is a <= b <= c, I'd describe that as non-descending
timestamps.

>> I think you were observing the output of `ls -tr` and *incorrectly*
>> assuming that it indicated that, for example, 0 is newer than 1,
>> when in fact 0 and 1 have the same timestamp, but `ls -tr` uses
>> some other criterion to decide how to sort them.
>
> Yes and no. As seen in my previously posted data the files with the
> same internal timestamp are ordered, of course not by the timestamp
> but by something else; may it be the name, or the physical position
> in the directory, or whatever; that order is *also* affected by the
> '-r'. So me have some mixture, primary search criterion is the time,
> and the secondary criterion isn't clear to me.

Just to be clear, there are no separate internal and external
timestamps.  A file's mtime is, on my system, stored with nanosecond
resolution.  `ls -t` or `ls -tr` uses the full nanosecond-precise
timestamp as the primary sort key (and the name as the secondary
key when two files have the same precise mtime).  `make` uses the
full precision timestamp when deciding which of two files is newer.
And so on.

`ls -l` simply doesn't display the fractional part of the mtime.
`ls -l --full-time` does, as does `stat`.

>> Reading the GNU coreutils ls documentation, I don't see any mention
>> of how `ls -tr a b` orders its output when a and b have the same
>> timestamp.  I speculate that if the timestamps are the same, it
>> falls back to locale-specific alphabetical sorting -- and since
>> the `-r` (`--reverse`) option was given, it sorts b before a.
>> Someone could examine the source code.
>
> Yes, that's one possibility. (It's just not transparent which one
> it actually is.)

As discussed elsethread, that behavior seems to be required by Posix.

It would be nice, I suppose, if you could use the timestamp, oldest
first, as the primary key, and the name, not reversed, as the
secondary key, but ls doesn't have options to do that.  (And I'm
not suggesting it should.)

[...]

>> POSIX specifies the `-d` option to touch and allows fractional
>> seconds, but it doesn't specify how many digits are significant.
>
> Yes, that's what I've been using in my script. - I suppose that the
> supported resolution may depend on the filesystem.

In particular, FAT32 has 2-second resolution, so if you try to create
1000 files with distinct timestamps, they'll range over more than 8
minutes.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Time-stamping files
#409
Author: Brian Patrie
Date: Thu, 19 Nov 2026 09:56
4 lines
1404 bytes
Janis Papanagnou wrote:
> The "$f" should be quoted

Oops.  zsh user.  I often forget to portablize that.  :)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads