[SOLVED] Script for removing files depending on age?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
##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";
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.
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.
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 05:15 PM.
Reason: Typographical corrections and refinement
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.