Showing posts with label delete old files. Show all posts
Showing posts with label delete old files. Show all posts

Feb 14, 2014

Python Delete old files

Many a times, we come across deleting old files like logs, deprecated files from file system


We come across scenarios like 

To delete logs older than x days
To delete txt files older than x days

Let us discuss how we can achieve deleting old files using python script

What does the script do?
It will take the path from where you need to delete the txt files
The below program checks the time stamp of the files and deletes the txt files older than 7 days.
You can change the file extension, folder path and use it.
folder_path = "C:\Files_To_Read"
file_ends_with = ".txt"
how_many_days_old_logs_to_remove = 7

How to call the script?
python delete_old_logs.py

delete_old_logs.py

import os, time, sys

folder_path = "C:\Files_To_Read"
file_ends_with = ".txt"
how_many_days_old_logs_to_remove = 7

now = time.time()
only_files = []

for file in os.listdir(folder_path):
    file_full_path = os.path.join(folder_path,file)
    if os.path.isfile(file_full_path) and file.endswith(file_ends_with):
        #Delete files older than x days
        if os.stat(file_full_path).st_mtime < now - how_many_days_old_logs_to_remove * 86400: 
             os.remove(file_full_path)
             print "\n File Removed : " , file_full_path
 

May 15, 2012

Delete logs older than 7 days

#! /usr/bin/perl

use strict;
use warnings;
#use CGI;
#use CGI::Carp qw(fatalsToBrowser);   
use Data::Dumper;

foreach my $file (</test/logs/log_*.txt>) {   
    if ( -M $file > 7 ) {
        print "\n Deleting the log file more than 7 days old: " . $file;
        unlink $file; #or die "\nFailed to remove $file: $!";
    }
}

print "\n\n";


1;


Note:
  • The above program is based on the time stamp of the files. 
  • The program detects files staring with 'log_' format and deletes these files whose time stamp of the file is greater than 7 days (since the current time stamp).
  • '-M' is used to check the timestamp
  • 'ulink' is used to remove file 
  • Please change the corresponding shebang line  (#! /usr/bin/perl) if you are using windows environment to test this perl script
  • In the above example, in the specified path (/test/logs/) I have some files like... Let us assume we have files like this 
log_1.txt (7 days old)
log_2.txt (8 days old)
log_3.txt (9 days old)
log_4.txt (10 days old)
log_5.txt (3 days old)
log_6.txt (4 days old)
error_log.txt (10 days old)
server_log.txt (10 days old)

Program Output: The program will delete files
log_1.txt, log_2.txt, log_3.txt, log_4.txt

Since the files 'error_log.txt' and 'server_log.txt' won't start with 'log_' the program don't delete them irrespective of the timestamp.


Apr 26, 2012

Delete files older than X days on Linux


find /path/to/files* -mtime +5 -exec rm {} \;

This is an example, which deletes files older than 5 days from unix command line itself

Note:
1)  Apply this from unix command line itself