Operating System - HP-UX
1748165 Members
3772 Online
108758 Solutions
New Discussion юеВ

using tar and grep together

 
Naeem Ali_2
Occasional Contributor

using tar and grep together

hi,

I have a directory structure with 50000 sub-directories, totalling 30 gbytes.

i would like to tar it up, however, what i would lik eto do is grep out sub-directories with certain specific names in them.

e.g. grep -v sun and a grep -v irix or even a grep -v French

is there a way I can do all this on one command line?
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: using tar and grep together

Yes, get GNU's TAR from http://hpux.cs.utah.edu/hppd/hpux/Gnu/tar-1.13.25/

and you can do slick things like:

cd YOURbigDIRECTORY
/opt/tar/bin/tar --create --verbose --file=/var/appl/crazy.tar --exclude="*\/DNS
CACHE_INSTALLDIRhp11.11\/*" --exclude "*\/OraInstall200[2-4]-[0-9][0-9]-[0-9][0-
9]_[0-9][0-9]-[0-9][0-9]-[0-9][0-9]PM\/*" --exclude "*\/PHTXT\/*" --gzip ./*

The exclude will still create the upper directory entry, but it will NOT archive any files under it!


If you use gzip or compress on your archive, then you have to tell the --list option how the archive was compressed (--gzip in this case):
/opt/tar/bin/tar --list --file=/var/appl/crazy.tar --gzip|more

live free or die
harry
Live Free or Die
Rodney Hills
Honored Contributor

Re: using tar and grep together

You could also use "pax" that is now standard with hpux11.

find /mydir -type d -print| grep -v irix | pax -wd -x utar >mysave.tar

HTH

-- Rod Hills
There be dragons...
curt larson_1
Honored Contributor

Re: using tar and grep together

you can always use find with the prune options

find ./ -prune ... | tar ...

an example using prune
http://forums1.itrc.hp.com/service/forums/questionanswer.do?admit=716493758+1084992752438+28353475&threadId=245727

or
find ./ | grep -vE "sun|irix|French" | tar

but you might exclude more then you think that way
john korterman
Honored Contributor

Re: using tar and grep together

Hi,
you could try and bend the find command a litte and use the not operator for excluding filenames, e.g.:
# find . -type f ! -name "*sun*" ! -name "*French*"|xargs tar cvf - >mytar.tar

mytar.tar should be located out of reach of the find command.


regards,
John K.
it would be nice if you always got a second chance
Rodney Hills
Honored Contributor

Re: using tar and grep together

Curt,

"tar" doesn't read the list of files to backup from stdin, I think you are thinking of "cpio".

John,

Unfortunatelly if their are a lot of directories the xargs command could run the tar command multiple times. Thus the file mytar.tar will have whatever the last tar command that was executed.

-- Rod Hills
There be dragons...