TAR and ZIP files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers TAR and ZIP files
# 1  
Old 01-31-2012
TAR and ZIP files

Hi,
I need a help with zip and tar. I have no done any scripts before with zip command.

What I need to achieve is list files in a directory with a specific name (ID_DATE format- given examples) and then zip (or gunzip which I need to use, I am not sure) with timestamp on the file name and then take all these files and tar them to one single file and move to another directory.

examples below:

cd landing
PHP Code:
-rw-r--r-- 1 Z06189     bi 16077 Jan 30 12:59 ID_CONTROL_DATE.ID_CTRL.TXT
-rw-r--r-- 1 Z06189     bi 16061 Jan 30 16:33 ID_DATA1_DATE.ID_DATA.TXT
-rw-r--r-- 1 Z06189     bi 16105 Jan 31 08:13 ID_DATA2_DATE.ID_DATA.TXT
-rw-r--r-- 1 Z06189     bi   101 Jan 31 08:42 ID_DATA3_DATE.ID_DATA.TXT
-rw-r--r-- 1 Z06189     bi   101 Jan 31 08:48 ID_DATA4_DATE.ID_DATA.TXT
-rw-r--r-- 1 Z06189     bi 16105 Jan 31 08:51 ID_DATA5_DATE.ID_DATA.TXT
-rw-r--r-- 1 Z06189     bi   146 Jan 31 08:51 ID_DATA6_DATE.ID_DATA.TXT 
cd archival

PHP Code:
-rw-r--r-- 1 Z06189     bi 16077 Jan 30 12:59 ID_CONTROL_DATE.ID_CTRL.TXT_201201311116.z
-rw-r--r-- 1 Z06189     bi 16061 Jan 30 16:33 ID_DATA1_DATE.ID_DATA.TXT.201201311116.z
-rw-r--r-- 1 Z06189     bi 16105 Jan 31 08:13 ID_DATA2_DATE.ID_DATA.TXT.201201311116.z
-rw-r--r-- 1 Z06189     bi   101 Jan 31 08:42 ID_DATA3_DATE.ID_DATA.TXT.201201311116.z
-rw-r--r-- 1 Z06189     bi   101 Jan 31 08:48 ID_DATA4_DATE.ID_DATA.TXT.201201311116.z
-rw-r--r-- 1 Z06189     bi 16105 Jan 31 08:51 ID_DATA5_DATE.ID_DATA.TXT.201201311116.z
-rw-r--r-- 1 Z06189     bi   146 Jan 31 08:51 ID_DATA6_DATE.ID_DATA.TXT.201201311116.
cd archival

PHP Code:
-rw-r--r-- 1 Z06189     bi 16077 Jan 30 12:59 ID_DATE_201201311116.TAR 
I wrote the below script, but it zips all the files into one single file instead of zipping them individually.. can some one help me?

