- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script for tar & compressing files
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 06:52 AM
тАО03-05-2009 06:52 AM
need help with tar & compress all files in a folder older than 10 days. Then the script should delete the gz files that are older than 10 days.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 07:01 AM
тАО03-05-2009 07:01 AM
Re: Script for tar & compressing files
What have you tried? Use 'find' to find files ( -type f ) older than 10-days ( -mtime +10 ).
Collect the filenames output and use this list as a argument to 'tar'. Then 'gzip' the tarball. When done, use 'find' again looking for files named "*.gz" that are older than 10-days ( -name "*.gz* -mtime +10 ) and remove them ( '-exec rm {} +' ).
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 07:27 AM
тАО03-05-2009 07:27 AM
Re: Script for tar & compressing files
Does the below script look fine.
cd /tmp
val1=find /tmp -mtime +10
if [$? -eg 0 ]
then
tar cvf $val1 | gzip *.tar
else
echo "No files older than 10 days"
val3=find /tmp -name "*.gz" -mtime +10
echo " List of tar files $val3 "
if [$? -eg 0 ]
then
rm *.gz
fi
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 07:27 AM
тАО03-05-2009 07:27 AM
Re: Script for tar & compressing files
tar [...] | gzip
No need for a (temporary) "tarball" file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 07:46 AM
тАО03-05-2009 07:46 AM
Re: Script for tar & compressing files
OK, you have the general idea. Try this:
#!/usr/bin/sh
cd /tmp || exit 1
FILES=$(find . -type f -mtime +10)
[ -z "${FILES}" ] && { echo "no files older than 10-days"; exit 1; }
tar -cvf - ${FILES} | gzip - > /var/tmp/myarch.gz
find . -type f -name "*.gz" -mtime +10 -exec rm {} +
exit 0
...notice that we use a pipe to 'gzip' as Steven suggested to eliminate an intermediate file.
Noitce too, that the proper word in UNIX is "directory", not "folder" :-)
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 10:55 AM
тАО03-05-2009 10:55 AM
Re: Script for tar & compressing files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 11:04 AM
тАО03-05-2009 11:04 AM
Re: Script for tar & compressing files
> if the gz file already exists then, it should create a new gz file. How do I do that in the script.
You don't need to do anything. The redirection of the 'gzip' STDOUT to the file specified causes the shell to clobber any file already there.
If you prefer, 'rm' the final output file when the script begins:
#!/usr/bin/sh
set -u
GZARCH=/var/tmp/myarch.gz
rm -f "${GZARCH}"
cd /tmp/dummydir || exit 1
FILES=$(find . -type f -mtime +10)
[ -z "${FILES}" ] && { echo "no files older than 10-days"; exit 1; }
tar -cvf - ${FILES} | gzip - > ${GZARCH}
find . -type f -name "*.gz" -mtime +10 -exec rm {} +
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 11:11 AM
тАО03-05-2009 11:11 AM
Re: Script for tar & compressing files
I dont want to delete all gz files. I only want to delete only those gz files which are older than 10 days. And when I run the script, if there is a gz file which is not older than 10 days, then the script should check & create another gz file without deleted the old gz file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 11:25 AM
тАО03-05-2009 11:25 AM
Solution> I dont want to delete all gz files. I only want to delete only those gz files which are older than 10 days. And when I run the script, if there is a gz file which is not older than 10 days, then the script should check & create another gz file without deleted the old gz file.
Then consider this:
#!/usr/bin/sh
set -u
SEARCH=/tmp
GZDIR=/var/tmp
GZFILE=${GZDIR}/myarch_$(date +%Y%m%d).gz
cd ${SEARCH} || exit 1
FILES=$(find . -type f -mtime +10)
[ -z "${FILES}" ] && { echo "no files older than 10-days"; exit 1; }
tar -cvf - ${FILES} | gzip - > ${GZFILE}
find ${GZDIR} -type f -name "*.gz" -mtime +10 -exec rm {} +
rm ${FILES} #...to remove what you archived...
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2009 11:41 AM
тАО03-05-2009 11:41 AM
Re: Script for tar & compressing files
Appreciate your help. I have assaigned points to you.