You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extend 'test' with non-POSIX file time comparison extensions #3589
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.
Reacted by David Adam, Kurtis Rader, Daniel Danner, pizzacat83, Julian Dunskus, Aaron Gyes and Tom
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.
@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...
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)
Activity
floam commentedon Nov 27, 2016
Strangely, our included completions for
testimply that we already can do-ef,-nt,-ot.'test' completions: comment out entries
faho commentedon Apr 20, 2017
Sooo... which time does this compare? atime, ctime or mtime? Or all of them?
floam commentedon Apr 28, 2017
mtime.najamelan commentedon Mar 14, 2019
In the mean time, what's the recommended way of testing which of two files is newer in fish shell?
faho commentedon Mar 14, 2019
Your system most likely has a
testcommand, so usecommand test file1 -nt file2.Or something like
statto get the dates.Or, if you're doing this for
mv, usemv -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 commentedon Mar 14, 2019
@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 gettingtest: 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 commentedon Mar 14, 2019
Welll... no, not really.
The problem is that
testis specified in a way that interacts weird with some fish features.You mean like
""(find)"" = "Cargo.yml"? Yeah, that won't work. If thefindprints nothing, its entire argument will be eliminated, so you end up runningtest = Cargo.yml, which isn't valid.So... the
findwill 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 isif count (find Cargo.yml -newer Cargo.toml) >/dev/nullIf 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 usetest -ninstead - 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 commentedon Mar 14, 2019
builtin test: Implement -ot, -nt
builtin test: Implement -ot, -nt
builtin test: Implement -ot, -nt
builtin test: Implement -ot, -nt, -ef
builtin test: Implement -ot, -nt, -ef
builtin test: Implement -ot, -nt, -ef
builtin test: Implement -ot, -nt, -ef
builtin test: Implement -ot, -nt, -ef
builtin test: Implement -ot, -nt, -ef