PHP Code:
#### SCript to archieve source file and tar them
#!/bin/sh
date_str=`date "+%Y%m%d%H%M%S"`
logfile=${ENVID}_archival_${date_str}.log
echo Script $0 started at `date` >${logfile}
ENVID=$1
#### Check if one parameter is received#######
if [ $# -ne 1 ]; then
echo "input parameter for the script is missing" >>${logfile2>&1
exit 1
fi
##### Get the ENVID and DATE_OF_RUN from the parameter file####
parmfilename=${ENVID}_parm.parm
DATE_OF_RUN
=`cat $parmfilename | grep "DATE_OF_RUN" | cut -d"=" -f2`
#cd /landing
#### List files based on input parameter####
FILE_LIST=`ls ${ENVID}_TRIGGER_${ENVID}.TRG.TXT | ls $ENVID_*_$DATE_OF_RUN.$ENVID_*.TXT`
echo 
"files are $FILE_LIST>${logfile}
for 
FILE in $FILE_LIST
do
zip $FILE_$(date +"%Y%m%d%H%M%S").zip $FILE
done
echo "Files are zipped" >${logfile
thanks!
# 2  
Old 01-31-2012
.zip is a container, not just a form of compression, which is why you end up with lots of files in one folder.

Try gzip, which is pure compression. gzip 1 2 3 will compress the files 1, 2, and 3 into 1.gz, 1.gz, and 3.gz for example.
# 3  
Old 01-31-2012
thanks for the response Corona688.

Do you mean .gz itself is a package which compresses multiple files to individual .gz files?

I am not sure what happens in my script, I changed to gzip as you mentioned and would like to .gz files individually and then tar them to a single file.

Below is my script which gzips multiple files to one file instead of individual .gz files.
PHP Code:
 
#### SCript to archieve source file and tar them
#!/bin/sh
date_str=`date "+%Y%m%d%H%M%S"`
ENVID=$1
logfile
=${ENVID}_archival_${date_str}.log
echo Script $0 started at `date` >${logfile}
#### Check if one parameter is received#######
if [ $# -ne 1 ]; then
echo "input parameter for the script is missing" >>${logfile2>&1
exit 1
fi
##### Get the ENVID and DATE_OF_RUN from the parameter file####
parmfilename=${ENVID}_parm.parm
echo $parmfilename >>${logfile}
DATE_OF_RUN=`cat $parmfilename | grep "DATE_OF_RUN" | cut -d"=" -f2`
echo 
$DATE_OF_RUN >>${logfile}
#cd /landing
#### List files based on input parameter####
FILE_LIST=`ls $ENVID_*_$DATE_OF_RUN.$ENVID_*.TXT`
echo 
"files are $FILE_LIST>>${logfile}
for 
FILE in $FILE_LIST
do
gzip -c $FILE gzip $FILE_$(date +"%Y%m%d%H%M%S").gz
done
echo "Files are zipped" >>${logfile
can someone please help?
# 4  
Old 01-31-2012
Quote:
Originally Posted by Vijay81
thanks for the response Corona688.

Do you mean .gz itself is a package which compresses multiple files to individual .gz files?
No. A .gz file is a data stream with no particular content. It might be used to hold a single compressed file, like I demonstrated. It might be used to hold a tarball. Lots of different UNIX compressors work like this -- they compress a data stream and don't care one bit about the finer details of the contents. This is very different from a .zip file which knows what the 3000 files in it are.

Often it's paired with tar though, which is a bit like a .zip without compression. The nice thing about tar though, is that it's streamable -- you don't need the entire file to start extracting it, etc.
Quote:
I am not sure what happens in my script, I changed to gzip as you mentioned and would like to .gz files individually and then tar them to a single file.
Any particular reason? Usually what happens is the exact opposite -- create a tarball(bundle of files slapped together with tar), and compress that. That's what a .tar.gz is.

Code:
tar -zcf filename.tar.gz folder/

Without knowing anything about what data your script is reading from it's awful hard to see what's going wrong with it why.
# 5  
Old 01-31-2012
thanks for your inputs corona688. My script is getting some input parameters which helps it to find the file names and then lists all the files with specific names and gunzip and tars them to an archive folder. Timestamp is added as part of the tar-red file name to have an option to store multiple files on a day.

I have modified the script to do tar instead of gzip.. please find the modified script attached, I see that a .tar.gz file is created. But in order to verify that it has tar-red properly, I would like to unzip (or do what ever is required) to see the files back in its original state. Can you please help me with how to get the original files seperately?

Also, I am getting the error message below when I run the script.. can you please advice?

SCRIPT:
PHP Code:
cat archival.sh
#### SCript to archieve source file and tar them
#!/bin/sh
date_str=`date "+%Y%m%d%H%M%S"`
ENVID=$1
logfile
=${ENVID}_archival_${date_str}.log
echo Script $0 started at `date` >${logfile}
#### Check if one parameter is received#######
if [ $# -ne 1 ]; then
echo "input parameter for the script is missing" >>${logfile2>&1
exit 1
fi
##### Get the ENVID and DATE_OF_RUN from the parameter file####
parmfilename=${ENVID}_parm.parm
echo $parmfilename >>${logfile}
DATE_OF_RUN=`cat $parmfilename | grep "DATE_OF_RUN" | cut -d"=" -f2`
echo 
$DATE_OF_RUN >>${logfile}
#cd /landing
#### List files based on input parameter####
FILE_LIST=`ls $ENVID_*_$DATE_OF_RUN.$ENVID_*.TXT`
echo 
"files are $FILE_LIST>>${logfile}
for 
FILE in $FILE_LIST
do
#gzip -c $FILE | gzip > $FILE_$(date +"%Y%m%d%H%M%S").gz
tar -zcf ${ENVID}_$(date +"%Y%m%d%H%M%S").tar.gz /opt/informatica/infa_shared/SrcFiles/arch
done
echo "Files are zipped" >>${logfile
Error message:

PHP Code:
tarRemoving leading `/' from member names
tar: Removing leading 
`/' from member names
tar: Removing leading `/' 
from member names
tar
Removing leading `/' from member names 
Thanks!
# 6  
Old 01-31-2012
Without seeing what your input files look like, I still have no idea what your script is doing. Post a sample, mangled if you must, but a sample of your input data so I can see what your program is supposed to be doing!
Quote:
Originally Posted by Vijay81
thanks for your inputs corona688. My script is getting some input parameters which helps it to find the file names and then lists all the files with specific names and gunzip and tars them to an archive folder. Timestamp is added as part of the tar-red file name to have an option to store multiple files on a day.

I have modified the script to do tar instead of gzip.. please find the modified script attached, I see that a .tar.gz file is created. But in order to verify that it has tar-red properly, I would like to unzip (or do what ever is required) to see the files back in its original state.
What's your system? Some versions of tar have a verify option.

The error message is not an error, by the way, just a warning. It means what it says -- in storing the files, it converts '/path/to/file' into 'path/to/file' so it's not an absolute path any more.

What's more worrying is that you're running it more than once, which makes no sense at all, since tar can hold multiple files.

Please post a sample of your input data! Smilie
# 7  
Old 01-31-2012
Hi Corona,

Please find the input files. What I am trying to achieve is zip all of these files and then tar them to a single file with the timestamp appended to the tar file name.

PHP Code:
-rw-r--r-- 1 z06189 bi 16061 Jan 30 16:33 FERAL_CONTROL_20120115.FERAL_CTRL.txt
-rw-r--r-- 1 z06189 bi     0 Jan 31 11:46 FERAL_CONTROL_20120131.FERAL_CNTRL.TXT
-rw-r--r-- 1 z06189 bi   135 Jan 31 13:14 FERAL_CONTROL_20121213.FERAL_CTRL.TXT
-rw-r--r-- 1 z06189 bi   194 Jan 31 11:53 FERAL_DATA1_20120131.FERAL_DATA.TXT
-rw-r--r-- 1 z06189 bi   102 Jan 31 13:20 FERAL_DATA1_20121213.FERAL_98312.TXT
-rw-r--r-- 1 z06189 bi    91 Jan 31 11:53 FERAL_DATA_20120131.FERAL_CNTRL.TXT
-rw-r--r-- 1 z06189 bi     0 Jan 31 11:45 FERAL_DATA_20120131.FERAL_DATA.TXT
-rw-r--r-- 1 z06189 bi   135 Jan 31 13:19 FERAL_DATA_20121213.FERAL_DATA.TXT
-rw-r--r-- 1 z06189 bi   102 Jan 31 13:20 FERAL_DATA_20121213.FERAL_ssss.TXT 
thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (1 Reply)
Discussion started by: b.saipriyanka
1 Replies

2. UNIX for Beginners Questions & Answers

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (2 Replies)
Discussion started by: b.saipriyanka
2 Replies

3. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

4. Shell Programming and Scripting

Zip Multiple files to One .zip file in AIX system

Hi I have a requirement in unix shell where I need to zip multiple files on server to one single .zip file. I dont see zip command in AIX and gzip command not doing completely what I want. One I do .zip file, I should be able to unzip in my local Computer. Here is example what I want... (9 Replies)
Discussion started by: RAMA PULI
9 Replies

5. Shell Programming and Scripting

help with tar & zip only last months(say,Sep) files

Need to 1. archive all the files in a directory from the previous month into a tar/gz file, ignoring all already archived 'tar.gz' files 2. Check created .tar.gz file isnt corrupted and has all the required files in it. and then remove the original files. I am using a function to get the... (1 Reply)
Discussion started by: Prev
1 Replies

6. UNIX for Advanced & Expert Users

How to zip/tar millions of files?

Hi guys, I have an issue processing a large amount of files. I have around 5 million files (some of them are actually directories) in a server. I am unable to find out the exact number of files since it's taking "forever" to finish (See this thread for more on the issue). Anyway, now I... (6 Replies)
Discussion started by: verdepollo
6 Replies

7. AIX

ZIP multiple files and also specify size of zip file

I have to zip many pdf files and the size of zip file must not exceed 200 MB. When size is more than 200 MB then multiple zip files needs to be created. How we can achieve this in UNIX? I have tried ZIP utility but it takes a lot of time when we add individual pdfs by looping through a... (1 Reply)
Discussion started by: tom007
1 Replies

8. Shell Programming and Scripting

To write a shell script which groups files with certain pattern, create a tar and zip

Hi Guru's, I have to write a shell script which groups file names based upon the certain matching string pattern, then creates the Tar file for that particular group of files and then zips the Tar file created for the respective group of files. For example, In the given directory these files... (3 Replies)
Discussion started by: rahu_sg
3 Replies

9. UNIX Desktop Questions & Answers

Using Tar Zip

Hi, I want to backup my SQL database using tar zip but I'm paranoid that I will archive it. What I mean is I want the files to stay where they are but make a zipped copy of the files as well, I don't want to delete the originals. Is the command? tar -cvzf databasename.tar.gz... (1 Reply)
Discussion started by: chickenhouse
1 Replies

10. UNIX for Dummies Questions & Answers

unzip .zip file and list the files included in the .zip archive

Hello, I am trying to return the name of the resulting file from a .zip archive file using unix unzip command. unzip c07212007.cef7081.zip Archive: c07212007.cef7081.zip SecureZIP for z/OS by PKWARE inflating: CEP/CEM7080/PPVBILL/PASS/G0063V00 I used the following command to unzip in... (5 Replies)
Discussion started by: oracledev
5 Replies
Login or Register to Ask a Question