LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-17-2012, 06:32 PM   #1
blacklion
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
Script: find, compress, remove original files, older than x days?


I need to search a single directory, find all files older than X amount of days, compress the files, and remove the originals. I have a script that works now, but I am sure there is a more efficient way to do what I need to do...

Code:
#!/bin/sh

DATE=`date '+%Y-%m-%d'`

tar -cvf Log_File_Backups_$DATE.tar --files-from /dev/null

find . -maxdepth 1 -type f -name "*test.log*" -mtime +7 -exec tar --remove-files --append --file=Log_File_Backups_$DATE.tar '{}' \; && gzip -c Log_File_Backups_$DATE.tar > Log_File_Backups_$DATE.tgz && rm Log_File_Backups_$DATE.tar
 
Old 02-17-2012, 09:02 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
It is probably more efficient to:
  1. use the find command to build a list of files to archive
  2. run tar with options:
    • -T (or --files-from), naming the file created by find
    • -z (or --gzip)
 
1 members found this post helpful.
Old 02-20-2012, 12:27 PM   #3
blacklion
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the assistance, that's what I was really wanting to do but was having some issues with the --files-from, the -T worked better.

Here is what I have now to compress any log files older than 7 days:
Code:
DATE=`date '+%Y-%m-%d'`
cat /dev/null > log_to_backup.txt
find . -maxdepth 1 -type f -name "*.log*" -mtime +6 | sort -k 1 -n >> log_to_backup.txt
tar -zcf Log_File_Backups_$DATE.tgz -T log_to_backup.txt
while read file; do rm "$file"; done < log_to_backup.txt
 
Old 02-20-2012, 12:36 PM   #4
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
files=( $( find . .......  )   )

tar -zcf LogFileBAckups${DATE}.tgz  ${files[*]}
rm ${files[*]}
Works until system can't handle the length of command line anymore.
 
Old 02-20-2012, 12:43 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by blacklion View Post
Thanks for the assistance, that's what I was really wanting to do but was having some issues with the --files-from, the -T worked better.
Thanks for sharing your solution

find's --files-from and -T options ought to be exactly equivalent.

Threads can be marked SOLVED vi the Thread Tools menu.
 
Old 02-27-2012, 11:22 AM   #6
blacklion
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
Thanks for sharing your solution

find's --files-from and -T options ought to be exactly equivalent.
Yeah, it should have been, but the OS (which is based off RHEL5) has been hardened by the developers of the software and it won't accept the --files-from. Finding a lot of commands/options they have removed now that I am trying to write custom scripts.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to remove files older than 3 days rust8y Linux - General 26 10-04-2012 09:26 AM
script to compress a directory and then delete older than 7 days replica88 Linux - General 3 02-03-2010 08:21 PM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 04:43 PM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 09:30 AM


All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration