finding newest file in a directory and removing the rest

Discussion in 'Perl Misc' started by Stu, Oct 10, 2008.

  1. Stu

    Stu Guest

    I was wondering if somebody can point me in the right direction?

    I have a requirement to remove all the files in a given directory
    except for the one that has been
    created last. In ksh, to find this file I would not remove I would do
    a ls -ltr | tail -1. Can somebody provide me with a function that can
    do something similiar but with perl code.

    Thanks in advance to all who answer this post
     
    Stu, Oct 10, 2008
    #1
    1. Advertising

  2. Stu <> wrote:
    >I have a requirement to remove all the files in a given directory
    >except for the one that has been
    >created last. In ksh, to find this file I would not remove I would do
    >a ls -ltr | tail -1. Can somebody provide me with a function that can
    >do something similiar but with perl code.


    perldoc -f opendir
    perldoc -f readdir (or instead perldoc -f glob)
    perldoc -f -M (for a finer granularity see perldoc -f stat); Please be
    advised that this provides the inode change time which may or may not
    coincide with the file creating time. However this is the best guess on
    many (most?) file systems because they don't track file creation time.
    Besides, "creation time" is ambigious at best. If you copy a photo from
    the memory card to you computer, at what moment was that file created:
    when you hit the shutter or when you copied the file?
    perldoc -f sort
    perldoc -f unlink

    jue
     
    Jürgen Exner, Oct 10, 2008
    #2
    1. Advertising

  3. Stu <> wrote:
    > I was wondering if somebody can point me in the right direction?
    >
    > I have a requirement to remove all the files in a given directory
    > except for the one that has been
    > created last.



    That is only possible on file systems that keep track of
    a file's creation time.

    Most *nix filesystems do not record creation time at all.


    If you can live with modification time instead of creation time,
    then this should do it:

    my(undef, @f) = sort { -M $a <=> -M $b } grep -f, glob '*';
    unlink @f;


    (but that does not remove all old files, old files that start
    with a dot will be left alone.
    )

    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Oct 10, 2008
    #3
  4. Stu

    Ben Morrow Guest

    Quoth Stu <>:
    > I was wondering if somebody can point me in the right direction?
    >
    > I have a requirement to remove all the files in a given directory
    > except for the one that has been
    > created last. In ksh, to find this file I would not remove I would do
    > a ls -ltr | tail -1. Can somebody provide me with a function that can
    > do something similiar but with perl code.


    perldoc -f readdir
    perldoc -f stat
    Maybe perldoc -f -X
    perldoc -f sort
    perldoc File::stat

    Ben

    --
    Many users now operate their own computers day in and day out on various
    applications without ever writing a program. Indeed, many of these users
    cannot write new programs for their machines...
    -- F.P. Brooks, 'No Silver Bullet', 1987 []
     
    Ben Morrow, Oct 10, 2008
    #4
  5. Stu

    Stu Guest

    On Oct 10, 12:14 pm, Tad J McClellan <> wrote:
    > Stu <> wrote:
    > > I was wondering if somebody can point me in the right direction?

    >
    > > I have a requirement to remove all the files in a given directory
    > > except for the one that has been
    > > created last.

    >
    > That is only possible on file systems that keep track of
    > a file's creation time.
    >
    > Most *nix filesystems do not record creation time at all.
    >
    > If you can live with modification time instead of creation time,
    > then this should do it:
    >
    >     my(undef, @f) = sort { -M $a <=> -M $b } grep -f, glob '*';
    >     unlink @f;
    >
    > (but that does not remove all old files, old files that start
    >  with a dot will be left alone.
    > )
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


    Thanks for the response but I am not too sure I understand what this
    statement is doing.

    When I do this:

    @f = sort { -M $a <=> -M $b } grep -f, glob </axsma/pbh/var/proc/pbh40/
    *>;
    foreach $file (@f)
    {
    print $file . "\n";
    }

    I get I only get one file as the output yet there are several files in
    the above-mentioned directory. I would have thought I would have
    gotten back all the files in the directory in sorted order by modtime
    or creation time.

    The file I did get back appears to have a time associated with it that
    is in the middle of all my files

    Oct 1 15:08 newest file
    Sep 19 10:40 time associated with the file that was returned
    Jul 18 11:21 older file

    How come the older or the newer file was not returned?

    Once again thanks for the help
     
    Stu, Oct 13, 2008
    #5
  6. Stu <> wrote:
    > On Oct 10, 12:14 pm, Tad J McClellan <> wrote:
    >> Stu <> wrote:
    >> > I was wondering if somebody can point me in the right direction?

    >>
    >> > I have a requirement to remove all the files in a given directory
    >> > except for the one that has been
    >> > created last.

    >>
    >> That is only possible on file systems that keep track of
    >> a file's creation time.
    >>
    >> Most *nix filesystems do not record creation time at all.
    >>
    >> If you can live with modification time instead of creation time,
    >> then this should do it:
    >>
    >>     my(undef, @f) = sort { -M $a <=> -M $b } grep -f, glob '*';
    >>     unlink @f;
    >>
    >> (but that does not remove all old files, old files that start
    >>  with a dot will be left alone.
    >> )
    >>
    >> --
    >> Tad McClellan
    >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"



    It is bad form to quote .sigs. Please do not do that.

    You should also trim irrelevant text, and interleave your comments.

    If you are going to comment on one line of code, then you should
    quote only the one line of code that you are going to comment on.


    > Thanks for the response but I am not too sure I understand what this
    > statement is doing.
    >
    > When I do this:
    >
    > @f = sort { -M $a <=> -M $b } grep -f, glob </axsma/pbh/var/proc/pbh40/
    > *>;



    You are calling glob() *twice*.

    Once with its name, and once with its alternate form.

    Call it only once instead:

    @f = sort { -M $a <=> -M $b } grep -f, </axsma/pbh/var/proc/pbh40/*>;
    or, better
    @f = sort { -M $a <=> -M $b } grep -f, glob '/axsma/pbh/var/proc/pbh40/*';


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Oct 13, 2008
    #6
  7. Stu <> wrote:
    >@f = sort { -M $a <=> -M $b } grep -f, glob </axsma/pbh/var/proc/pbh40/
    >*>;
    >
    >I get I only get one file as the output yet there are several files in
    >the above-mentioned directory. I would have thought I would have


    This is a neat one :))

    You are double globbing. Either drop the glob() call or replace the
    angle brackets with paranthesis.

    jue
     
    Jürgen Exner, Oct 13, 2008
    #7
  8. Stu

    Stu Guest

    On Oct 13, 12:14 pm, Tad J McClellan <> wrote:
    > Stu <> wrote:
    > > On Oct 10, 12:14 pm, Tad J McClellan <> wrote:
    > >> Stu <> wrote:
    > >> > I was wondering if somebody can point me in the right direction?

    >
    > >> > I have a requirement to remove all the files in a given directory
    > >> > except for the one that has been
    > >> > created last.

    >
    > >> That is only possible on file systems that keep track of
    > >> a file's creation time.

    >
    > >> Most *nix filesystems do not record creation time at all.

    >
    > >> If you can live with modification time instead of creation time,
    > >> then this should do it:

    >
    > >>     my(undef, @f) = sort { -M $a <=> -M $b } grep -f, glob '*';
    > >>     unlink @f;

    >
    > >> (but that does not remove all old files, old files that start
    > >>  with a dot will be left alone.
    > >> )

    >
    > >> --
    > >> Tad McClellan
    > >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >
    > It is bad form to quote .sigs. Please do not do that.
    >
    > You should also trim irrelevant text, and interleave your comments.
    >
    > If you are going to comment on one line of code, then you should
    > quote only the one line of code that you are going to comment on.
    >
    > > Thanks for the response but I am not too sure I understand what this
    > > statement is doing.

    >
    > > When I do this:

    >
    > > @f = sort { -M $a <=> -M $b } grep -f, glob </axsma/pbh/var/proc/pbh40/
    > > *>;

    >
    > You are calling glob() *twice*.
    >
    > Once with its name, and once with its alternate form.
    >
    > Call it only once instead:
    >
    >    @f = sort { -M $a <=> -M $b } grep -f, </axsma/pbh/var/proc/pbh40/*>;
    > or, better
    >    @f = sort { -M $a <=> -M $b } grep -f, glob '/axsma/pbh/var/proc/pbh40/*';
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
    >
    > - Show quoted text -


    Thanks for all your help. That was it
     
    Stu, Oct 13, 2008
    #8
  9. Stu

    Dr.Ruud Guest

    Stu schreef:

    >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >
    > Thanks for the response but I am not too sure I understand what this
    > statement is doing.


    It prints an address.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 13, 2008
    #9
  10. Stu <> wrote:
    > On Oct 13, 12:14 pm, Tad J McClellan <> wrote:
    >> Stu <> wrote:
    >> > On Oct 10, 12:14 pm, Tad J McClellan <> wrote:
    >> >> Stu <> wrote:



    [ snip 20 lines ]


    >> >> --
    >> >> Tad McClellan
    >> >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >>
    >> It is bad form to quote .sigs. Please do not do that.
    >>
    >> You should also trim irrelevant text, and interleave your comments.



    [ snip 20 lines ]


    >> --
    >> Tad McClellan
    >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Thanks for all your help.



    Yeah, right.

    Off to perpetual invisibility you go...


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Oct 13, 2008
    #10
  11. Dr.Ruud <> wrote:
    > Stu schreef:
    >
    >>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >>
    >> Thanks for the response but I am not too sure I understand what this
    >> statement is doing.

    >
    > It prints an address.



    <grin>


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Oct 13, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    How use newest userid in auto INSERT (2.0)

    VB Programmer, Jan 13, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,808
    VB Programmer
    Jan 13, 2006
  2. scad
    Replies:
    4
    Views:
    960
    James Kanze
    May 28, 2009
  3. Mmcolli00 Mom

    Show newest file in directory?

    Mmcolli00 Mom, Dec 18, 2008, in forum: Ruby
    Replies:
    6
    Views:
    125
    Robert Klemme
    Dec 19, 2008
  4. sc0ri0n

    Finding the newest directory

    sc0ri0n, May 13, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    108
    Joe Smith
    May 13, 2004
  5. jordilin
    Replies:
    17
    Views:
    194
    Peter J. Holzer
    Oct 29, 2007
Loading...

Share This Page