How might one make a bash script which when run will check for the existence of a file/folder and if the file/folder is there remove it?
22 Answers
There's no point in testing if it exists or not, rm -rf already does that for you. If it exists, rm removes it. If it doesn't exist, rm considers the job already done, and just returns with success.
#!/usr/bin/env bash
rm -rf "$@"Which also means there's not really any point in the script, since you can just run rm -rf /some/dir instead of scriptname /some/dir.
Simple if statement with rm -rf command
#!/bin/bash
if [ -e "$1" ];then rm -rf "$1" ; fi 1