stop unix find on a directory structure after finding 1st occurrence

Hi,

Has anyone tried to restrict Solaris 10 unix find on a large directory structure based on time to stop running after finding the first occurrence of a matching query. Basically I'm trying to build up a usage map of user workspaces based on file modification (week/month/3 months/year etc) and I know if I can get find to stop after finding something modified in the last year in the very first directory instead of running to the bottom getting all files the program would run a lot faster:

Excerpt:

            week=Yes; month=Yes; month6=Yes; year=Yes; year2=Yes
            if [[ -z \`find "$\{DIR\}/$\{j\}" -mtime -730\` ]];then
                    week=No; month=No; month6=No; year=No; year2=No
            else
                    if [[ -z \`find "$\{DIR\}/$\{j\}" -mtime -365\` ]];then 
                            week=No; month=No; month6=No; year=No
                    else
                            if [[ -z \`find "$\{DIR\}/$\{j\}" -mtime -180\` ]];then 
                                    week=No; month=No; month6=No
                            else
                                    if [[ -z \`find "$\{DIR\}/$\{j\}" -mtime -30\` ]];then
                                            week=No; month=No;
                                    else
                                            if [[ -z \`find "$\{DIR\}/$\{j\}" -mtime -7\` ]];then
                                                    week=No;
                                            fi
                                    fi
                            fi
                    fi
            fi

Thanks in advance,
John

please use code tags and identation!

idea: use the -exec parameter with some custom script

echo not_found > /tmp/find_result
find -whatever -options -exec kill_this_find_and_write_success_message_to_tmp_file.sh \;
if [ some condition on /tmp/find_result ]
then
  ...

Cheers, that was one way I was going to go if there wasn't another way, killing the find itself in a seperate program. Sorry bout the indentation, don't post to forums that much :slight_smile:

John

Just to close this thread off, here's the solution since noone else came up with something easier:

function something {
# $1 = the directory to search from
# $2 = timeframe to check

echo 1 > ${RESULT_FILE}
find ${1} -mtime -${2} -exec ${LOCAL}/kill_find.ksh ${1} ${2} ${RESULT_FILE} {} \;
sleep 2
result=`cat ${RESULT_FILE}`
rm -f ${RESULT_FILE}
return $result
}

week=No; month=No; month6=No; year=No; year2=No

if something "${DIR}/${j}" 7
then
week=Yes; month=Yes; month6=Yes; year=Yes; year2=Yes
elif something "${DIR}/${j}" 30
then
month=Yes; month6=Yes; year=Yes; year2=Yes
elif something "${DIR}/${j}" 180
then
month6=Yes; year=Yes; year2=Yes
elif something "${DIR}/${j}" 365
then
year=Yes; year2=Yes
elif something "${DIR}/${j}" 730
then
year2=Yes
fi

and seperate function:

#!/bin/ksh

kill -9 `ps -ef | grep -v grep | grep "find ${1} -mtime -${2}" | awk '{print $2}'`
rm -f ${3}
echo 0 > ${3}