LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-06-2011, 09:34 PM   #1
mensaman
LQ Newbie
 
Registered: Jun 2011
Location: Alabama
Distribution: openSUSE
Posts: 5

Rep: Reputation: 10
Post Script for removing files depending on age?


I want to develop a bash or Perl script that will evaluate files in the local directory and delete every file that is older than a specified time period, such as three months.

I write documents and save the latest modifications by putting the current date in the filename, so I can easily see which versions are the newest. I would like to clean up my directory every once in a while, though, by eliminating the copies that are more than a few months old.

Thanks for your help.
 
Old 06-06-2011, 09:41 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!


And what is the problem you hit in that approach?



Cheers,
Tink
 
0 members found this post helpful.
Old 06-07-2011, 03:28 AM   #3
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 94

Rep: Reputation: 4
I presume that your problem is the script it self. I could not get all the way to full script status, but here is what I got so far..

ls -l | grep Apr | awk '{ print $9}'

The first part is the ls command. You can sort your information however you want it sorted with this section...
The second part sets WHAT you are looking for. You must be exact in what you type in or it will not work. I THINK that you maybe able to specify multiple months.
The third part sets WHERE you are looking. In this case, the nineth field of ls.

I would like to know if you get any further with your script.
 
1 members found this post helpful.
Old 06-07-2011, 03:42 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Quote:
Originally Posted by lonesoac0 View Post
I presume that your problem is the script it self. I could not get all the way to full script status, but here is what I got so far..

ls -l | grep Apr | awk '{ print $9}'

The first part is the ls command. You can sort your information however you want it sorted with this section...
The second part sets WHAT you are looking for. You must be exact in what you type in or it will not work. I THINK that you maybe able to specify multiple months.
The third part sets WHERE you are looking. In this case, the nineth field of ls.

I would like to know if you get any further with your script.
grep Apr isn't dynamical, i mean, if he uses that, then he needs to adjust every month his script...
I've got something similar.
I search for files in certain folders, and when it's older than 2 days, i need to get a message
so, with minor adjustements, this should work?
Correct me if i'm wrong

Code:
	statresult=`$find $MAIN/some/folder -type f | $wc -l`
	dirsize=`echo $statresult`

	if [ $dirsize -gt 0 ]; then
		VARm=`$find $MAIN/some/folder -name "*" -type f -mtime 90 | $wc -l`
		
		if [ $VARm -gt 0 ]; then
                        <code for deleting files>
			echo '$Varm files which are older than  3 months'
                fi
       else
			echo 'No files older than 3  months'
		
	fi
