System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

FIND command..stop it from processing sub-dirs

SOLVED
Go to solution
OFC_EDM
Respected Contributor

FIND command..stop it from processing sub-dirs

Is there any way of preventing the find command from processing sub-directories?

I need to find files in
/loc/A greater than a specific date
to /loc/A/archive


This is my test:
-----------------------
1. Touched a reference file
touch -t 200806030000 /tmp/june3.ref

2. Verify (without moving files) that the find command finds files older than June 3rd (Use ! operator with -newer option)

find /loc/A ! -newer /tmp/june3.ref -type f -exec ls -l {} \;

Result:
--------------------------
Lists files older than June 3rd ok.
But descends into /loc/A/archive which is where I want move the files I find from /loc/A.

So this will be a loop. I need to stop the find command from processing files in sub-directories.

I could script this but if there's a way to tell "find" to process the current directory only that would be great.

Cheers
The Devil is in the detail.
14 REPLIES
Laurent Menase
Honored Contributor

Re: FIND command..stop it from processing sub-dirs

use -prune

find /loc/A ! -newer /tmp/june3.ref -type f -o ! -name A -prune ! -type d
OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

I tried -prune without success. It's still descding the sub-directories.

Don't know if this matters but I'm running this on HP Tru64.
The Devil is in the detail.
Laurent Menase
Honored Contributor
Solution

Re: FIND command..stop it from processing sub-dirs

In fact it is:
find /loc/A ! -newer /tmp/june3.ref \( -type f -o ! -name A -prune ! -type d \)

OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

Thanks Laurent

My final command

find . ! -newer /tmp/june3.ref \( -type f -o ! -name A -prune ! -type d \) -exec ls -l {} \;
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

Sorry...update

My final command was

find . ! -newer /tmp/june3.ref \( -type f -o -prune ! -type d \) -exec ls -l {} \;

Took out the ! -name A
Still worked the same as with it in.

Laurent what was the purpose of the ! -name A portion of the command....is it a mistake to leave it out?

Cheers
The Devil is in the detail.
Dennis Handly
Acclaimed Contributor

Re: FIND command..stop it from processing sub-dirs

>what was the purpose of the ! -name A portion of the command....is it a mistake to leave it out?

It skips the directory/file "A". But "-type f" also skips it.
You might also use ! -path "./A/*", but -prune may stop earlier.
OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

odd behaviour

When I closed the thread my output didn't include any of the sub-dirs.

Now it does. gone through my history of commands to see if I did anything different and no luck.

Tried the ! -path option as well without success.
The Devil is in the detail.
James R. Ferguson
Acclaimed Contributor

Re: FIND command..stop it from processing sub-dirs

Hi Kevin:

Use the '-path option' like this:

# find /loc/A -type f ! -path "/loca/A/archive/*" ! -newer /tmp/june3.ref ! -exec ls -l {} \+

I reordered the options a bit to discard non-matches a bit earlier (hopefully). That aside, note the use of the *quoted* '-path' argument so that the shell doesn't expand the "*". Notice too, the faster form of '-ecec' using the "+" terminator to spawn a process with multiple arguments instead of one per argument.

Regards!

...JRF...
OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

Thanks James but the -path option doesn't appear to be valid for Tru64.

I'm slowly building the command up. I can list files in the directory now without the subdirs


EXAMPLE 1 - which does show files in subdirs
> find /tmp/A/*

/tmp/A/B
/tmp/A/B/test2.txt
/tmp/A/test1.txt

EXAMPLE 2 - Dies not show subdirs
root > find /tmp/A/* -prune ! -type d

/tmp/A/test1.txt

Now just have to get the condition in for files older than June 3.

Stay tuned .... :)
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

I believe this is now working....

root > find /loc/A/* -prune ! -type d ! -newer /tmp/june3.ref -exec ls {} \;

I put the /* after the pathname
And reorderd the options.

Here are 3 lines of the output. But I had hundreds of lines and none (verified with grep) had files in any subdirectories.

/loc/A/54482.log
/loc/A/54484.lis
/loc/A/54484.log

Anyone see any potential flaws with this?

Cheers
The Devil is in the detail.
James R. Ferguson
Acclaimed Contributor

Re: FIND command..stop it from processing sub-dirs

Hi (again) Kevein:

> Thanks James but the -path option doesn't appear to be valid for Tru64.

Well, this is the *HP-UX* formum! You should have posted this in the Tru64 UNIX family:

http://forums11.itrc.hp.com/service/forums/familyhome.do?familyId=280

You can ask to have it moved, here:

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1236703

Regards!

...JRF...
OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

I've requested this thread be moved.

Thanks
The Devil is in the detail.
Laurent Menase
Honored Contributor

Re: FIND command..stop it from processing sub-dirs

Why do I put ! -name A ?

because you made the following request:
find /loc/A ! -newer /tmp/june3.ref -type f -exec ls -l {} \;

If you avoid all what I made the first directory encountered is A; so the -prune avoid to continue. and it will display nothing

When you make it from . it is different as we are already in the current directory, so - prune don't cut the display.

Why I didn't use find /loc/A/* -prune ! -type d ! -newer /tmp/june3.ref -exec ls {} \;

because if I have hidden files begining with a "." then they will not be seen.
OFC_EDM
Respected Contributor

Re: FIND command..stop it from processing sub-dirs

I got it working.
Thanks for the help.

I got onto other things. I'll post what I did once I find that darn command I used.
The Devil is in the detail.