GoofyBall
GoofyBall

Reputation: 411

sh - Delete Directory Whenever it Appears

In my application, whenever I open VS2013 there is an annoying folder being generated which causes my whole build to fail. The folder has some old dependencies which I do not want. I couldn't be bothered to find out why it is appearing there: I just want it gone when VS2013 starts up.

I need a simple script to delete it whenever it appears. To remove a directory we do:

rm -rf FolderThatWontGoAway

But is there way to magically tell a sh script to delete something when it shows up in the file system? Something like:

if [[ -z $(find -name "FolderThatWontGoAway") ]];
then 
    echo "Folder is not here."
else
     #remove!
     rm -rf FolderThatWontGoAway 
fi

Note: A cron would not be suitable because I open and close VS2013 too erratically to know how frequently this job should run.

Upvotes: 0

Views: 134

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74605

You could do this using inotifywait. Something like this:

inotifywait -m -e create . | 
while read dir ev file
do 
    if [ -d "$file" ] && [ "$file" == "FolderThatWontGoAway" ]
    then 
        rm -rf "$file"
    fi
done

This will monitor the current directory for "create" events. If a new directory is created with the name "FolderThatWontGoAway", it will remove the folder and its contents.

Upvotes: 1

Related Questions