Operating System - HP-UX
1828884 Members
2915 Online
109985 Solutions
New Discussion

Using the Find command to Exclude several Directories

 
Jeremy Foland
Advisor

Using the Find command to Exclude several Directories

Hello,

I am trying to use the find command to make some system backups.

I have an Network File system on the drive, and several other directories that I don't want to backup.

I am using HP-UX 9.03 (yeah, yeah, I know...)

The first thing I'm trying to do is just write all of the file names to a file to make sure I'm getting the correct ones, but it isn't working.

The driectories I'm trying to exclude are:
/PE-WRKST
/users/hp4062/measure
/users/hp4062/PPG
/users/hp4062/ON_LINE
/users/hp4062/OFF_LINE

Here is the command I was trying to use:

find . \! \( -path /PE-WRKST -o -path /users/hp4062/measure -o -path /users/hp4062/PPG -o -path /users/hp4062/ON_LINE -o -path /users/hp4062/OFF_LINE \) -print > /users/hp4062/temp/BackTest

I'm not quite sure why I keep getting the excluded directories in my file?

Am I using the -o wrong?
Do I need to Exclude each of the arguments?

Regards,

Jeremy Foland

15 REPLIES 15
H.Merijn Brand (procura
Honored Contributor

Re: Using the Find command to Exclude several Directories

GNU find can do that much more easily:

l1:/u/usr/merijn 102 > nfind --help
Usage: nfind [path...] [expression]
default path is the current directory; default expression is -print
expression may consist of:
operators (decreasing precedence; -and is implicit where no others are given):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
options (always true): -daystart -depth -follow --help
-maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xdev
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -ipath PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm [+-]MODE -regex PATTERN
-size N[bckw] -true -type [bcdpfls] -uid N -used N -user NAME
-xtype [bcdpfls]
actions: -exec COMMAND ; -fprint FILE -fprint0 FILE -fprintf FILE FORMAT
-ok COMMAND ; -print -print0 -printf FORMAT -prune -ls
l1:/u/usr/merijn 103 >

Or you can use perl

# perl -MFile::Find -le 'find(sub{m,^\./(PE-WRKST|users/hp4062/(measure|PPG|ON_LINE))\b,&&return;print$File::Find::name},".")'


But you can also change your own command:

find . | egrep -v -e ^./PE-WRKST -e '^./users/hp4062/(measure|PPG|ON_LINE)' > /users/hp4062/temp/BackTest
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Using the Find command to Exclude several Directories

It's a pain but this is what you have to do; I just do it for 1 directory but you get the idea:

find . ! \( -path '/users/hp4062/PPG' - '/users/hp4062/PPG/*' \) -print

to exclude the PPG directory and any files beneath it. Note the use of single quotes. We do not want the shell to expand the *'s.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Using the Find command to Exclude several Directories

That was a typo it should have been -o (or). The idea is that you must specify both the directory '/mydir' and files/subdirs '/mydir/*' for your syntax to work as expected. You might be able to get by with simply '/mydir*' but that might not have the desired results. e.g. /mydir2 would also be excluded thus specifying only the directory and the 'directory/*' is much safer and robust.
If it ain't broke, I can fix that.
Jeremy Foland
Advisor

Re: Using the Find command to Exclude several Directories

Thanks:

procura - Unfortunately on my ancient relic of a HP-UX system, the nfind and perl commands are unavailable. Thanks anyways.

A. Clay Stephenson ,

Can you explain the minus in the middle of the two single quoted expressions. I don't understand why both expressions are needed. Can you explain a bit more?

find . ! \( -path '/users/hp4062/PPG' - '/users/hp4062/PPG/*' \) -print
Jeremy Foland
Advisor

Re: Using the Find command to Exclude several Directories

This is what I tried:

find . ! \( -path '/PE-WRKST' -o -path '/users/hp4062/measure' -o -path '/users/hp4062/PPG' -o -path '/users/hp4062/ON_LINE' -o -path '/users/hp4062/OFF_LINE' -path '/PE-WRKST/*' -o -path '/users/hp4062/measure/*' -o -path '/users/hp4062/PPG/*' -o -path '/users/hp4062/ON_LINE/*' -o -path '/users/hp4062/OFF_LINE/*' \) -print > /users/hp4062/temp/BackTest

I also tried it with a \! instead.
now I'm trying switching to csh first, and trying the above. I keep getting those directories included.

What about

find . \( -path '/PE-WRKST' -o -path '/users/hp4062/measure' -o -path '/users/hp4062/PPG' -o -path '/users/hp4062/ON_LINE' -o -path '/users/hp4062/OFF_LINE' -path '/PE-WRKST/*' -o -path '/users/hp4062/measure/*' -o -path '/users/hp4062/PPG/*' -o -path '/users/hp4062/ON_LINE/*' -o -path '/users/hp4062/OFF_LINE/*' \)-o -print > /users/hp4062/temp/BackTest

Will that work?

Jeremy Foland
Rodney Hills
Honored Contributor

Re: Using the Find command to Exclude several Directories

If you have some mount points you don't want to traverse, you can include "-xdev" on "find" and it will not cross mount points.

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: Using the Find command to Exclude several Directories

Ooos, that was a typo on my part the '-' should have been -o (or).

The main point is that you need to specify the directory

-path /mydir and any files and subdirs below it -path '/mydir/*'. You might be tempted to specify them both is one shot as '/mydir*' BUT that would exclude '/mydir2' - something you probably did not intend.
If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor

Re: Using the Find command to Exclude several Directories

Jeremy,

in your last example, there doesn't seem a need for the

\(

or the closing \)

and the

-o

after the \)

