Skip to content

Extend 'test' with non-POSIX file time comparison extensions #3589

Closed

Description

@floam

These non-POSIX extensions are pretty popular and commonly used. I can't see the harm in making it easier to do file date comparisons with fish using test.

     file1 -nt file2
                   True if file1 exists and is newer than file2.
     file1 -ot file2
                   True if file1 exists and is older than file2.

At least on OS X, these are the other operations the system test can do that aren't POSIX, according to the manpage. Fish already supports -O and -G.

     file1 -ef file2
                   True if file1 and file2 exist and refer to the same file.

     -O file       True if file exists and its owner matches the effective user id of this process.

     -G file       True if file exists and its group matches the effective group id of this process.

Activity

  1. floam commented on Nov 27, 2016

    @floam
    MemberAuthor

    Strangely, our included completions for test imply that we already can do -ef, -nt, -ot.

  2. added a commit that references this issue on Nov 27, 2016
  3. faho commented on Apr 20, 2017

    @faho
    Member

    Sooo... which time does this compare? atime, ctime or mtime? Or all of them?

  4. floam commented on Apr 28, 2017

    @floam
    MemberAuthor

    mtime.

  5. najamelan commented on Mar 14, 2019

    @najamelan

    In the mean time, what's the recommended way of testing which of two files is newer in fish shell?

  6. faho commented on Mar 14, 2019

    @faho
    Member

    Your system most likely has a test command, so use command test file1 -nt file2.

    Or something like stat to get the dates.

    Or, if you're doing this for mv, use mv -u.

    None of this is standard (but "-nt" isn't to begin with), so it won't work on e.g. Solaris or OpenBSD, but your normal GNU/Linux should have all of these.

  7. najamelan commented on Mar 14, 2019

    @najamelan

    @faho Thanks! I was really getting stuck with test (find Cargo.yml -newer Cargo.toml) = Cargo.yml, but command test works just fine. The one area where fish really is not user friendly is the error messages. I just kept getting test: Missing argument at index 2, even when I added a string to both sides of the equasion for the times find returns nothing. I don't get why, but error messages are another topic...

  8. faho commented on Mar 14, 2019

    @faho
    Member

    I was really getting stuck with test (find Cargo.yml -newer Cargo.toml) = Cargo.yml, but command test works just fine.

    Welll... no, not really.

    The problem is that test is specified in a way that interacts weird with some fish features.

    ven when I added a string to both sides of the equasion for the times find returns nothing.

    You mean like ""(find)"" = "Cargo.yml"? Yeah, that won't work. If the find prints nothing, its entire argument will be eliminated, so you end up running test = Cargo.yml, which isn't valid.

    So... the find will already only print "Cargo.yml" if the file is newer, or nothing if it isn't. So you don't actually have to compare the string, you just need to know that it's there. So what you want is

    if count (find Cargo.yml -newer Cargo.toml) >/dev/null

    If you really want to use test, if not test -z (find Cargo.yml -newer Cargo.toml) is the shortest I can think of. (Don't use test -n instead - our test follows POSIX, and that says that it should return true if only called with one argument, which is quite the misfeature and should be removed)

  9. najamelan commented on Mar 14, 2019

    @najamelan

    if count (find Cargo.yml -newer Cargo.toml) >/dev/null
    That makes sense. I don't write shell scripts very often, so I wouln't have thought of count...

  10. added a commit that references this issue on Jul 16, 2022
  11. modified the milestones: fish-future, fish 3.6.0 on Jul 16, 2022
  12. locked as resolved and limited conversation to collaborators on Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions