I have a directory containing several files. I need to know the full path of only the most recent file that contains a certain string.
This needs to work on macOS, and the directory path contains spaces.
I need to be able to invoke this bash command without having to cd
into that directory first.
Note: I've seen similar questions posted here, but they are only checking if the most recent file in a directory contains that string. This means it will fail if the second-most-recent file in the directory is the one that contains the string I'm looking for.
Part of the reason I'm struggling is that things break when directories have spaces in them, and figuring out how to quote paths is non-trivial with xargs
. Using find -exec
doesn't work because there is no find
flag to return results in last-modified order.
Clarifications: The file's contents need to contain the string, not the filename. I'm looking to use bash, as this is part of other bash scripts, and I'm also keen to understand how to do this in bash.