Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix command to find most recent directory created

I want to copy the files from the most recent directory created. How would I do so in unix?

For example, if I have the directories names as date stamp as such:

/20110311
/20110318
/20110325
like image 636
jdamae Avatar asked Mar 31 '11 18:03

jdamae


People also ask

How do I find the most recently created file in Linux?

Finding Files Modified on a Specific Date in Linux: You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.

How do I find the last modified directory in Linux?

-mtime n is an expression that finds the files and directories that have been modified exactly n days ago. In addition, the expression can be used in two other ways: -mtime +n = finds the files and directories modified more than n days ago. -mtime -n = finds the files and directories modified less than n days ago.


1 Answers

This is the answer to the question I think you are asking.

When I deal with many directories that have date/time stamps in the name, I always take the approach that you have which is YYYYMMDD - the great thing about that is that the date order is then also the alphabetical order. In most shells (certainly in bash and I am 90% sure of the others), the '*' expansion is done alphabetically, and by default 'ls' return alphabetical order. Hence

    ls | head -1
    ls | tail -1

Give you the earliest and the latest dates in the directory.

This can be extended to only keep the last 5 entries etc.

like image 79
Beano Avatar answered Oct 23 '22 11:10

Beano