sort oldest 5 files in a directory tree recursively based on timestamp Linux

Discussion in 'Shell Script' started by abhelp, Aug 28, 2010.

  1. abhelp

    abhelp New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have a directory listing with many subdirectories having many files.
    I want to recursively search for the oldest 5 files starting from the base directory and not 5 from each subdirectory.
    I am writing a shell script which sorts them using
    ls -lRtur|egrep "txt|jpg" > /tmp/file1
    Now from this /tmp/file1 file I want to sort the files same as what the ls -ltr command does that is oldest file time to newest file time first.
    How do I sort based on Linux time stamp?
    The files itself also have Linux timestamps embedded in them
    So I can sort based after extracting them as well if it is easier.
    My /tmp/file1 has entries like below.
    -rw-rw-r--. 1 usr1 usr1 705 2010-01-22 17:25 sample20100603173659.jpg

    I want to get the 5 oldest files and then delete them.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice