mahesh
mahesh

Reputation: 274

Is there anyway to create a forcefully directory even if it exists at the source location

Is there any way to create a directory even if it exists. I want to override the existing directory with newly directory. supose i have a directory dir1(which has some contents inside it). Now i want to create the same directory as a dir1 ,but it's not happening for me. I don't know how do i go about it?

Upvotes: 0

Views: 2050

Answers (1)

Francisco Puga
Francisco Puga

Reputation: 25158

I think that the only way to achieve that is to use rm to delete the folder and mkdir to create it after.

You can create a previous check to only remove the folder if it exits with something like:

[[ -d "$FOLDER" ]] && rm -rf "$FOLDER"
mkdir -p "$FOLDER"

or in one sentence

{ [[ -d "$FOLDER" ]] && rm -rf "$FOLDER"; } ; mkdir -p "$FOLDER";

If you want to keep it simple:

rm -rf "$FOLDER" && mkdir -p "$FOLDER"

Upvotes: 1

Related Questions