I want to search for existence of a file using child_process
in node:
const { exec } = require('child_process');
exec('ls | grep "filename"', (err, result) => {...})
When the filename exists, exec result is fine. But when the filename doesn't exist, I get an error:
Command failed: ls | grep "filename"
In this case, how can I tell if it's an error executing the command, or just because no result is found?
EDIT
Thanks for the advice on not searching for a file this way. The above code is not the actual code, but just a demo piece illustrating my problem with grep
. In my actual case I'm searching for keywords in the output by task spooler, thus I used exec
and tsp -l | grep ...