Check Directory Existence Script

LinuxLinuxBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-387372{{"`Check Directory Existence Script`"}} linux/echo -.-> lab-387372{{"`Check Directory Existence Script`"}} linux/read -.-> lab-387372{{"`Check Directory Existence Script`"}} linux/mkdir -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/shebang -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/comments -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/quoting -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/variables_usage -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/cond_expr -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/read_input -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/cmd_substitution -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/subshells -.-> lab-387372{{"`Check Directory Existence Script`"}} shell/globbing_expansion -.-> lab-387372{{"`Check Directory Existence Script`"}} end

Check Directory Existence Script

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".

Requirements

  • The script should be named ~/project/directory_exist.sh.
  • The script should prompt the user to enter a directory name.
  • The script should check if the directory already exists in the current location using the -d option.
  • If the directory exists, the script should print "Directory exist".
  • If the directory does not exist, the script should create the directory using the mkdir command.
  • The script should print "Directory created" after creating the directory.

Example

To run the script, use the following command:

bash directory_exist.sh

Output:

Enter directory name
new_directory
Directory created

Summary

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.

Other Linux Tutorials you may like