LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 02-03-2012, 07:38 AM   #1
casperdaghost
Member
 
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

Code:
/data/startup/logs/casperinit01.11-17-11.txt
/data/startup/logs/casperinit01.02-07-11.txt
/data/startup/logs/casperinit01.04-14-11.txt
/data/startup/logs/casperinit01.02-15-11.txt
/data/startup/logs/casperinit01.09-27-11.txt
/data/startup/logs/casperinit01.12-03-10.txt
/data/startup/logs/casperinit01.01-30-12.txt
/data/startup/logs/casperinit01.07-18-11.txt
/data/startup/logs/casperinit01.05-20-11.txt
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, 07:52 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

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

Rep: Reputation: 0
Wink

Try this:
Code:
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, 07:53 PM   #4
casperdaghost
Member
 
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.

Code:
casper@casperbox ~ $ logstart_times_carcore 
#!/usr/bin/perl
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

/data/noc/startup/logs/:
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.

Code:
#!/usr/bin/perl
use strict;
use warnings;

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

casper@casperbox ~ $ logstart_times_carcore 
/data/noc/startup/logs/casperbox.01-31-12.txt
/data/noc/startup/logs/casperbox.02-01-12.txt
/data/noc/startup/logs/casperbox.02-02-12.txt
/data/noc/startup/logs/casperbox.02-03-12.txt
/data/noc/startup/logs/casperbox.02-06-12.txt

/data/noc/startup/logs/:
total 55248
casperbox.11-13-10.txt
casperbox.11-15-10.txt
xargs: ls: terminated by signal 13
casper@casperbox ~ $

Last edited by casperdaghost; 02-06-2012 at 07:59 PM.
 
Old 02-06-2012, 08:53 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.

Either

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

2. If you are using Perl anyway, then don't shell out, use the Perl stat cmd http://perldoc.perl.org/functions/stat.html
 
  


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


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