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.
|
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 {} \; |
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..
|
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.
|
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
|
bash recursion
I just came up with a solution to this using recursion:
Code:
latest_file() { Limitations: probably doesn't work if a filename has spaces. |
Code:
ls -1rt --group-directories-first | tail -1 |
Quote:
This version of that approach would get some penetration: Code:
ls -1rt --group-directories-first * */* */*/* | tail -1 Code:
ls -1rt --group-directories-first $(find . -type d | sed 's/[^/]/*/g' | tr -s '*' | sort | uniq) | tail -1 |
Quote:
|
Quote:
Code:
c@CW8:/tmp/tmp$ for i in $( seq 1 100 ); do touch $i; touch a$i; done |
All times are GMT -5. The time now is 06:23 PM. |