I have a need to search a certain folder for the newest created file and grep the contents for a pattern. I have had ok success so far using this command:
find /var/log/folder -type f -printf '%T+ %p\n' | sort | tail -n1 | xargs grep 'string to find'
This does find the latest file that has the match but I am getting an unexpected line in stdout.
$ find /var/log/folder -type f -printf '%T+ %p\n' | sort | tail -n1 | xargs grep 'string to find'
grep: 2016-05-12+20:01:59.0570667340: No such file or directory
/var/log/folder/file:string to find
How can I change my command so it does not give me the "no such file or directory" line?
ls dir
andls dir/
do different things? Not for me. – G-Man Says 'Reinstate Monica' May 13 '16 at 03:20rsync
and notls
. The problem was that I had-a
in my answer which was including.
and..
, which might end up being the most recently changed thing in a directory. – DopeGhoti May 13 '16 at 03:31ls
's-p
option (which appends a '/' to directory names) and piping intogrep -v '/$'
before piping intotail -n 1
. This works with at least GNU and FreeBSD's versions ofls
. – cas May 13 '16 at 15:03grep
and notfind
orxargs
. – Jesse Nickles Feb 16 '21 at 01:49