LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 01-20-2011, 03:17 PM   #1
maunir
LQ Newbie
 
Registered: Dec 2005
Posts: 2

Rep: Reputation: 0
Help needed to sort files based on timestamp in bash scripting.


I have already written a script in bash to find any new files that were modified within the last 10 minutes. The script is scheduled to run every 10 minutes. So after the find it runs against the database. This works great if the files are not dependent on each other. So our users have to wait for the next 10 minutes to drop a file if there is any dependency. So what they want is some kind of function that will look at time and will execute based on time-stamp for the file. For ex. if File A is dropped in this directory at 10:31 and then File B is dropped in the same location at 10:32 then they want file 10:31 to be executed first. I can't get either the sort functionality to work nor the stat command. There are no sub-directories but just files and a simple "ls -ltr" gives me everything from the parent directory. Any help would be greatly appreciated. Excerpt from my script.

while read sqllist;do
listoffiles=$(/usr/local/bin/find $sqllist -mmin -10)
for FILE in $listoffiles
do
"all the function against Database"
done
done < /usr/local/sbin/listsql.txt

some of the command I have tried so far and the sorting is not in correct order.
#sorting=`ls -1tr $FILE`
#sorting=`/usr/local/bin/stat --format=%Y --format=%n $FILE`

This is how my listing of file looks like
-rw-r--r-- 1 root root 0 Jan 20 11:40 test4.txt
 
Old 01-20-2011, 03:54 PM   #2
gb2312
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Rep: Reputation: 6
You can try the find command which can

* -mmin -10
select file modified within last 10min
* -printf '%T@ %p\n'
print timestamp (from epoch) and path

Like:

$ find /tmp -maxdepth 1 -type f -mmin -10 -printf '%p\n'p
1295556351.0000000000 /tmp/c
1295556345.0000000000 /tmp/a
1295556348.0000000000 /tmp/b

Then you can sort as number:

$ find /tmp -maxdepth 1 -type f -mmin -10 -printf '%T@ %p\n' | sort -n
1295556345.0000000000 /tmp/a
1295556348.0000000000 /tmp/b
1295556351.0000000000 /tmp/c
 
Old 01-20-2011, 04:35 PM   #3
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
It seems like the problem with your sort is that you are doing it on a single file not the list of files.

So instead of

#sorting=`ls -1tr $FILE`

try the following so the list of files in a directory are sorted together

listoffiles=$(ls -1tr `/usr/local/bin/find $sqllist -mmin -10`)


though gb2312's solution may be more efficient.
 
Old 01-24-2011, 02:46 PM   #4
maunir
LQ Newbie
 
Registered: Dec 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks gb. That worked perfectly.
 
  


Reply


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] Bash Script; Sort files into directory based on data in the file name MTAS Programming 31 10-06-2010 12:47 PM
sort oldest 5 files in a directory tree recursively based on timestamp Linux abhelp Programming 1 06-04-2010 05:58 AM
will it be poosile to sort the output of ls command based on timestamp of files. shad.ithbti Linux - Newbie 1 09-15-2009 02:51 AM
script to sort files according to timestamp aeby Linux - Newbie 1 05-16-2007 09:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 PM.

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