in reply to Unix - Keep the last modified file in the directory and move/copy the rest of the files.

use File::Copy; my $src = '/path/to/source/directory'; my $tgt = '/path/to/target/directory'; opendir SRC, $src or die $!; my @files = sort { -M $a <=> -M $b } readdir SRC; shift @files; move("$src/$_", "$tgt/$_") for @files;

Update: fixed as noted below.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

  • Comment on Re: Unix - Keep the last modified file in the directory and move/copy the rest of the files.
  • Download Code

Replies are listed 'Best First'.
Re: Re: Unix - Keep the last modified file in the directory and move/copy the rest of the files.
by sauoq (Abbot) on Jun 06, 2003 at 09:56 UTC

    I think you'd want to shift @files; rather than pop because -M returns the age of the file in days, not the actual mtime.

    Also, it looks like a last minute name change bit you. That should be readdir SRC; rather than DIR.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re^2: Unix - Keep the last modified file in the directory and move/copy the rest of the files. (common readdir trap)
by Aristotle (Chancellor) on Jun 07, 2003 at 00:29 UTC
    my @files = sort { -M $a <=> -M $b } readdir SRC;
    readdir doesn't prepend the SRC directory to the filename for you, so if your cwd is not the source directory, you will get strange results from that code.

    Makeshifts last the longest.