Little Girl's Mostly Linux Blog

CheckIfADirectoryExists

Check if a directory exists

This page was last updated on January 19, 2017.
Use this Bash script, replacing /test with the full path to a possible directory, to check if the directory exists and tell you the result:
#!/bin/bash

target="/test"

echo "Checking for the $target directory";
if
	[ -d $target ];
then
	echo "The $target directory exists.";
else
	echo "The $target directory does not exist.";
fi;
Use this Bash script, replacing /test with the full path to a possible directory, to check if the directory exists, create it if it doesn’t, and tell you the results:
#!/bin/bash

target="/test"

echo "Checking for the $target directory";
if
	[ -d $target ];
then
	echo "The $target directory exists.";
else
	mkdir $target;
	echo "The $target directory did not exist and has been created.";
fi;

Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: