1834178 Members
2510 Online
110064 Solutions
New Discussion

compress file

 
peterchu
Super Advisor

compress file

In the directory /tmp , there are some files and new files will be generated into it, I want to regularly archive the files into one by tar , for simple manage the file , I want to add date and time to the end of file ( eg. if I archive it at 10 Aug 05 , 10:00am , the file name should be file.10Aug05-10:00am , could suggest how to do it ? thx in advance.
4 REPLIES 4
Rajeev  Shukla
Honored Contributor

Re: compress file

Do something like this
filename=`date '+%d%b%y-%I%p'`
tar cvf /$filename.tar /tmp/
Does it solve what you want
Devender Khatana
Honored Contributor

Re: compress file

Hi,

It is allthough not suggested to archive all these files as many of these files will be opened by running processes.

Allthough try this by writing a small script and then calling this script manually or through crontab/at.Define following two line apart from other general ones in your script.
========================================
TIMESTAMP=$(date +%m%d.%I%M)
tar -cvf /tmp/tararchive.TIMESTAMP /tmp/files
========================================

This will stamp time in all the files created but your syntax will be slightly different. But this I think is enough for you to start with.

HTH,
Devender
Impossible itself mentions "I m possible"
Muthukumar_5
Honored Contributor

Re: compress file

It is suggested to do this archive execution @ non-working hours. i.e) /tmp directory access has to idle so that it will be successful else problem will come bcas of opened files.

You can do archive as,

tar -cvf archive_$(date +'%d%b%y-%H:%M%p').tar /tmp/*
[[ $? -eq 0 ]] && echo "Tar execution is success @ $(date +'%d%b%y-%H:%M%p')"

hth.

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: compress file

Just change as,

tar -cvf /tmp/archive_$(date +'%d%b%y-%H:%M%p').tar /tmp/*
[[ $? -eq 0 ]] && echo "Tar execution is success @ $(date +'%d%b%y-%H:%M%p')"

hth.
Easy to suggest when don't know about the problem!