Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-03-2010, 11:43 PM   #1
LQ Newbie
Registered: Jun 2010
Posts: 11

Rep: Reputation: 0
sort oldest 5 files in a directory tree recursively based on timestamp Linux

Hi all

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.

Old 06-04-2010, 04:58 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi and welcome to LinuxQuestions!

You can try the find command in conjunction with xargs, e.g. from the top-level directory:
find . -type f \( -name \*.txt -o -name \*.jpg \) -print0 | xargs -0 ls -lt | tail -5
In this way the entire list of files is passed as argument to the ls -lt command (through xargs). The only caveat is if the list is too long and it goes beyond the limit for the length of command arguments.

Indeed, if you already have a list of the files and all of them contain the date/time in their name (and in the format you've shown) you can sort them numerically. Here is a complicate way (just one of the many possibilities):
while read index
  sed -n "${index}p" /tmp/file1
done < <(awk --re-interval '$NF ~ /[0-9]{14}/{print NR, gensub(/.*([0-9]{14}).*/,"\\1","g",$NF)}' /tmp/file1 | sort -k2n | awk 'NR <= 5{print $1}')
The three piped commands in the process substitution that feeds the while loop do the following:
1) print out a numbered list of timestamps extracted from the file names
2) sort them numerically based on the timestamp
3) print out the indexes (line numbers in the original file) of the first 5 occurences
The sed command inside the loop prints out the relevant lines extracted from the original file.

Easier if you want to sort based on the actual timestamp:
sort -k6,7n /tmp/file1 | head -5
but this depends on the way the timestamp is displayed by the ls command (in your example is good but it might not be true for other systems). Anyway, we can always force GNU ls to format the timestamp as we need it, for example:
$ ls -l testfile
-rw-r--r-- 1 colucix users 545 Jun  4 11:56 testfile
$ ls -l --time-style=+"%Y-%m-%d %H:%M" testfile
-rw-r--r-- 1 colucix users 545 2010-06-04 11:56 testfile
Hope this helps.

Last edited by colucix; 06-04-2010 at 05:22 AM. Reason: (Hopefully) corrected syntax of English language


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recursively unrar a directory tree J_Humphrey Linux - General 4 11-16-2014 12:51 PM
will it be poosile to sort the output of ls command based on timestamp of files. shad.ithbti Linux - Newbie 1 09-15-2009 01:51 AM
find all files in current folder and all subfolders and sort all by timestamp dlnlinux Linux - Newbie 2 06-03-2008 11:29 AM
script to sort files according to timestamp aeby Linux - Newbie 1 05-16-2007 08:18 AM
Auto Delete oldest files @ratex when directory is 98% full jmanjohn61 Linux - Software 1 04-05-2005 03:44 PM

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration