LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-20-2004, 07:32 AM   #1
linuxtesting2
Member
 
Registered: Mar 2004
Location: Norway
Distribution: Freebsd, Fedora, Redhat, Solaris 10
Posts: 169

Rep: Reputation: 30
newbi gzip


gzip file.gz file1 file2 file3

Why want this zip down file1 2 and 3, and save it with the name file.gz

I just want to compress more than 1 file. When i try

gzip -c fil1 fil2 > files.gz

it works, but the content of fil1 and fil2 is compress to 1 file when i unzip it



-ole
 
Old 10-20-2004, 08:29 AM   #2
paicolman
Member
 
Registered: Sep 2004
Location: Switzerland
Distribution: Fedora Core 2
Posts: 48

Rep: Reputation: 16
try:
tar -cvf tarfile file1 file2 file3
gzip tarfile

I think there is an option to tar and gzip in a single command, but don't know by hart. Do a "man tar" then you'll see...
 
Old 10-20-2004, 09:55 AM   #3
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,185

Rep: Reputation: 52
Quote:
Originally posted by paicolman
try:
tar -cvf tarfile file1 file2 file3
gzip tarfile

I think there is an option to tar and gzip in a single command, but don't know by hart. Do a "man tar" then you'll see...
it's a z. use j for bzip2

tar -cvzf tarfile.gz file1 file2 file3
 
Old 10-20-2004, 10:10 AM   #4
linuxtesting2
Member
 
Registered: Mar 2004
Location: Norway
Distribution: Freebsd, Fedora, Redhat, Solaris 10
Posts: 169

Original Poster
Rep: Reputation: 30
thx that worked just fine, and now i want that to fuction with a find command. I would like to find every log files in a directory and zip them down, but when i use the following command it will go forever...hehe

find . -size +100c -exec tar -cvzf log.gz * {} \;


Wed Oct 20 root@xxx:~/log/# find . -exec tar -czvf log.tar * {} \;
1/
1/P9250850.log
1/P9250852.log
P9250850.log
P9250852.log
./
./P9250850.log
./P9250852.log
........

Wed Oct 20 root@xxx:~/log/#

In that directory i have

Wed Oct 20 root@xxx:~/log/# ls
. .. 1 P9250850.log P9250852.log ......
Wed Oct 20 root@xxx:~/log/# ls -la


The problem is that it will continue forever, another option i would also have is not down zip down files that end with *.log.gz in the same directory, just the files that end with *.log.

Thanks for helping


-ole
 
Old 10-20-2004, 10:28 AM   #5
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,185

Rep: Reputation: 52
edit: do not read. the command will not work, and the explanation is wrong too.


find . "*.log" -exec tar -czvf log.tgz {} \;

that should do it, but i'm drinking, so who knows.

to explain a little, the {} puts the results of the find command there, so if you make the find search specific, you will just get the results of the find command tarred. the * isn't needed.
so in the case of this command, the find command places the filenames it "finds" in the tar command. tar -czvf log.tgz {results of find command}. the \; just terminates the -exec.


Last edited by whansard; 10-21-2004 at 05:26 AM.
 
Old 10-20-2004, 10:31 AM   #6
paicolman
Member
 
Registered: Sep 2004
Location: Switzerland
Distribution: Fedora Core 2
Posts: 48

Rep: Reputation: 16
Prost!
 
Old 10-20-2004, 10:41 AM   #7
linuxtesting2
Member
 
Registered: Mar 2004
Location: Norway
Distribution: Freebsd, Fedora, Redhat, Solaris 10
Posts: 169

Original Poster
Rep: Reputation: 30
ok, but what if i want every files except *.gz files, say that i have *.log *.jpg *.c *.h *.cpp files...how can i just do like this

Find every files and zip them down except *.gz and *.h


-ole
 
Old 10-20-2004, 10:47 AM   #8
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,185

Rep: Reputation: 52
this one.

http://www.newbelgium.com/beer_abbey.shtml
 
Old 10-20-2004, 10:47 AM   #9
linuxtesting2
Member
 
Registered: Mar 2004
Location: Norway
Distribution: Freebsd, Fedora, Redhat, Solaris 10
Posts: 169

Original Poster
Rep: Reputation: 30
Wed Oct 20 root@xxx:~/log# find . "*.log" -exec tar -cvzf tarfile.tgz {} \;
./
./PA160872.log
./PA160874.log
./PA160872.log
./PA160874.log
./tarfile.tgz
tar: ./tarfile.tgz: File shrank by 893260 bytes; padding with zeros
tar: Error exit delayed from previous errors
find: *.log: No such file or directory
Wed Oct 20 root@xxx:~/log#
 
Old 10-20-2004, 11:02 AM   #10
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,185

Rep: Reputation: 52
Quote:
Originally posted by linuxtesting2
ok, but what if i want every files except *.gz files, say that i have *.log *.jpg *.c *.h *.cpp files...how can i just do like this

Find every files and zip them down except *.gz and *.h


-ole
i've seen a command that would do it, but i can't remember.
you may be happy with gzip -r *. it'll skip gzipped files.
i think i messed up on the find command before. it would repetatively make a tar file of one name over and over for each filename. not what you want. I've gotta go.
 
