Hello All,
I am trying to capture the exit status of find command and want to delete the files only when it is successful. But it is always returning me as success even if the pattern of that file doesn't exist in the current directory. please help, checked manual page but couldn't able to figure out.
#!/bin/bash -x
logit()
{
echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
}
LOG_FILE=/home/infrmtca/bin/findtest.log
if find . -type f -name "*xyz*"
then
logit "Success"
find . -type f -name "*xyz*" -delete
else
logit "Failure"
fi
Running find twice assuming that the hierarchy hasn't changed is a bad idea.
It would be a much better solution to delete the files when first encountered, while also printing them to a logfile. Afterwards, the emptiness of the logfile can be tested to determine if any files and were removed.
Alternatively, you can run find and collect matching filenames in a file. Test that file for emptiness. If not empty, rm each file listed.