LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2011, 03:19 PM   #1
iwitham
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
I am attempting write a shell script to compare date/time stamps in a directory


I am attempting to write a shell script, to become part of a cron, that will check the date/time stamp of files in a specific directory for file older the 24 hours. Then create a tar file of the identified file and remove the old files. I currently have the process to locate the old files using 'find':

find /<path/to/directory/> -mtime +1 -type f -exec ls -la {} \;

But am lost. Any help would be appreciated.
 
Old 09-09-2011, 03:35 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Well, you're 99% of the way there.

Using your find command, which is giving you a list (forget about the -exec ls -la {} \; part, you don't need it for what you want to do), simply use find to get a list of files in a temporary file (say, /tmp/filelist or something). That would be
Code:
find <path-to-direcoty> -mtime +1 > /tmp/filelist
Then, create your tar archive
Code:
cat /tmp/filelist | tar -cvf filelist.tar -
Then, use the same list to remove the files
Code:
rm `cat /tmp/fielist`
Just in case the list of file is quite large, you can use find to do that instead (and here's where you can use -exec)
Code:
find <path-to-direcoty> -mtime +1 > /tmp/filelist -exec rm {} \;
or you could use xargs
Code:
cat /tmp/filelist | xargs rm
Finally, remove the temporary file
Code:
rm /tmp/filelist
Wrap that up pretty in a shell program and viola!

Hope this helps some.
 
Old 09-12-2011, 07:05 AM   #3
iwitham
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks for the pointers. This was very helpful. I did have to go with find <path-to-direcoty> -mtime +1 > /tmp/filelist -exec rm {} \; due to the immense file size. I have been able to successfully create a cron with this and a few other commands that does what I needed.
 
Old 09-12-2011, 08:20 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Something I forgot to mention above (twice, now) was that it may be useful to restrict find to looking only for files (or not to look at directories as it were); e.g.,
Code:
find <path-to-direcoty> -type f -mtime +1 > /tmp/filelist
If there are any sub-directories involved they will show up in the list using -type f so there's no problem doing so.

Anyway, glad to know you got going.
 
  


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
Please help me write shell script to check the modified date of a file. thinhnguyenduy79 Linux - Newbie 1 06-17-2011 04:14 AM
are linux date/time stamps correct? Trio3b Linux - General 4 11-18-2010 11:49 AM
New to linux and have a question about a shell script I'm attempting to write trace_effect Linux - Newbie 9 12-11-2009 01:23 AM
Using sed in a shell script to add date and time seefor Programming 3 02-25-2009 01:21 PM
Shell Script - Date/Time runnerpaul Programming 9 08-20-2008 07:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:09 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