Search code examples
unix

Check if directory exists and delete in one command


Is it possible to check if a directory exists and delete if it does, in Unix, using a single command?

I have situation where I use Ant sshexec task where I can run only a single command in the remote machine. And I need to check if directory exists and delete it.


Solution

  • Assuming $WORKING_DIR is set to the directory... this one-liner should do it:

    if [ -d "$WORKING_DIR" ]; then rm -Rf $WORKING_DIR; fi
    

    (otherwise just replace with your directory)