Operating System - HP-UX
1833548 Members
3139 Online
110061 Solutions
New Discussion

Re: Find command - directory & file selection

 
SOLVED
Go to solution
Kevin McKie
Occasional Advisor

Find command - directory & file selection

Hi, folks.

I have an archive directory containing several subdirectories, one of which is called "secure". I need to compress all files in all the subdirs (using compress -f or compressdir -f) EXCEPT the files in "secure" - they may be needed at an instant's notice. Your help would be much appreciated.
Si monumentum requiris, ecce Washington.
15 REPLIES 15
Peter Kloetgen
Esteemed Contributor

Re: Find command - directory & file selection

Hi Kevin,

the following command should do it for you:

find /path/to/directory -type d -print | grep -v 'secure' | compressdir -f

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Kevin McKie
Occasional Advisor

Re: Find command - directory & file selection

Thanks, Peter.

That worked OK as it stood, but then I started experimenting. I tried to identify a single directory for compression using:

find . -type d | grep pioneer | compressdir -f

(where pioneer is another of the sub-dirs) and every single sub-dir in the archive directory was compressed; the grep seems to have been ignored.

My original intention was to include in a log a count of the files to be compressed (i.e. everything not ending in .Z), which I did with:

UnCompFiles=$(find . -type f -name "*[!Z]" -print | wc -l)

Unfortunately, that includes the files in the secure sub-dir. If possible, I'd like to know how to find uncompressed files in all archive sub-dirs except the secure sub-dir. A pint of warm, flat British beer to the first correct answer!
Si monumentum requiris, ecce Washington.
Kevin McKie
Occasional Advisor

Re: Find command - directory & file selection

Hi, Peter et al. -

Scratch part of my last message - I missed out the "-print" from my find. It now identifies the pioneer directory accurately, but compressdir -f doesn't work properly; it doesn't compress empty files, a requirement of mine.

So it now looks as though my definitive question is: how do I identify uncompressed files in sub-dirs other than secure so that I can compress them using compress -f?

Thanks again,

Kevin
Si monumentum requiris, ecce Washington.
Peter Kloetgen
Esteemed Contributor

Re: Find command - directory & file selection

Hi again Kevin,

you want to know the number of all files which where compressed, but no files to be compressed which are under subdirectory secure?

Here we go.....

var=`find /path -type d -print | grep -v 'secure' | ls -Al | wc -l`

That should do it for you....

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Steve Steel
Honored Contributor

Re: Find command - directory & file selection

Hi

UnCompFiles=$(find . -type f -name "*[!Z]" -print |grep -v /secure/| wc -l)

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Peter Kloetgen
Esteemed Contributor

Re: Find command - directory & file selection

Hi the third time Kevin,

sorry for my last posting, there is a little error in it:

ls -Al --> output are "allmost" all entries, without "." and "..", but also a header line, named "total" and then the number of blocks of directory. Either you forget the -l option or you include the command "grep -v total" before the command "wc -l"

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Kevin McKie
Occasional Advisor

Re: Find command - directory & file selection

Hi, Peter & Steve -

You've both been incredibly helpful, so thank you for your kind patience. Warm British beers all round!

Your answers to stripping out the "secure" sub-dir both involve piping find's output to grep; what I'm really trying to get to here is how to use *only* the find command. That way, I can use the -exec primary to call compress -f as mentioned in an earlier posting. I've been experimenting with the -path, -prune and -only primaries but haven't been successful.

A juicy 10 points to whoever shows me how to read the sub-dirs as I'd like, using only find's primaries :-)

Honest - this'll be my last question for a while!
Si monumentum requiris, ecce Washington.
Darren Prior
Honored Contributor

Re: Find command - directory & file selection

Hi Kevin,

You may wish to leave off the compressdir -f whilst you're experimenting so you can just see the list of directories that will be passed to compressdir.

To answer your query about counting the files, you need to filter out any files which have 'secure' in the returned path. I'd suggest changing your line to:

UnCompFiles=$(find . -type f
-name "*[!Z]" -print | grep -v 'secure' | wc -l)
If an individual file is likely to contain the word secure then it might be better to include the trailing slash after secure in the grep statement.

regards,

Darren.
Calm down. It's only ones and zeros...
H.Merijn Brand (procura
Honored Contributor

Re: Find command - directory & file selection

Also study 'man xargs', which might prove useful in your quest

just another ??? 0.02
Enjoy, Have FUN! H.Merijn
Peter Kloetgen
Esteemed Contributor
Solution

Re: Find command - directory & file selection

Hi Kevin,

i just had a closer look to the man page of the find- command. Brrrrrrrr....

In my opinion it's not possible to solve the problem using only the find command because you don't get rid of the subdirectory "secure". But for the reason our solutions worked, why think over a problem that is no longer existant? Pipelining is very often the only way to get something to work, we should be glad to have such a powerful tool.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Jose Mosquera
Honored Contributor

Re: Find command - directory & file selection

A shell-script solution:

cd
DIRS=`ls -p|grep /|grep -v secure`
for DIR in $DIRS
do
compressdir -f $DIR
done

Regards
Kevin McKie
Occasional Advisor

Re: Find command - directory & file selection

Hello, everyone -

First of all, thank you all for your kind help and interest in my wee problem. I was heartened to receive such patient attention and so many solutions.

I'm sure you'll be pleasantly surprised to hear that I found the find-only solution I was looking for:

find . -type -f ! -name "*.Z" ! -path "./secure/*" -exec compress -f {} \;

In any case, I've learned a lot today - thank you again.

TTFN from bonnie Edinburgh.

In the words of Young Mr. Grace: "You've all done very well!"
Si monumentum requiris, ecce Washington.
Kevin McKie
Occasional Advisor

Re: Find command - directory & file selection

Sorry, folks -

An additional hyphen crept in. Last posting should have read:

find . -type f ! -name "*.Z" ! -path "./secure/*" -exec compress -f {} \;

Regards,

Kevin
Si monumentum requiris, ecce Washington.
Peter Kloetgen
Esteemed Contributor

Re: Find command - directory & file selection

Hi Kevin,

thank you! Now i learned something new.... Negation with the find- command! :-)

no points for this please

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
H.Merijn Brand (procura
Honored Contributor

Re: Find command - directory & file selection

Ever considered using GNU find? It has very powerful options like -regex.

http://hpux.cs.utah.edu/hppd/hpux/Gnu/findutils-4.1.5/
Enjoy, Have FUN! H.Merijn