LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2011, 02:14 PM   #1
MTWiley
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Rep: Reputation: Disabled
Having trouble putting everything together.


I've been working on this for the better part of three days and I'm pretty frustrated at this point, so I thought I'd ask for some help.

I'm trying to put together a script that will check for files older than 18 months and move them to a compressed tar file and I need this script to be able to be run on multiple different folders. It's a bit of a daunting task for me as this is the first script that I've been asked to write, so I decided to break it up.

First I came up with the find command

Code:
find /mnt/networkshares/[Sharename]/ -type f  -mtime +589 > /mnt/networkshares/[Sharename]/[Sharename]-Archive-Log-`date +"%Y-%m-%d"`.txt
Second I came up with the tar command that would take that list and put them into a compress tar file.

Code:
tar --create --file=[Sharename]-Archive-`date +"%Y-%m-%d"`.tgz --files-from=[Sharename]-Archive-Log-`date +"%Y-%m-%d"`.txt --gzip
Then I tried to do something similar with the rm command to remove the original files, but I've hit a crick wall, the list that was originally created isn't escaped and causes errors when it's being fed into rm because there are spaces in a good portion of the file names. So I came up with:

Code:
find /mnt/networkshares/sales/ -type f  -mtime +589 -ls > /mnt/networkshares/sales/Sales-Archive-Log-`date +"%Y-%m-%d"`.txt
This gives me a list of the files with the necessary escapes, but it also gives me a ton of other information that I don't need, so I came up with:

Code:
find /mnt/networkshares/sales/ -type f  -mtime +589 -ls | cut -c68- > /mnt/networkshares/sales/Sales-Archive-Log-`date +"%Y-%m-%d"`.txt
but this leaves some spaces in front of some of the lines and cuts it off perfectly on others.

I spoke w/ my manager about this and he was saying that there is a more efficient way of doing this using for and next loops. I've spent the majority of the morning trying to sort that out, but have been having equally as many if not more problems. If someone could help me out I'd greatly appreciate it. This is what I've got so far:

Code:
#!/bin/bash
SHARES= ls /mnt/networkshares/ | grep -v testscript.sh
for s in $SHARES
do
FILES= find /mnt/networkshares/$s/ -type f -mtime +580 > /mnt/$s/$s-Archive-Log-`date +"%Y-%m-%d"`.txt
tar --create --files=$s-Archive-`date +"%Y-%m-%d"`.tgz --files from=$s-Archive-Log-`date +"%Y-%m-%d"`.txt --gzip
done
and I'll continue to update as I progress, but if you've got any questions, comments, suggestions they'd all be greatly appreciated.

Last edited by MTWiley; 06-24-2011 at 02:16 PM. Reason: corrections
 
Old 06-24-2011, 03:48 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
So what you really want is for tar to take a list of files, read from a file (because a commandline list would probably be too long), and stuff each of those into a tarball, without recursing through subdirectories:
Code:
#
#  Make sure date is the same for all uses in this script
#
now=$(date +"%Y-%m-%d")

fromfile=/mnt/networkshares/[Sharename]/[Sharename]-Archive-Log-${now}.txt

find /mnt/networkshares/[Sharename]/ -type f  -mtime +589 > $fromfile
#
#  Read filenames from list-file
#
tar --no-recursion --files-from $fromfile -cvf
Okay, on further reflection, this is what you already have. I've maybe cleaned it up some.

To remove all of the files, you can loop over each entry in the list file:
Code:
while read filename; do
    echo rm "$filename"
done < $fromfile
Test first (I haven't). When you like, edit out the 'echo' to 'go-live'.

---- rod.

Last edited by theNbomr; 06-24-2011 at 05:00 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble putting together video clips in OpenShot Jeff91 Linux - Software 0 03-01-2010 12:07 AM
Putting this up here as required mojobo LinuxQuestions.org Member Intro 2 09-11-2009 09:03 AM
aving trouble putting knoppix onto my Hd Yona Linux - Laptop and Netbook 1 06-10-2006 11:54 PM
Putting together a new computer... Bombadillow Linux - Newbie 10 01-24-2005 04:13 AM
Putting Wget together werley_123 Linux - Newbie 11 07-14-2003 12:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:33 PM.

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
Open Source Consulting | Domain Registration