Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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..
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
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.
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.
Last edited by libCognition; 04-28-2012 at 02:58 AM.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.