• Time-stamping files

    From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.unix.shell on Sat Nov 14 09:13:50 2026
    From Newsgroup: comp.unix.shell

    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

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.unix.shell on Sat Nov 14 09:28:46 2026
    From Newsgroup: comp.unix.shell

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.unix.shell on Sat Nov 14 12:10:01 2026
    From Newsgroup: comp.unix.shell

    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

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Christian Weisgerber@naddy@mips.inka.de to comp.unix.shell on Sat Nov 14 17:35:45 2026
    From Newsgroup: comp.unix.shell

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Brian Patrie@bpatrie@bellsouth.spamisicky.net to comp.unix.shell on Sun Nov 15 04:11:05 2026
    From Newsgroup: comp.unix.shell

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.unix.shell on Sun Nov 15 12:17:35 2026
    From Newsgroup: comp.unix.shell

    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,-a-a touch name*.pdf-a-a 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:

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

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

    -a ls --sort=version | while read f
    -a do
    -a-a-a touch $f ; sleep .1
    -a 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

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Geoff Clare@geoff@clare.See-My-Signature.invalid to comp.unix.shell on Mon Nov 16 13:44:18 2026
    From Newsgroup: comp.unix.shell

    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>
    --- Synchronet 3.22a-Linux NewsLink 1.2