Operating System - HP-UX
1822936 Members
3968 Online
109645 Solutions
New Discussion юеВ

How to tar all files except select few

 
SOLVED
Go to solution
Madhu Sudhan_1
Respected Contributor

How to tar all files except select few

Hello,
I remember reading this and also have used it once or twice. Iam unable to recollect now. Could anyone please let me know, how to tar all files except a, b, c, d, e and f

Thanks,
Madhu
Think Positive
12 REPLIES 12
Chris Wilshaw
Honored Contributor

Re: How to tar all files except select few

With the standard tar command, you have to supply the files as a list. If you don't want to do this directly on the command line, you can use something like

tar -cvf /tmp/files.tar `cat /tmp/file_list.txt`

where /tmp/file_list.txt contains the files you want (including the full path)

eg

/etc/passwd
/etc/hosts
/var/adm/syslog
...
...

Stefan Farrelly
Honored Contributor

Re: How to tar all files except select few

You need to firstly create you tar file with 1 file in;

tar cvf

Then use find to find the files/dirs you want, but use the negate -name (! -name ) to exclude those you dont want, eg;

cd /tmp
find . ! -name ! -name -exec tar rvf {} \;

This will append all files in /tmp except file1 and file2 to your tarfile
Im from Palmerston North, New Zealand, but somehow ended up in London...
Thierry Poels_1
Honored Contributor

Re: How to tar all files except select few

Hi,

if you have not a zillion files in your directory, and do not want to exclude a zillion files:

tar cvf tarfile `ls | grep -v -e "^file1$" -e "^file2$" `

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Bernhard Mueller
Honored Contributor

Re: How to tar all files except select few

Madhu,

if a, b, c, etc just mean subdirs or files in the current directory it would be as easy as:

tar cvf /bigdir/tarfile `ls | grep -v -E "a|b|c|d|e|f"`


else it should be something like:

tar cvf /bigdir/tarfile `find . -xdev -type f | grep -v -E "a|b|c|d|e|f"`

Regards
Bernhard

T G Manikandan
Honored Contributor

Re: How to tar all files except select few

you can do something like this

#ls|egrep -v 'a|b|c' >/tmp/a/z;tar cvf a.tar `cat /tmp/a/z`

The best option would be 'find' instead of a 'ls'
Graham Cameron_1
Honored Contributor

Re: How to tar all files except select few

Basically, tar accepts a file mask or masks, so if you can construct a mask for what you want, (and what you don't want), you can do it.

In this case.

tar cvf tarfile a?* b?* c?* d?* e?* f?* [0-9g-z]*

will get them, unless you have wierd filenames beginning with non-alphanumerics.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Madhu Sudhan_1
Respected Contributor

Re: How to tar all files except select few

Graham, you are almost there.

I remember using ![#filename] ( or in some combination) of ! [] #, where filename is name of the file that has to be excluded in the tar.

Unable to get the right combination now.

Thanks,
Madhu
Think Positive
G. Vrijhoeven
Honored Contributor

Re: How to tar all files except select few

Hi,

find . !-name | tar cvf file.tar -

Gideon
Graham Cameron_1
Honored Contributor

Re: How to tar all files except select few

Ah

That would be
tar cvf tarfile !(a|b|c|d|e|f)

--Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Madhu Sudhan_1
Respected Contributor

Re: How to tar all files except select few

Doesn't seem to work. See this.
$ tar cvf ../all.tar !(d.txt | e.txt)
a a.txt 0 blocks
a b.txt 0 blocks
a c.txt 0 blocks
a d.txt 0 blocks
a e.txt 0 blocks
$ tar tvf ../all.tar
rw-r--r-- 47354/20 0 Nov 19 05:11 2003 a.txt
rw-r--r-- 47354/20 0 Nov 19 05:11 2003 b.txt
rw-r--r-- 47354/20 0 Nov 19 05:11 2003 c.txt
rw-r--r-- 47354/20 0 Nov 19 05:11 2003 d.txt
rw-r--r-- 47354/20 0 Nov 19 05:11 2003 e.txt
$

-Madhu
Think Positive
Graham Cameron_1
Honored Contributor
Solution

Re: How to tar all files except select few

Madhu

It doesn't like the spaces.

tar cvf ../all.tar !(d.txt|e.txt)

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Madhu Sudhan_1
Respected Contributor

Re: How to tar all files except select few

:) Amazing. Thanks Graham. You get 10.

-Madhu

Think Positive