delete files older than X minutes
7,165
Simply with GNU find
command (if supported):
find . -type f -mmin +30 -delete
As you've changed your condition, here's updated version:
find . -type f -cmin +15 -delete
Related videos on Youtube
Author by
Anil Kumar
Updated on September 18, 2022Comments
-
Anil Kumar over 1 year
I want to move files and Directories older than 15 min of creation to archive folder in HP Unix but i did not find any option for it. I created a Perl script for it but it is moving files only not directories.
#!/usr/bin/perl use strict; use warnings; use File::Find; use File::Copy; my $dstdir = '/tmp/test14'; @ARGV = ("/tmp/test11/") unless @ARGV; print STDERR "Begin @ ", scalar localtime, "\n"; find( sub { if ( -f $_ && -M _ >= 1/96 ) { print STDERR "Moving '$_'\n"; move( $File::Find::name, $dstdir ) or die "$!\n"; } }, @ARGV ); print STDERR "Ended @ ", scalar localtime, "\n"; 1;
Input:
/tmp/test11# ll drwxrwxrwx 2 root sys 96 Mar 14 21:46 hello drwxrwxrwx 2 root sys 96 Mar 14 21:46 hello1 -rw-rw-rw- 1 root sys 0 Mar 14 22:03 hello3
Output:
/tmp/test14# ll -rw-rw-rw- 1 root sys 0 Mar 14 22:03 hello3
-
Kusalananda about 6 years
-
don_crissti about 6 yearsThis is what happens when you copy-paste scripts... The script only moves regular files because that's what the author wanted:
if ( -f $_
... are you the author ? Then how comes you don't understand what your own script does ? -
haukex about 6 yearsYour title says "delete files older than X minutes", but your post says "I want to move files", which is it?
-
don_crissti about 6 years@haukex - that's a minor detail - you still have to know which ones to move or delete so the operation itself is not that important; what's worse is that OP is using "creation time" when the requirement here seems to be finding files based on "modification time" (at least, that's what OP's script appears to do). Even worse, (based on the comments, answers and upvotes) it looks like people here think
gnu find
'scmin n
checks if the file was created n minutes ago. -
haukex about 6 years@don_crissti Yes, there are several issues with the code. But I ask because I did try to fix the Perl script, only to discover that moving entire directories is not as easy as it sounds -
File::Copy::move
is not guaranteed to work on directories, and I had additional trouble withFile::Copy::Recursive
. If the OP only wants to delete directories, that would make things much easier (so it's not such a minor detail).
-
-
Kusalananda about 6 yearsSimple, if you have a
find
that supports all those fancy GNU options. -
RomanPerekhrest about 6 years@Kusalananda, yes, that's for GNU implementation (specifically)
-
Anil Kumar about 6 yearsI don't have GNU support.
-
ctrl-alt-delor about 6 yearsWhen I was using solaris, I wanted to use the Gnu tool, so I downloaded them.
-
Sparhawk about 6 yearsAlso, the question says "older than 15 min of creation" (pre-edit "older than 30 min of creation"), not modified time (
-mmin
).