Search code examples
linuxunixfindaix

Different results with find option and -mtime +7*365


I am moving some scripts from AIX to RHEL and I have found a find command of a following format:

find -mtime +7*365

Has anyone experienced a syntax like above?

Replacing 7*365 with 2555 gives different results. And yes, I can't track the original creator of that script to ask personally.

EDIT: I forgot about a '+' sign, apologies. I have focused on multiplication in the -mtime value, but the full command is

find /path/ -mtime +7*365 -name 'SOME_NAME*.*'

Solution

  • First, if there's a file whose name starts with 7 and ends with 365 in the current directory, 7*365 is replaced by the name(s) of the matching file(s). To guarantee that find sees 7*365, the wildcard needs to be protected, e.g. find -mtime '7*365' or find -mtime 7\*365.

    Then I've never seen a find implementation that accepts arithmetic expressions. Only a non-negative integer in decimal (GNU find also accepts hexadecimal with a leading 0x), with an optional leading - or +. The AIX man page says that “a decimal integer” (with optional leading - or +) is required. I do't have AIX here to test, but with 7*365, I'd expect an error, or if the integer parser is very sloppy it might be parsed as 7 or 0.

    To look for files that were modified almost 7 years ago, you'd need to tell the shell to perform the arithmetic: find -mtime $((7*365)).

    A modified version of what you wrote that does work is the following:

    typeset -i interval
    interval=7*365
    find … -mtime "$interval"
    

    It works without the quotes on interval too (as long as IFS doesn't contain a digit). The reason this works is that typeset -i declares interval as an integer variable. When you assign a value to interval, the shell performs arithmetic, so interval gets set to 2555. This only works under ksh and bash, not under plain sh.