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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting stop unix find on a directory structure after finding 1st occurrence
# 1  
Old 06-06-2008
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
# 2  
Old 06-06-2008
please use code tags and identation!

idea: use the -exec parameter with some custom script
Code:
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
  ...

# 3  
Old 06-06-2008
Quote:
Originally Posted by fabtagon
please use code tags and identation!

idea: use the -exec parameter with some custom script
Code:
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 Smilie

John
# 4  
Old 06-06-2008
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}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Finding maximum occurrence value using awk

Hi everyone, I'm a new member at the forum I mistakenly posted this elsewhere too. I have a file like this: field 2 values are either 0 or negative. file test4: 100815 -20 118125 0 143616 0 154488 0 154488 0 154488 -6 196492 -5 196492 -9 196492 -7 27332 0... (5 Replies)
Discussion started by: meet77
5 Replies

2. UNIX for Dummies Questions & Answers

find mv and create directory structure

Hi there, I'm trying to pull all my flacs out of my Music collection. I can do it with following command find b/ -name *.flac -exec mv {} flac/ \; which works great except it moves all the flac files to the flac folder. I want it to recreate the original folder the flacs were found in and mv... (8 Replies)
Discussion started by: fistikuffs
8 Replies

3. UNIX for Dummies Questions & Answers

Finding file in a directory structure + move

Hi All, im a new guy if it comes to Unix. I am trying to auto categorize Nzbget downloads the most basic way. I already manage to find files within the directory i'm at and move them with if check to a certain dir. Unfortunately this command is restricted to the directory i'm at and does not... (2 Replies)
Discussion started by: RoxXxoR
2 Replies

4. Shell Programming and Scripting

find size, cp breaks directory structure

I'm using this now: find /some/path/with/sourcefiles -type f -size -7M -exec /bin/cp -uv {} /some/path/ \; but it doesn't preserve the directory structure, also I've tried it with find /some/path/with/sourcefiles -type f -size -7M -exec /usr/bin/rsync -auv {} /some/path/ \; but that doesn't... (9 Replies)
Discussion started by: unclecameron
9 Replies

5. UNIX for Dummies Questions & Answers

Tree directory structure of Unix to get final node path

Hi, I want to list all the last directories from mentioned base path. for eg: If i have a base path say /base/base1/ How can i get the path till last node in tree like directory structure of unix by applying any command. so that i will get following output. ... (7 Replies)
Discussion started by: Shiv@jad
7 Replies

6. UNIX for Dummies Questions & Answers

Why cant i find my ftp directories in the unix structure

Hi, I'm new here so hello to everyone...i'm also new to linux/unix but got my first dedicated server about 2 weeks ago and have been using ssh to configure it on ubuntu linux. I have a question about something i can't work out....if i use ftp to transfer files ( php, html, javascript files... (2 Replies)
Discussion started by: elduderino
2 Replies

7. UNIX for Dummies Questions & Answers

how to stop to current directory using find

Hello, I just want to ask the following use of find command: 1. how can I find files only to the current directory? 2. how can I find files to directories and all subdiretories (are this include soft links?) but will not go to other mountpoints that is under that mountpoint. Im combining... (1 Reply)
Discussion started by: james_falco
1 Replies

8. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

9. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

10. UNIX for Dummies Questions & Answers

Unix/Linux Directory Structure

Does anyone know of a good Internet source that explains the directory structure of Unix/Linux?? Thanks Gregg (2 Replies)
Discussion started by: gdboling
2 Replies
Login or Register to Ask a Question