combine multiple gzip compressed files in a tar file - bash

I have a really large tar file containing many gzip files. I would like to combine all the gzip files into one gzip file and place in another directory. To view the contents of the tar file I use:
tar -zxvf Big.tar
which gives:
Big/FileA.gz
Big/FileB.gz
Big/FileC.gz
Big/FileD.gz
To normally combine multiple gzip files I would move to that directory and use:
cat FileA.gz FileB.gz FileC.gz FileD.gz > BigFile.gz
However the BigFile.gz would remain in that directory. I'm just not sure how to do this within a tar file and how to have the combined file be written to another directory.

With GNU tar, you can do:
tar -Oxf Big.tar --wildcards 'Big/*.gz' > /tmp/your_file.gz
With OS X tar, you have to list the files individually:
tar -Oxf Big.tar Big/FileA.gz Big/FileB.gz > /tmp/your_file.gz
The salient feature in both is -O, which writes the files to stdout.
Here's an example transcript on a GNU system:
$ pwd
/home/me
$ tar tf Big.tar
Big/
Big/foo.txt.gz
Big/bar.txt.gz
$ tar -Oxf Big.tar --wildcards 'Big/*.gz' > /tmp/your_file.gz
$ zcat /tmp/your_file.gz
This is the contents of foo.txt
This is the contents of bar.txt

Using tar and assuming folder Big has FileA.gz, FileB.gz, etc.:
tar -czv -f /path/to/final/BigFile.gz -C Big .

Related

Using shasum along with compression in MAC OS-X [duplicate]

I can run:
echo "asdf" > testfile
tar czf a.tar.gz testfile
tar czf b.tar.gz testfile
md5sum *.tar.gz
and it turns out that a.tar.gz and b.tar.gz have different md5 hashes. It's true that they're different, which diff -u a.tar.gz b.tar.gz confirms.
What additional flags do I need to pass in to tar so that its output is consistent over time with the same input?
tar czf outfile infiles is equivalent to
tar cf - infiles | gzip > outfile
The reason the files are different is because gzip puts its input filename and modification time into the compressed file. When the input is a pipe, it uses an empty string as the filename and the current time as the modification time.
But it also has a --no-name option, which tells it not to put the name and timestamp into the file. So if you write the expanded command explicitly, instead of using the -z option to tar, you can make use of this option.
tar cf - testfile | gzip --no-name > a.tar.gz
tar cf - testfile | gzip --no-name > b.tar.gz
I tested this on OS X 10.6.8 and it works.
For MacOS:
In man tar we can look at --options section and there we will find !timestamp option, which will exclude timestamp from our gzip archive. Usage:
tar --options '!timestamp' -cvzf archive.tgz filename
It will produce same md5 sum for same files with same names

How to zip more than one file in one folder in shell script

I am having three files one is .txt ,one is .pdf and the other is .csv . How to zip these three files in open folder and rename the folder.
You can do:
zip file.zip file.csv file.pdf file.txt
Use Gzip, compress using:
tar -czvf file.tar.gz file1 file2 file3
And extract using:
tar -xzvf file.tar.gz

tar command - compress without folders

I use a tar command like this:
tar -cf backupdb/db/2012/11/database.2012-11-25.tar.gz backupdb/db/2012/11/25/*.sql
But when I open the tar file I see a directory backupdb/db/2012/11/25 :/
How to modify the command to compress files backupdb/db/2012/11/25/*.sql but without parent directories.
I know the -C switcher in tar command, but I don't know how to use it to compress files with specify exptension.
You are looking for the -C flag:
tar -C backupdb/db/2012/11/25/ -cf backupdb/db/2012/11/database.2012-11-25.tar.gz *.sql

HP-UX - How can I read a text file from tar archive without extracting it?

I have a tar archive which contains several text files. I would like to write a script to display (stdout) the content of a file without extracting it to the current directory.
Actually I would like to do the same as:
tar tf myArchive.tar folder/someFile.txt
cat folder/someFile.txt
rm -R folder
but without the rm...
I tried this way but it didn't work:
tar tf myArchive.tar folder/someFile.txt | cat
Thanks
Use x to extract, with f from archive file. Then add also option -O to direct extracted files to standard output.
tar xf myArchive.tar folder/someFile.txt -O

Why does my shell script create a file named pipe?

I have this simple set up:
pwd
/home/abc/pipetest
ls
mydir pipetest.sh
Now I do:
./pipetest.sh
And then I get
ls
file.tar.bz2 mydir pipe pipetest.sh
My question is: Why did the file named pipe get created? It contains some characters that could not be seen using vi. What's going on?
pipetest.sh contains:
#!/bin/sh
directory_name=mydir
tar cf pipe $directory_name
bzip2 -c < pipe > file.tar.bz2
tar cf pipe $directory_name writes the tar file to a file named pipe.
What you want to do is using the actual pipe:
tar c $directory_name | bzip2 > file.tar.bz2
Or simply use
tar cjf file.tar.bz2 $directory_name
tar -cf pipe
creates a tar file named "pipe" in the current directory.

Resources