Skip to content

Delete PHP files with errors in bash

I needed to go through and delete a lot of automatically generated PHP files that contained errors (pretty much all because they got cut off before the generator finished)  So I poked around and put together a code that will go though a designated directory and delete PHP files. I also added on to have it limited to a certain number, so I could monitor it.

1
2
3
4
5
6
7
find ... -exec bash -c '
    if php -l "$1" >&/dev/null; then
        echo "$1: pass" >&/dev/null;
    else
        sudo rm $1
        echo "$1: fail"
    fi' -- {} \; | tail -n +1 | head -50

And you can double check there are no files with errors using this command:

1
find ... -iname "*.php" -exec php -l {} \; | grep -i "Errors.parsing"

Sources:
http://stackoverflow.com/questions/28413735/how-do-i-limit-the-results-of-the-command-find-in-bash
http://superuser.com/questions/341601/find-with-exec-how-to-perform-multistep-conditional-action-on-each-file
http://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error

Published inProductivity

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *