Operating System - Tru64 Unix
1748288 Members
3288 Online
108761 Solutions
New Discussion юеВ

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 14
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.