The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding the oldest file in a particular directory pavan_movva Shell Programming and Scripting 4 04-08-2009 08:24 AM
how to find a file named vijay in a directory using find command amirthraj_12 UNIX for Dummies Questions & Answers 6 10-25-2008 01:37 PM
how to grep the oldest file in a directory ericaworld Shell Programming and Scripting 1 05-29-2007 02:24 PM
Removing the oldest file in a directory pavan_movva Shell Programming and Scripting 2 10-10-2006 12:38 PM
Oldest File In A Directory bergerj3 UNIX for Dummies Questions & Answers 2 02-27-2002 03:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-25-2006
rahulrathod rahulrathod is offline
Registered User
  
 

Join Date: Sep 2004
Location: Mumbai-India
Posts: 158
Find Oldest file in a directory tree

This might just be one command.

Any1 having the solution?

Thanks,
Rahul.
  #2 (permalink)  
Old 01-25-2006
HLee1981 HLee1981 is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 35
Quote:
Originally Posted by rahulrathod
This might just be one command.

Any1 having the solution?

Thanks,
Rahul.
I think

Code:
ls -gt

would be one way of doing it. This will give a shortened version of the long list with the oldest files being listed in the end of the list. This should list them by the timestamp in descending order.
  #3 (permalink)  
Old 01-25-2006
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
i guess g is ignored by ls, the below one will work anyway

ls -1t|tail -1

1 lists the output of ls as a column
t sorts it by time
tail -1 lists the last item in the list
  #4 (permalink)  
Old 01-27-2006
rahulrathod rahulrathod is offline
Registered User
  
 

Join Date: Sep 2004
Location: Mumbai-India
Posts: 158
This will only give me the oldest file in the current directory.

What about searching the oldest file in the subdirectories?

Can we do it with find??

Last edited by rahulrathod; 01-27-2006 at 05:56 AM..
  #5 (permalink)  
Old 01-27-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
Try and adapt this ksh script :


Code:
#!/usr/bin/ksh
#
# Usage: $0 [directory]
#
oldest_file=

ls -1R ${1-} | \
awk -v dir="${1-}" '
        BEGIN {
           if (dir !~ /\/$/) 
              dir = dir "/"
           oldest_file_in_dir = 1;
        }
        /:$/ {
           dir = substr($0,1,length-1) "/";
           oldest_file_in_dir = 1;
           next;
        }
        oldest_file_in_dir {
           print dir  $0;
           oldest_file_in_dir = 0;
           next;
        }
    ' | \
while read file
do
   if [ -z "$oldest_file" ]
   then
      oldest_file="$file"
   else
      [[ "$file" -nt "$oldest_file" ]] && continue
      oldest_file="$file"
   fi
done

echo "Oldest File: $oldest_file"

Jean-Pierre.
  #6 (permalink)  
Old 01-27-2006
rahulrathod rahulrathod is offline
Registered User
  
 

Join Date: Sep 2004
Location: Mumbai-India
Posts: 158
I am trying to do something simpler as follows:

$find . -name "*" -exec ls -lrt {} \; |sort -k8 | more

But this gives me files in the ascending order of years

2003
2003
..
..
2004
..
..
2005

Now , I want to sort these further by month, day and time.

Any inputs??
  #7 (permalink)  
Old 09-23-2007
AnswerGuy AnswerGuy is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 8
Lightbulb GNU find $DIR -printf "%T@ %p\n" | sort -n | head -1

I would use the following command (assuming I had GNU find available):
find $DIR -printf "%T@ %p\n" | sort -n | head -1
The %T is modification time in a custom format; @ is the format specifier for for "seconds since the epoch. The rest should be pretty self-evident. %C would be used if you cared about the "ctime" (the time at which the file's inode was changed in any way, including modifications to the file contents (as with mtime) but also changes to ownership, permissions, link count? etc.

A couple of heuristics for scripting in general:

Any time you're trying to find files with specific characteristics other than matching a simple glob pattern and any of the simple test features like -d (directory) or -r (readable), etc, you almost always want to use the find command. Most of the switches and arguments to find control what find will consider returning (to things like -print, -exec, or -ls).

Any time you're going to compare or manipulate timestamps under UNIX you probably want to use "seconds since the epoch" (this allows simple numeric operations such as sorting or arithmetic adjustment).

So it seems obvious to just traverse the tree printing all the timestamps in a usable form; sort numerically and discard all but the first line. (You could also sort -nr, reversed, and take the last line with tail -1; but then your tail process has to read the entire pipe. In this example the head command can exit and break the pipe to kill to output from the sort command; which is marginally more efficient. Of course all of the expense in this is in the sort command ... it will take most of the CPU time and memory. Meanwhile, of course the find command will expend I/O -- that's inherent in traversing a filesystem tree).

P.S. If you need to convert a "seconds since the epoch" timestamp into a form you can read you can use an expression like:
date --date "$(( $(date +%s) - $TIMESTAMP )) seconds ago"
.. and naturally you can also specify any format you like for that output as you would with any other date


JimD (former Linux Gazette AnswerGuy)
Closed Thread

Bookmarks

Tags
linux, mtime, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0