Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
floam opened this issue Nov 27, 2016 · 8 comments · Fixed by #9058
Closed

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

floam opened this issue Nov 27, 2016 · 8 comments · Fixed by #9058

Comments

@floam
Copy link
Member

floam commented Nov 27, 2016

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.
@floam
Copy link
Member Author

floam commented Nov 27, 2016

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

floam added a commit that referenced this issue Nov 27, 2016
Our builtin doesn't do some of this stuff. See #3589
@krader1961 krader1961 added this to the fish-future milestone Mar 16, 2017
@faho
Copy link
Member

faho commented Apr 20, 2017

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

@floam
Copy link
Member Author

floam commented Apr 28, 2017

mtime.

@najamelan
Copy link

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

@faho
Copy link
Member

faho commented Mar 14, 2019

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.

@najamelan
Copy link

@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...

@faho
Copy link
Member

faho commented Mar 14, 2019

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)

@najamelan
Copy link

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...

floam added a commit to floam/fish-shell that referenced this issue Jul 8, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.
floam added a commit to floam/fish-shell that referenced this issue Jul 8, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.
@floam floam mentioned this issue Jul 8, 2022
3 tasks
floam added a commit to floam/fish-shell that referenced this issue Jul 10, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.
floam added a commit to floam/fish-shell that referenced this issue Jul 10, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
floam added a commit to floam/fish-shell that referenced this issue Jul 10, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
floam added a commit to floam/fish-shell that referenced this issue Jul 10, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
floam added a commit to floam/fish-shell that referenced this issue Jul 10, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
floam added a commit to floam/fish-shell that referenced this issue Jul 10, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
floam added a commit that referenced this issue Jul 16, 2022
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in #3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
@floam floam modified the milestones: fish-future, fish 3.6.0 Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants