LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-03-2015, 08:41 AM   #1
Venky58
LQ Newbie
 
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled
Need Help to Implement a Shell Script for Deleting Directories Older then 122 Days


Hi All,

I need to implement Unix Shell Script for deleting the directory which is older than 122 Days.

The directories will be in below format.

06_12_2014

all the files which are older than 122 days by date have to be deleted.


/data/Isol/backup/06_12_2014

Thanks in advance for your help.
 
Old 02-03-2015, 11:31 AM   #2
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
What have you tried? What shell do you want to use? In perl, this should do it:
Code:
#!/usr/bin/perl -w

use strict;
use Time::Local;

my $path = ARGV[0];
die "Please specify which directory to search"
   unless -d $path;

my $curtime = time;

opendir ( my $DIR, $path);
while ( my $entry = readdir $DIR ) {
   next unless -d $path . '/' . $entry;
   next if $entry eq '.' or $entry eq '..';
   print "Checking directory $entry.\n";
   my ( $mon, $day, $year ) = split (/_/, $entry );
   my $dtime = timelocal(0,0,0,$day,$mon,$year);
   my $days_diff = int (( $curtime - $dtime ) / 86400 );
   if ( $days_diff > 122 ) {
      print "Delete $entry\n";
   }
}
closedir $DIR;
I haven't extensively tested it.
 
Old 02-09-2015, 07:52 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
One problem you will have is that a directory is not modified when files within the directory are modified. And deleting a file will also update the directory modification time.

Another is that subdirectories will isolate newly created files from being reflected in any dates of old directories...

So you can only delete directories that are empty... and older than a given time.

Last edited by jpollard; 02-09-2015 at 07:55 AM.
 
Old 02-09-2015, 11:13 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,249

Rep: Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836
and you do not need to write a script, because the command find itself can handle it (just look at the man page and the examples about deleting old files/empty dirs)
 
Old 02-09-2015, 11:22 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
The problem is that even find is not likely to work for what is being requested...

The time delay to delete directories is AT LEAST twice the time delay to delete files... And since it is a recursive activity, it tends to accumulate rapidly with the depth of the directory tree.

I don't think find records timestamps before it scans a directory (scanning will update access times...) as that starts to require a relatively unbounded stack. I admit, stack usage isn't that critical anymore, but still, it is something to think about.
 
Old 02-10-2015, 01:33 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,249

Rep: Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836
if you do not want to explain things but implement the original requirement: find is completely capable doing it. OP did not tell anything about the content of those directories, so probably there will be no any conflict with the files inside.
(since it was a backup directory probably all the files inside are older...)
 
Old 02-10-2015, 05:33 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
well, all but one... the last one put in the directory.
 
Old 02-10-2015, 12:48 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,788
Blog Entries: 13

Rep: Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831
I wonder where the specification of 122 days came to be ... Or is this an assignment?

If directory names represent the date, then providing the naming rules are accurate and consistent you should be able to determine based on current date/time what 122 days back is and then perform this deletion, a script would be just computing the correct file name point.

Please make some sort of script effort, post what you have, where you're stuck. And if it is homework, then it is OK to say that, but yes the right thing to do is work at it, and ask questions, not for the forum members to suggest an exact script for you.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to remove print jobs older than xx days randy-e Linux - Newbie 13 12-20-2013 01:13 PM
Find the directories which is older than x days delete and zip those directories ramesh pagadala Linux - Newbie 2 08-29-2013 08:17 AM
Delete backups older than 7 days through shell script Rk_Raj Linux - Newbie 4 04-09-2013 02:47 AM
Shell script to copy all folders older than 7 days from Linux to Windows by samba blackthu80 Linux - General 4 03-17-2011 12:29 AM
shell script - auto ftp files that older than 60 days bakeng Linux - Newbie 2 12-25-2007 10:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:00 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
Open Source Consulting | Domain Registration