Date comparisons

Hi,

I want to perform a simple date comparisons, i.e. select all files modified after a certain date (say 12-feb-2011)

I do not have the option of creating a file and using find's -newer option.

Any simple way to do this? I can do this by reading the stat command's output and comparing that to a literal string... but there may be an easier way.

Please explain why you can't create a file in your home directory or in /tmp.

touch -t is not allowed.

"Not allowed" is a peculiar way to put it. Why not? If you have stat you almost certainly have touch -t. Even if you don't have stat, you almost certainly have touch -t.

If you have stat though, that opens up other options. Date strings in "YYYY/MM/DD" order can be compared alphabetically; later times will be greater than earlier times.

if [ "2011-02-01" ">" "2011-02-02" ]
then
        echo "Greater"
else
        echo "Less than or equal"
fi

Well, it is indeed peculiar. I did ask why it was not enabled... And the response I got was it is "not allowed". When I asked why, I got the cryptic answer "for security purposes"... But that is an altogether different story :rolleyes:

Since the script I am creating is most probably a one time , use and throw script, I have hard coded the date I want to compare against (5-mar-2012) and used stat to get timestamp of the files I want to check. My question however was : isn't there a way to do the equivalent of sysdate - 365 (sql) in unix... Or even simply get a value of the date time as it is actually stored (i read somewhere that it is stored as the number of seconds since 1970). That would help too

Isn't it possible to use some option for find, such as:

-ctime
-mtime
-cmin
-mmin

I'm not sure which would be best for your case.
But couldn't one of these work to find all files modified after some date?

That's a blind spot. It's sad to say it, but the only truly portable way to do date math in UNIX is Perl.

GNU date is fantastic if you have it:

$ date -d "today - 1 day" +%Y-%m-%d

2013-03-05

$

...but on AIX or Solaris or anything-but-Linux that's a third-party utility. It's such an obvious feature to put in the date command that people who script in Linux often assume it's everywhere, but it's not. GNU awk has some extended date features too.

Just to be clear here. There is a very easy, very portable way to do what was requested in the 1st message in this thread on any Linux or UNIX system, but jawsnnn has ruled it out:

touch -t 201102122359.59 timestamp.$$
find directory... -newer timestamp.$$ ...
rm timestamp.$$

So within your constraints

Set some arbitrary date and then loop thru looking for new filetimes

# find files older than the first second of January 2010 - in epoch seconds
when=$(date --date="01 Jan 2010" +%s)
cd /someplace
for file in *
do
[ -d $file ] && continue
ftime=$(stat -c %X) # file time in epoch seconds
[ $ftime -le $when ] && echo $file
done > /tmp/myfilelist

I won't comment on your sysadmin. You should learn C or perl. You can write touch in 10 lines of C.

utime(2) - Linux man page

The "today -1 day" option is closest to what would help me.

As long as I know the date against which comparison needs to be done, any of the other suggested options would work (including the one with stat that I have used for now), but if I need to determine it dynamically, the date -d option should work

---------- Post updated at 10:16 AM ---------- Previous update was at 10:08 AM ----------

I just realized I can also subtract the number of seconds in an year from today's epoch second value and use that as a basis for comparison. Thanks everyone for your help.

Yes, a good example of how one's solutions are colored by the question one imagines. "How do I do date math" is too narrow a question when "how do I compare dates" is sufficient.