so what i do is, i search for files in a certain folder, when he founds some files, then he needs to delete them (but i don't know how to delete a group of files)
probably it needs a little bit of tweaking, but i think you're on your way now?

i use a lot of variables, because i defined them at the start of my script, so in case that a location of a command has changed (for some reasons) i need to change only 1 piece of my code
 
1 members found this post helpful.
Old 06-07-2011, 04:52 AM   #5
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
Pasting the program which I am using.. Hope it may be useful.
This will remove files as well as directories.
You can modify it accordingly.

#!/usr/bin/perl

use strict;
use warnings;
use File::Path;
##Variables used##

###########Sub routine######################

sub del_old {

my $dir=$_[0];
my $age=$_[1];
print "Trying to open $dir\n";

opendir(D, "$dir") or die $!;
chdir $dir;

my $time = time();
while (my $f = readdir(D)) {
next if $f =~ /^\./;

my ($atime, $mtime, $ctime) = (stat($f))[8..10];
my $age_hours = ($time - $mtime) / 3600;
my $age_days = int($age_hours / 24);

next unless $age_days > $age;

print "---> Deleting $f ($age_days days)...";
if (!-l $f && -d $f ) {

rmtree($f) or warn "couldn't rmdir $f: $!";

}
else {

unlink $f;

}

print " done\n";

}

closedir(D);

}

##Main Starts Here##
print "How many days old files or directories you want to keep:";
chomp(my $age=<STDIN>);

chomp(my $dir=`pwd`);

print "WARNING!!!!! Deleting Files and directories older than $age days from $dir\n";
print "Please enter 'I AGREE':";
chomp(my $agree=<STDIN>);
if ($agree=~/I AGREE/){

print "Waiting for 5 seconds\n";
sleep 5;
del_old($dir,$age);

print "\n";
print "=" x 80,"Done\n";
print "\n";

}

else {

print "Coming out of program. Not deleting any directories or files\n";

}
 
1 members found this post helpful.
Old 06-11-2011, 10:55 PM   #6
mensaman
LQ Newbie
 
Registered: Jun 2011
Location: Alabama
Distribution: openSUSE
Posts: 5

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by Tinkster View Post
Hi, welcome to LQ!


And what is the problem you hit in that approach?



Cheers,
Tink

Time. I have written a couple Perl scripts, but I still have much to learn and very little time to research it. A gentleman at work was looking for a way to delete all files past a particular age, but I was struggling to find the time to work it out myself.
 
Old 06-11-2011, 10:58 PM   #7
mensaman
LQ Newbie
 
Registered: Jun 2011
Location: Alabama
Distribution: openSUSE
Posts: 5

Original Poster
Rep: Reputation: 10
Talking

Quote:
Originally Posted by ssrameez View Post
Pasting the program which I am using.. Hope it may be useful.
This will remove files as well as directories.
You can modify it accordingly.

#!/usr/bin/perl

use strict;
use warnings;
use File::Path;
##Variables used##

###########Sub routine######################

This is perfect! Exactly what I was in need of, but lacked the time to figure it out. Thank you very much for your post!
 
Old 06-11-2011, 11:02 PM   #8
mensaman
LQ Newbie
 
Registered: Jun 2011
Location: Alabama
Distribution: openSUSE
Posts: 5

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by lonesoac0 View Post
I presume that your problem is the script it self. I could not get all the way to full script status, but here is what I got so far..

ls -l | grep Apr | awk '{ print $9}'

The first part is the ls command. You can sort your information however you want it sorted with this section...
The second part sets WHAT you are looking for. You must be exact in what you type in or it will not work. I THINK that you maybe able to specify multiple months.
The third part sets WHERE you are looking. In this case, the nineth field of ls.

I would like to know if you get any further with your script.
ssrameez hit the nail on the head for what I was in need of, but this was very useful information, as well. Thank you.
 
Old 06-12-2011, 12:08 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by mensaman View Post
This is perfect! Exactly what I was in need of, but lacked the time to figure it out. Thank you very much for your post!
Is it? From your description it sounded like you
wanted to delete them based on the name rather
than on a time-stamp. How odd.



Cheers,
Tink
 
Old 06-12-2011, 03:07 PM   #10
mensaman
LQ Newbie
 
Registered: Jun 2011
Location: Alabama
Distribution: openSUSE
Posts: 5

Original Poster
Rep: Reputation: 10
Tinkster,

I'm in the habit of including the revision date in the filename to make it easier on myself and others to determine which file represents the newest version, but without automatically replacing prior revisions, in case we want to look back at an earlier copy. The time stamp of the file, itself, will not only allow me to remove my personal files beyond a particular date, but will also allow me to do so for other files on the system, as well. So, this script is very helpful in accomplishing that broader application, in spite of the narrow scope of my initial post.

Last edited by mensaman; 06-12-2011 at 06:15 PM. Reason: Typographical corrections and refinement
 
  


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
[SOLVED] Removing odd characters from CSV files using a script? carbo Programming 7 04-28-2010 03:42 AM
Need rpm for openldap with depending files rajaselvam@ Linux - Software 7 11-17-2009 07:06 PM
Removing program diretories and files with a bash script wimnat Linux - Software 1 05-25-2009 09:55 PM
script for removing 30day old files but keeping *.zip timnewton Red Hat 7 10-14-2008 04:50 AM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 11:12 AM


All times are GMT -5. The time now is 02:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration