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,

"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.

use perl, example below to find files from the last hour (not my code BTW) :wink:

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

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.

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,

find /data/output -type d -name "20*" |sort -nr |head -1