Old 10-20-2004, 12:37 PM   #11
Linux24
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204

Rep: Reputation: 30
Quote:
Originally posted by linuxtesting2
Wed Oct 20 root@xxx:~/log# find . "*.log" -exec tar -cvzf tarfile.tgz {} \;
./
./PA160872.log
./PA160874.log
./PA160872.log
./PA160874.log
./tarfile.tgz
tar: ./tarfile.tgz: File shrank by 893260 bytes; padding with zeros
tar: Error exit delayed from previous errors
find: *.log: No such file or directory
Wed Oct 20 root@xxx:~/log#
Has anyone actually tried this and then extracted the contents to see if it worked? I don't think it will. I think only the last file the find command finds will be zipped. Better to do a find and pipe to tar to zip multiple files found using the find command.
 
Old 10-20-2004, 09:26 PM   #12
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,185

Rep: Reputation: 52
yeah. i hosed it.
find . -name "*.log" -exec tar -rvf tarfile.tar {} \;
then
gzip tarfile.tar

the r will append the tar file, but you can't use that with compression. you have to do it seperately.

keep in mind this leaves the originals in place.
you would extract with tar zxvf tarfile.tar.gz

Last edited by whansard; 10-20-2004 at 09:28 PM.
 
Old 10-20-2004, 10:57 PM   #13
Linux24
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204

Rep: Reputation: 30
Allow me to suggest the simpler approach:

Instead of this:

find . "*.log" -exec tar -cvzf tarfile.tgz {} \;

Try this:

find . -name '*.log' | xargs tar cvzf tarfile_name.tgz

First of all, none of these find suggestions will work because none used the -name option to have a file name. Every one of them would error out.

Also, xargs lets the user pump a completed found list into tar instead of -exec which pumps the found items through one at a time. -Exec is almost never useful. Usually, when you post a question about how to use find, helpful people will run man on find and build a single command line entry with only find using what they see there. But find's -exec is actually kind of lame in most cases, and xargs with a pipe is the better way to go.

But there are many roads to perdition - this is not the only solution.

The difference is in the way the command runs using a pipe and xargs is very important.

Using find with -exec the command runs end to end with each item found.

find --- is something found? If yes --- -exec this command; if no--- go back to find ---if everything done, then exit out.

This results in a problem if you are building a compressed archive of files - every time the command finds a file to compress, it destroys the archive and replaces it with a new one with the same name.

But with xargs, find completes its list before the data is piped to tar, so that when the tar archive is built, all of the files are put into it and it is only built once.

find --- is something found? Add to list. ---- if everything searched --- exit and send list to command by xargs.

Quote:
Find every files and zip them down except *.gz and *.h
This is not that difficult to do.

find . '*'
Will find nothing or everything - it will error out without a name argument.

find . -name '*'
Will find everything.

grep -v .h
Will whittle out of the list anything with .h

grep -v .gz
Will whittle out anything with .gz on the end

Then pipe to tar using xargs (this will be the remaining file name list
find . -name '*' | grep -v .h | grep -v .gz | xargs tar cvfz tarfile.tgz

That should do it. I tested it on my system by creating a directory with four files in it. Three of them were png images, and the fourth was a bz2. I found all files, grep -v'd away the bz2, and tar'd up the rest with xargs just as written above, and it worked just fine.


Last edited by Linux24; 10-20-2004 at 11:00 PM.
 
Old 10-21-2004, 04:26 AM   #14
linuxtesting2
Member
 
Registered: Mar 2004
Location: Norway
Distribution: Freebsd, Fedora, Redhat, Solaris 10
Posts: 169

Original Poster
Rep: Reputation: 30
thanks for all help. i have ended up with the following command:
!
find /map -size +10k -name '*' ! -mtime -365 | grep -v .tar | grep -v .gz | grep -v .tgz | grep -v .zip | grep -v .rar | grep -v .bz2 | grep -v .tar.gz | xargs tar cvfz backup.tgz

It now finds every file that is bigger than 10kilobytes exepct those tgz rar.... and zip does down to a file....


-ole
 
Old 10-21-2004, 04:19 PM   #15
Linux24
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204

Rep: Reputation: 30
Great job! You now know how to figure this stuff out for yourself. Learning "find" is one of the keys to Unix happiness. It is incredibly powerful in concert with xargs and grep.

Now - see if you can figure out how to reduce that command line down to fewer pipes and then you will be on your way.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Solaris 8 gzip issues- cannot gzip -d lynx sixosix Solaris / OpenSolaris 4 03-13-2005 04:17 PM
newbi needs help emek311 Linux - Hardware 1 07-15-2004 10:51 PM
Total Newbi VashTheStampede Linux - Newbie 16 07-24-2003 11:17 PM
A newbi plea for help!! matrim Linux - Newbie 2 12-12-2002 02:48 AM
Help Newbi needs help with named and webserver firestorm Linux - Networking 3 11-28-2000 07:09 AM


All times are GMT -5. The time now is 01:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration