Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move and compress files 1

Status
Not open for further replies.

DrSmyth

Technical User
Jul 16, 2003
557
GB
Hi,
Been searching the forum, but can't seem to find the right answer i'm looking ofr...

Basically I need to remove all the log files from a folder, compress them and move them to another folder..
I've come up with this:
Code:
gzip -r $LOG_PATH_STR_PARM
cp $LOG_PATH_STR_PARM/*.gz $LOG_PATH_STR_PARM/archive
rm -f /gcdm/dev/scripts/regulatory_reporting/mcmi/logs/*.gz
which seems to work, but I've been told I should use a tar command as my approach isn't very efficient. Very new to unix, so not sure what to do..
Can anyone help.?
 
Replace this:
cp $LOG_PATH_STR_PARM/*.gz $LOG_PATH_STR_PARM/archive
rm -f /gcdm/dev/scripts/regulatory_reporting/mcmi/logs/*.gz

with this:
mv $LOG_PATH_STR_PARM/*.gz $LOG_PATH_STR_PARM/archive

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Cheers PHV, seems to work great... Thanks for the tip, have a star
 
The problem with using tar is that it is recursive and, as your archive directory is a subdirectory of your log directory you will be archiving your archives.

Supposing you put your archives somewhere else then
Code:
#Create a compressed archive called yymmdd.tar.gz in the archive diretory
tar -czf $archive_dir/$(date =%y%m%d).tar.gz $log_dir
#delete all the log files which were archived.
rm -f $logdir/*

To retrieve a file
[ol]
[li]Identify which archive file it is in by which date it was created[/li]
[li]list the contents of the archive with tar -tzf <archive file>[/li]
[li]extract the file using tar -xzf <archive file> <file to extract>[/li]
[/ol]

Note that the -z flag (to enable compression) is part of GNU tar and not available on all flavours of Unix.

On the internet no one knows you're a dog

Columb Healy
 
From the limited information you have given, your approach is not necessarily inefficient, apart from replacing the copy (cp) and remove (rm) with a move (mv). It all depends on what you want to achieve. I guess that will depend on how many and how often the logfiles are being created. If you want to have an archive directory with lots of individually zipped logfiles, your method is fine. If however you want to 'pack up' the log files (on daily or weekly or monthly basis) into one file and then zip that, then you want to use 'tar' and 'gzip' (or 'compress') - depending on the Operating System.

I hope that helps

Mike
 
Hmmm... It's a monthly task and I'll be removing the previous months log files when a new month batch takes effect..

The archive directory doesn't have to be in the log directory, I've just been playing around trying to get it to work from here..

Also the version of Unix i'm on doesn't support the z tag.

So if i understand the main points I get this:
Code:
gzip $LOG_PATH_STR_PARM*.log
tar -cf $LOG_ARCHIVE_PATH_STR_PARM $LOG_PATH_STR_PARM
rm $LOG_PATH_STR_PARM/*.gz
with LOG_ARCHIVE_PATH_STR_PARM being a directory that is no longer a sub directory of LOG_PATH_STR_PARM.

This runs but i can't find the tar file i've located. sure i'm missing something here!!!
 
Without knowing what your variables are set to it's difficult to advise. However, you should at least be able to find your tar file using a simple find:

find / -name '<value of $LOG_PATH_STR_PARM>'

I want to be good, is that not enough?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top