Find the answer to your Linux question:
Results 1 to 4 of 4
Hi I want to find the file with *.core extension in a directory. If the Number of such files are > 50 then i want to delete all thos *.core ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    22

    delete oldest file

    Hi

    I want to find the file with *.core extension in a directory.
    If the Number of such files are > 50 then i want to delete all thos *.core files whose modification time is oldest.

    I don't want to use ls -t command because if the number of file are large say 3000 the it possible that this command gets hang.

    I prefably want to use find


    Thanks

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    Not sure if you can use find to find the oldest file...

    You could still use ls with using the '*' which will expand and may cause you problems as you've mentioned.

    As a work around you could try something like

    ls -t | grep "\.core$" | tail -n1

    which would give you the file name of the oldest .core file.
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    I'm sure there's probably a more eloquent way of doing this but this works.

    What it does is:

    List the date of each file found in the format yyyymmddhhmmss
    Sort date in decreasing order
    Extract the filename starting with the 51st file
    Pass it to xargs to execute the rm command

    Use at your own risk.

    Code:
    find . -maxdepth 1 -type f -name "*.core" -exec ls -l --time-style=+%Y%m%d%H%M%S {} \; | \
    sort -k6,6nr | awk 'NR>50 {print $7}' | xargs rm -fv
    Before using it with 'xargs rm -fv' use 'xargs ls -l' to make sure it's selecting the correct files to delete.

  4. #4
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97
    find is the right way to do...

    use "find" command..

Posting Permissions

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