Glam Prestige Journal

Bright entertainment trends with youth appeal.

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?

2

2 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.

2

Simple if statement with rm -rf command

#!/bin/bash
if [ -e "$1" ];then rm -rf "$1" ; fi 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy