Introduction
In Bash scripting, it is important to check the existence of a directory before creating it. This challenge will demonstrate how to create a directory by checking its existence in the current location.
In Bash scripting, it is important to check the existence of a directory before creating it. This challenge will demonstrate how to create a directory by checking its existence in the current location.
Create a Bash script named directory_exist.sh
that prompts the user to enter a directory name. The script should then check if the directory already exists in the current location. If the directory exists, the script should print "Directory exist". If the directory does not exist, the script should create the directory and print "Directory created".
~/project/directory_exist.sh
.-d
option.mkdir
command.To run the script, use the following command:
bash directory_exist.sh
Output:
Enter directory name
new_directory
Directory created
In this challenge, we learned how to create a directory by checking its existence in the current location using Bash scripting. By using the -d
option, we were able to test if a particular directory exists or not before executing the mkdir
command.