Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-03-2012, 06:38 AM   #1
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
sorting find by date

I need a whole path in a search

ls is not working

so I have been using find

find /data/startup/logs/ -print | tail

find is great becasue it gives me the whole path - however i am accustomed to ls -ltr | tail, which give me files from the last ten days. the find command does not have any order to it - the output is random.
how do I sort the output of find, so i can tail it and get the ;ast ten dates.
Old 02-03-2012, 06:52 AM   #2
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Would this work ?
find /data/startup/logs -mtime -10 | xargs ls -lt
Old 02-03-2012, 07:20 AM   #3
LQ Newbie
Registered: Aug 2011
Distribution: Debian, Gentoo
Posts: 24

Rep: Reputation: 0

Try this:
ls -lrtd /data/startup/logs/*
-r for reverse order (oldest first)
-t to sort by modification time
-d to list directories
Seems to work!
Old 02-06-2012, 06:53 PM   #4
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
The ls -ltrd works - that is a really nice hack on the ls commands.
I searched the interwebs long and hard and did not see anything like this. my regards eehmke.

The find works too - i know that I want to find out more about the find commands.

casper@casperbox ~ $ logstart_times_carcore 
use strict;
use warnings;

if (! defined($ARGV[0])) { 
exec("find /data/noc/startup/logs/  -mtime -7  | xargs ls -ltr |head" );
-rw-r--r-- 1 root root 89292 Jan 31 07:05 /data/noc/startup/logs/casperbox.01-31-12.txt
-rw-r--r-- 1 root root 86122 Feb  1 07:05 /data/noc/startup/logs/casperbox.02-01-12.txt
-rw-r--r-- 1 root root 92153 Feb  2 07:05 /data/noc/startup/logs/casperbox.02-02-12.txt
-rw-r--r-- 1 root root 85764 Feb  3 07:05 /data/noc/startup/logs/casperbox.02-03-12.txt
-rw-r--r-- 1 root root 84994 Feb  6 07:05 /data/noc/startup/logs/casperbox.02-06-12.txt

total 55248
-rw-r--r-- 1 root root    94313 Jan  3  2011 casperbox.01-03-11.txt
-rw-r--r-- 1 root root    90218 Jan  3 07:05 casperbox.01-03-12.txt
xargs: ls: terminated by signal 13
I don't need the dates, so i got rid of the -ltr - I still get the
remainder at the end, and dont really know why I get the the two files
from November at the end. I don't need them. and from a pure learning
standpoint, do not know where they come from.

use strict;
use warnings;

if (! defined($ARGV[0])) { 
exec("find /data/noc/startup/logs/  -mtime -7  | xargs ls  |head" ); 

casper@casperbox ~ $ logstart_times_carcore 

total 55248
xargs: ls: terminated by signal 13
casper@casperbox ~ $

Last edited by casperdaghost; 02-06-2012 at 06:59 PM.
Old 02-06-2012, 07:53 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
The problem you've got with the find (& this also happens if you use -exec instead of |xargs..) is that the ls cmd is also being applied to the actual dir itself...
Try it without the "| xargs" to see the difference.


1. shell soln
find /data/startup/* logs -mtime -10 | xargs ls -lt ...

2. If you are using Perl anyway, then don't shell out, use the Perl stat cmd


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] sorting of files and creating a loop to process them date wise umix Linux - Newbie 8 12-03-2011 04:12 AM
sorting date secondchanti Linux - Newbie 3 08-18-2010 08:55 PM
Sorting photos into folders based on exif date SuperJediWombat! Programming 7 05-16-2009 06:53 PM
Sorting all files of home folder modified after a specific date for backup fc6_user Linux - General 2 03-30-2007 02:24 AM
Sorting files by time / date. TheRealDeal Linux - General 2 08-06-2006 09:35 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:12 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