I'm looking for a good portable way of finding the oldest file in a directory tree matching a pattern, explicitly without using find's -printf option.
So far I found this solution: https://superuser.com/a/552609/1182477 which I extend with a pattern like this:
find . -type f -name "*.csv" -print0 | xargs -0 ls -ltr | head -n 1
But this fails silently when no file matches the pattern. I get the oldest file regardless of the pattern.
My diagnosis so far: when nothing matches the pattern, nothing gets passend into xargs and thus ls -ltr lists all files.
How can I solve this? I want to get nothing out, when nothing matches the pattern.
And maybe, as a bonus, a solution that also avoids the ARG_MAX problem with long file lists? (And before someone asks: logrotate is not available on the system.)
One thing that I stumpled upon in my diagnostics and what could help me find a solution to the above:
ls *.csv
lists files matching that pattern. But
*.csv | xargs ls
lists all files. Shouldn't both of them do the same?
Thank You very much.