2

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
  • 1
    Are you using HP Unix's standard find (no -cmin option), or do you have access to GNU find? – Kusalananda Mar 14 '18 at 6:32
  • This 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 ? – don_crissti Mar 14 '18 at 15:18
  • 1
    Your title says "delete files older than X minutes", but your post says "I want to move files", which is it? – haukex Mar 14 '18 at 21:34
  • @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's cmin n checks if the file was created n minutes ago. – don_crissti Mar 14 '18 at 21:51
  • @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 with File::Copy::Recursive. If the OP only wants to delete directories, that would make things much easier (so it's not such a minor detail). – haukex Mar 14 '18 at 21:58
5

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
  • Simple, if you have a find that supports all those fancy GNU options. – Kusalananda Mar 14 '18 at 6:33
  • @Kusalananda, yes, that's for GNU implementation (specifically) – RomanPerekhrest Mar 14 '18 at 6:35
  • I don't have GNU support. – Anil Kumar Mar 14 '18 at 6:43
  • When I was using solaris, I wanted to use the Gnu tool, so I downloaded them. – ctrl-alt-delor Mar 14 '18 at 10:00
  • Also, the question says "older than 15 min of creation" (pre-edit "older than 30 min of creation"), not modified time (-mmin). – Sparhawk Mar 14 '18 at 11:44
0

I don't have access to HP-UX, but on Solaris or any BSD system (i.e. not using GNU find), the following would work:

find . -type f -cmin +15 -print -exec rm {} +

This would find all regular files in or below the current directory, whose last inode status change was more than 15 minutes ago. The pathnames of these files will be displayed and then the files would be deleted.

To be able to give interactive confirmation before deleting anything, use

find . -type f -cmin +15 -print -ok rm {} ';'

Most Unix file systems do not keep track of file creation time. The -cmin predicate of find will test against the ctime of the file, i.e. the "change time" of the inode. If the file's inode has not changed since it was created, then this would be its "creation time". The ctime timestamp gets updated on file creation and on modifications of ownership or permissions. Modification of the contents of the file also updates this timestamp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.