Find Oldest file in a directory tree


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find Oldest file in a directory tree
# 1  
Old 01-25-2006
Find Oldest file in a directory tree

This might just be one command.

Any1 having the solution?

Thanks,
Rahul.
# 2  
Old 01-25-2006
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  
Old 01-25-2006
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
This User Gave Thanks to linuxpenguin For This Post:
# 4  
Old 01-27-2006
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  
Old 01-27-2006
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  
Old 01-27-2006
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  
Old 01-27-2006
deleted... sorry
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Unable to find files, those can be present anywhere in the directory tree,based on its creation date

Hi I am unable to find files, those are present anywhere in the same directory tree, based on the creation date. I need to find the files with their path, as I need to create them in another location and move them. I need some help with a script that may do the job. Please help (2 Replies)
Discussion started by: sam192837465
2 Replies

2. UNIX for Dummies Questions & Answers

How to find directory listing from root to all files in tree format with details of perm/own/grp?

Hi, My apologies if my query is already available on this forum but I am new and could not find. I need a script to list all directories/sub directories and files with permissions/groups/owners. The script would run from home directory and should capture every directory. How do I do this? ... (4 Replies)
Discussion started by: 8709711
4 Replies

3. Shell Programming and Scripting

How to find files in directory tree by date

I'm using a directory naming convention to organize files as exemplified here: 2012/Aug/week-20-Aug/23-Thu/tuv.txt 2012/Aug/week-27-Aug/30-Thu/abc.txt 2012/Sep/week-27-Aug/01-Sat/def.txt 2012/Sep/week-03-Sep/07-Fri/xyz.txt How do I write a command that will list the file names abc.txt and... (4 Replies)
Discussion started by: siegfried
4 Replies

4. Shell Programming and Scripting

Deleting the oldest file in a directory

Hey! I have found similar posts both here and on other sites regarding this, but I cannot seem to get my script to work. I want to delete the oldest file in a test directory if there are more than two files. My script is currently: #!/bin/bash MEPATH=/usr/local/bin/test FILECOUNT=`ls... (4 Replies)
Discussion started by: Immolation
4 Replies

5. UNIX and Linux Applications

Finding the oldest file in a directory without ls

I am trying to determine the oldest and most recent files in a huge directory. I am using an ls -tr statement outside my find statement. The directory is too big and I am getting an "arg list too long" error. Is there something I can put in my find statement that doesn't create a list to... (2 Replies)
Discussion started by: hiyofjord
2 Replies

6. Shell Programming and Scripting

Finding the oldest file in a particular directory

Hi all, I am a newbie to scripting and I need your help regarding finding the oldest file in a particular directory. My intention is to remove that oldest file. Are there any options available with the "find" command to do this.. Thanks in advance for your help Pavan (4 Replies)
Discussion started by: pavan_movva
4 Replies

7. Shell Programming and Scripting

Setting Variable to oldest file in a directory

I am using a bash script to perform some automated maintenance on files in a directory. When I run the script using $sh -x script.sh <directory> the script works fine. It sets the variable to the oldest file, and continues on. However when I run the script like this $./script.sh <directory>, it... (5 Replies)
Discussion started by: spaceherpe61
5 Replies

8. Shell Programming and Scripting

how to grep the oldest file in a directory

Hi, Please help me out I want to grep the oldest file in a directory, could I use "ls" command? and how? thanx in advance (1 Reply)
Discussion started by: ericaworld
1 Replies

9. Shell Programming and Scripting

Removing the oldest file in a directory

Hi all, I need your assistance in removing the oldest file in a directory. I posted the same thread 3 days back and I got the following answer ls -1 -t | tail -1 | xargs rm which is not covering the case when there are directories older than the oldest file. So, could you please... (2 Replies)
Discussion started by: pavan_movva
2 Replies

10. UNIX for Dummies Questions & Answers

Oldest File In A Directory

I'm writing a script to find the oldest file in a directory. I know this can be done by using ls -rt | tail -1 but these are rather large directories and that can be somewhat slow since the script will be running constantly. Are there any other ways to do this that would be faster? I looked to... (2 Replies)
Discussion started by: bergerj3
2 Replies
Login or Register to Ask a Question