Creating a directory

Files / Basic operations

directory="data"

mkdir "$directory" && created1=true || created1=false
#created1 is true

#the directory is already there: mkdir fails with a message to stderr
mkdir "$directory" 2> /dev/null && created2=true || created2=false
#created2 is false

#mkdir makes one level only: "data/sub/inner" needs "data/sub" first
mkdir "data/sub/inner" 2> /dev/null && created3=true || created3=false
#created3 is false

#-p makes all the missing levels and is silent when the directory exists
mkdir -p "data/sub/inner" && created4=true || created4=false
#created4 is true

#-m sets the rights: the owner may write, everyone may read
mkdir -m 755 "data/public"
rights=$(stat -c %a "data/public")
#rights is 755

echo "created1 is $created1"
echo "created2 is $created2"
echo "created3 is $created3"
echo "created4 is $created4"
echo "rights is $rights"
rm -r "$directory"