1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

    Aug 28, 2010
    Likes Received:
    Trophy Points:
    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