I am running a command that I use to find the most recent file in a folder then grep the contents for a matching pattern. Here is the command flow that shows the problem I am having.
root@backup /]# grep -w 'sucessfully completed.' "$(ls -1rt /var/log/folder | tail -n1)" /bin/grep: <file_name>: No such file or directory [root@backup /]# cd /var/log/folder/ [root@backup file_folder]# grep -w 'sucessfully completed.' "$(ls -1rt /var/log/folder | tail -n1)" sucessfully completed.
In the first command, it finds the file but then says no such file or directory. Weird. Then, as you can see I cd'd inside the actual folder ran the same command and it worked. My variable has the absolute path so what is going on here? I have checked for any grep aliases that could be interfering and found none. If I run the commands separately outside of using a variable it works and it works outside of the files folder. (as I would expect)
[root@backup /]# grep -w 'sucessfully completed' /var/log/folder/file sucessfully completed.