Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004

    Unanswered: Delete all except latest 3 files in a folder

    Hi ,
    I'm a guy new to Unix.
    I need help to delete files in a folder on a certain criteria :-

    The problem is that i donna know the names of the files.
    they are generated auto. by a diff program.
    the target is just to retain the latest 3 files.
    like its a sought of daily proces -- so if a script can be written for that.

    like if i have files in my folder (in acsending order of timestamp -- this is imp).
    a1 : created 10:45
    a2 : created 10:46
    a3 : created 10:47
    a4 : created 10:48
    a5 : created 10:49
    -- i just need to retain the last 3 files (a3 , a4 , a5)
    i.e. -- only the latest 3 files and delete rest all the files.
    it should not do anything if there are 3 or less than 3 files in the folder.

    Can anyone please help me out ..

  2. #2
    Join Date
    Jun 2004
    Hi aashish,

    Try this command .

    First move to the particular directory.

    rm [I]-f[/I] `ls -tr |tr -s " " |cut -d " " -f1- |tail -3`


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts