Find the latest folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the latest folder
# 1  
Old 10-01-2010
Find the latest folder

Hi,

I want to find out the files that are created the recent, how can I do it?

find . -type d

( i dont know what option to use to find the latest, it can be 1 day old or 10 days, but want to pick the latest one only)

Thanks,
# 2  
Old 10-01-2010
"find" isn't going to do what you want it to. It doesn't keep track of concepts like "newest". If you had a file that was, say, created by the last run of whatever you're tracing, you could use the --newer flags to find directories created after that point, but you would still get more than one, likely. You would get all the directories created after that point in time.

This is usually where I would write a small c program calling ftw, or write something in perl (or whatever your favorite scripting language is) to stat each directory and keep track of their timestamp (you would want mtime, likely) and then emit the most recent one.
# 3  
Old 10-01-2010
use perl, example below to find files from the last hour (not my code BTW) Smilie

Code:
function SearchLogsLastHour
#This function returns a list of log files modified
#in the last hour using perl
{
cd /path/to/files/you/want/to/find

files="$(
  perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  ' /path/to/files/you/want/to/find
)"
    if [[ $files == '' ]]; then
        echo "Error: No log files modified in the last hour exist - exiting."
    fi
}

function OutputTemp


Last edited by rich@ardz; 10-01-2010 at 12:23 PM..
# 4  
Old 10-01-2010
Please state what Operating System and Shell you have.
Please post a representative example directory tree with files, highlighting which file (or files) or directory (or directories) you are looking for.

As other posters state there is no "find" command to find the most recent file or directory as such. There are techniques to do this but let's first find out what you want and what software you have.
# 5  
Old 10-01-2010
I am using Linux 2.6.18-128.4.1.el5 #1 SMP GNU/Linux

and bash shell.

/data/output/ has all the folders like

200901, 200902, 200903, 200904 ( which are logically created based on a job)

Thanks,

Last edited by rudoraj; 10-01-2010 at 03:55 PM..
# 6  
Old 10-02-2010
Code:
find /data/output -type d -name "20*" |sort -nr |head -1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Retrieve the Latest file in a folder using SFTP

HI Guys, Can anyone help me to retrieve the latest file from below example using SFTP I have below list of files in a folder v403t.lstprgms.sortin1 val027.d099.fwest.oct2711 xcelrptd.d1400sqp.dec1713.t040459.@02del xcelrptd.d1400sqp.dec1713.t073308.@02del... (3 Replies)
Discussion started by: heye18
3 Replies

2. Shell Programming and Scripting

Find latest date in folder

HI I have folder in home dir. /home/kpp/07222013 /home/kpp/07212013 /home/kpp/07202013 Output :-- /home/kpp/07222013 Just find latest date (5 Replies)
Discussion started by: pareshkp
5 Replies

3. Shell Programming and Scripting

Delete all except latest datetime entery folder

Hello Expert, I have a strange requirement, I have folder where weekly backup are taken... week1_bkup_02102011 week2_bkup_09102011 week3_bkup_16102011 . . everyweek a backup is created I want to delete all folder except latest one for example in above a week4_bkup_23102011 is added... (3 Replies)
Discussion started by: aks_1902
3 Replies

4. UNIX and Linux Applications

Need to copy the latest file from Unix server to Shared folder

Hi All, One job in unix server will generate .csv files daily. I need to copy the latest of these .csv file from the unix server to the shared drive/folder in windows through unix script. My shared folder will look something like W:\some folder(for example). Could any one of you please help... (3 Replies)
Discussion started by: jaya@123
3 Replies

5. UNIX for Dummies Questions & Answers

Copy the latest (last file) in given folder

#!/bin/bash for i in {1..1536..1} do #find /home/test/Desktop/up111/workplace/Malware/$i/logs for a in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do #max=a for b in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do ... (4 Replies)
Discussion started by: upvan111
4 Replies

6. Shell Programming and Scripting

Find folder within folder, then find other folder in same dir

Hi all I'm new to your forum but not new to shells. I'm having a little trouble though as it's been quite some time since I scripted. Here's what I'm trying to do: I'm trying to search a directory named '/var/root/Applications' for another directory 'fooBar'. The "Applications" directory... (9 Replies)
Discussion started by: DC Slick
9 Replies

7. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

8. Shell Programming and Scripting

Copying latest file into a folder

Hello all, this is my first post while i am trying to understand unix. I would basically like to know if i can do this: Lets say i have a folderA and folderB And i save something in folderA Can i make a script that checks folderA latest file, then compares it with the date of latest file in... (16 Replies)
Discussion started by: takissd
16 Replies

9. UNIX for Dummies Questions & Answers

Copy the latest file from a folder

Hi, I have a problem. I have some text files in a folder. The names can be like: emp_20080307053015.dat emp_20080306053015.dat emp_20080305053015.dat emp_20080304053015.dat The date format appended is like yyyymmdd and timestamp. What i need is i have to copy the latest file every... (3 Replies)
Discussion started by: Aswarth
3 Replies

10. Shell Programming and Scripting

Take a folder name and find it in another folder (Complicated)

Hi Script Experts, Here is my scenario: 1. /var/mqm/qmgrs folder will contain 11 folders as follows: 1. /var/mqm/qmgrs/Folder_Name1 ....................../Folder_Name2 ....................../Folder_Name3 ....... ...................../Folder_Name11 2. if Folder_Name1 exists... (5 Replies)
Discussion started by: hkhan12
5 Replies
Login or Register to Ask a Question