LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Find newest file (https://www.linuxquestions.org/questions/linux-software-2/find-newest-file-153425/)

capella 03-04-2004 09:10 AM

Find newest file
 
I have a directory where a new file gets created every 2 hours, I need a way to find the newest file in that directory regardless of the filename, can someone please help me out.

homey 03-04-2004 09:32 AM

something like this?
find /mnt/backup -type f -name '*' -cmin -120 {} \;

This example takes an action on the files it finds, deletes them in this case.
find /mnt/backup -type f -name '*.gz' -mtime +90 -exec rm {} \;

capella 03-04-2004 09:44 AM

Thanks
 
Thanks but I really need to find the newest file regardless of time created as there will be other backups moving in that directory, so the solution cannot be base on cmin etc..

itsjustme 03-04-2004 09:52 AM

How about doing an 'ls -lt' and piping that to a file and then parse the first line with a small script to get the filename.

capella 03-04-2004 09:58 AM

Yeah
 
I read somewhere that i can do that and combine it with tail-1, but i have no idea what that means im a database guy and was hoping someone could spoonfeed me the solution

libCognition 04-27-2012 04:44 PM

bash recursion
 
I just came up with a solution to this using recursion:
Code:

latest_file() {
  if [[ ! -z ${2} ]]
  then
    ls -l $2
    latest_file $1 $(find $1 -type f -newer "$2" | head -1)
  fi
}

Then run it with first argument being the folder to search, and the second argument being any arbitrary file in that folder. e.g. "latest_file /etc/ /etc/fstab | tail -n 1"

Limitations: probably doesn't work if a filename has spaces.

catkin 04-27-2012 10:25 PM

Code:

ls -1rt --group-directories-first | tail -1
(that's a number 1 in -1rt)

libCognition 04-28-2012 02:41 AM

Quote:

Originally Posted by catkin (Post 4665056)
Code:

ls -1rt --group-directories-first | tail -1
(that's a number 1 in -1rt)

That misses all the files that have a depth greater than zero.

This version of that approach would get some penetration:
Code:

ls -1rt --group-directories-first * */* */*/* | tail -1
but then you need to know how deep the structure goes. Or substitute with:
Code:

ls -1rt --group-directories-first $(find . -type d | sed 's/[^/]/*/g' | tr -s '*' | sort | uniq) | tail -1
What's interesting is the 'ls' version (above) got a different answer than 'find -newer' (from post 6), but both files were created the same minute. So one of the two approaches looks at the seconds.

catkin 04-28-2012 04:15 AM

Quote:

Originally Posted by libCognition (Post 4665139)
That misses all the files that have a depth greater than zero.

True but capella wrote in the OP "I have a directory" so its not a problem.

catkin 04-28-2012 04:26 AM

Quote:

Originally Posted by libCognition (Post 4665139)
What's interesting is the 'ls' version (above) got a different answer than 'find -newer' (from post 6), but both files were created the same minute. So one of the two approaches looks at the seconds.

That is interesting but experimentation showed that both find and ls are very fine grained regards time:
Code:

c@CW8:/tmp/tmp$  for i in $( seq 1 100 ); do touch $i; touch a$i; done
ls -lrt
[files listed in created order]
c@CW8:/tmp/tmp$ find . -newer 99
./100
./a100
./a99



All times are GMT -5. The time now is 06:23 PM.