On 2026-11-17 01:01, Keith Thompson wrote:
> Janis Papanagnou <jani…@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