Linux Shell Scripting: Check if Folder Exists

If we want to check if a folder exists we simply create the following .sh file:

#!/bin/bash
 
if [ ! -d "myFolder" ]; then
	echo "Creating myFolder"
	sudo mkdir myFolder
fi
 
if [ ! -d "myFolder/subFolder" ]; then
	echo "Creating myFolder/subFolder"
	sudo mkdir myFolder/subFolder
fi

Now you can run that using sudo and for example create a new folder where one didn’t exist before, if its required by some other script, for example one mounting a windows XP drive over a network to the current Linux filesystem using Samba. See the post covering that for more details.