How to delete a file based on its creation date in Perl?

My webapp is hosted on a unix server using MySQL as a database.

I wrote a Perl script to run a backup of my database. The perl script is inside cgi-bin folde and works. I only need to install cronjob and run the Perl script once a day.

Backups are stored in a folder named db_backups ,. However, I also want to add a command inside my Perl script to delete any files inside the db_backups folder that are older than they say 10 days ago.

I searched high and low for unix commands and cannot find anything that matches me.

+3
source share
4 answers

if (-M $file > 10) { unlink $file }

or, in combination with File::Find::Rule

my $ten_days_ago = time() - 10 * 86400;
my @to_delete = File::Find::Rule->file()
  ->mtime("<=$ten_days_ago")
  ->in("/path/to/db_backup");
unlink @to_delete;
+9

Unix , .

stat -M ( )/- C ( inode)/- A ( ), ( ).

+4

unix , .

find(1) xargs(1). : .

$ find /path/to/backup -type f -mtime +10 -print0 | xargs -0 echo rm -f

, , (tm), echo. , /path/to/backup, , , 10 , xargs, rm .

(print0 -0 GNU - , Linux), .)

+2
source

You should be able to do this without resorting to Unix commands. Scroll through the files in your directory, use statfor each file to get its latest change time for the file, then use unlinkin the file to delete it if it is larger than you want.

0
source

Source: https://habr.com/ru/post/1715802/


All Articles