Nor, is there a need to use apostrophes (').

Is this still an issue?

live free or die
harry
Live Free or Die
Bob_Vance
Esteemed Contributor

Re: Using the Find command to Exclude several Directories

If you're using the Posix, Bourne, Korn, or bash shells (and get rid of 'csh' ;>), your first example *should* have worked, as evidenced in :

find /tmp | grep /tmp/tmp
/tmp/tmp
/tmp/tmp/a b
/tmp/tmp/l1

find /tmp ! \( -path /tmp/tmp \) | grep /tmp/tmp
/tmp/tmp/a b
/tmp/tmp/l1

find /tmp ! \( -path '/tmp/tmp/*' \) | grep /tmp/tmp
/tmp/tmp


and, finally,

find /tmp ! \( -path /tmp/tmp -o -path '/tmp/tmp/*' \) | grep /tmp/tmp



Also, you *definitely* want to use single quotes around any -path arguments that contain wildcard characters.
Otherwise,

find /tmp ! \( -path /tmp/tmp/* \)

would be interpreted by the shell, before passing it to 'find', as:

find /tmp ! \( -path /tmp/tmp/a b /tmp/tmp/l1 \)

which 'find' would choke on:

find: bad option /tmp/tmp/a

You *can* sometimes get by with wildcards and the -name keyword, if you know that no names in your current directory match the -name wildcard argument.

And, yes, removing the "!" and adding the "-o -print" will work, as well.

bv
"The lyf so short, the craft so long to lerne." - Chaucer
ITeam
Super Advisor

Re: Using the Find command to Exclude several Directories

You could try using the egrep command to exclude the directories you dont want. for example.

find . -print | egrep -v 'PE-WRKST|users/hp4062/measure|users/hp4062/PPG|users/hp4062/ON_LINE|users/hp4062/OFFLINE' > /users/hp4062/temp/BackTest


The | symbol is the PIPE symbol, on a PC keyboard this is SHIFT+\.

If you wanted to send it to a tape drive instead, just remove the '> /users/hp4062/temp/BackTest' and put '| tar cvf /dev/rmt/0m'.

You do not have to put the leading / in the any of the excluded directories as the 'find .' does not contain them in its listings.

I hope this is of help to you.

Many thanks, Graeme.
H.Merijn Brand (procura
Honored Contributor

Re: Using the Find command to Exclude several Directories

nfind isn't available on *any* HP-UX system, you'll have to recompile it from source (ftp://download.xs4all.nl/pub/mirror/gnu/findutils/findutils-4.1.tar.gz) [I've installed it as nfind, so I can use it next to find], or get it from a porting center (http://hpux.connect.org.uk/hppd/hpux/Gnu/findutils-4.1.5/), but HP's porting centers do not port to HP-UX 9 anymore.

I neither can help you with a 9.0x port for perl. We don't have 9.0x anymore, but I guess you can still find a rather old version (4.036) of perl on /usr/contrib/bin, which won't recognize the -M option

What was wrong with the (e)grep solution?
Enjoy, Have FUN! H.Merijn
Jeremy Foland
Advisor

Re: Using the Find command to Exclude several Directories

procura ,


I hadn't tried the egrep earlier because I was unfamiliar with it. I tried it earlier today and it seems to work. Thanks.

Can you tell me what the -e option is. That was some pretty fancy use of character matching symbols, but I think I figured it out.

Thanks.

Jeremy
Jeremy Foland
Advisor

Re: Using the Find command to Exclude several Directories

OK, new problem.

Can I cpio -p to a directory on an NFS?

It keeps saying cannot write on /PE-WRKST/hp4062-2

Regards,

Jeremy
A. Clay Stephenson
Acclaimed Contributor

Re: Using the Find command to Exclude several Directories

That depends upon the permissions of the destination directory. If you can create/write a file in the NFS directory then your find command should work.

If it ain't broke, I can fix that.
Jeremy Foland
Advisor

Re: Using the Find command to Exclude several Directories

Actually, I think the problem might be related to my file system structure. I'm gonna post that as a new question since I'm starting to get off subject from the original post.

Thanks for all the help, everyone.