Create Directory with Bash Script

LinuxLinuxBeginner
Practice Now

Introduction

This challenge aims to test your knowledge of Bash commands and your ability to write a Bash script. In this challenge, you will create a Bash script that creates a new directory.


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/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-387371{{"`Create Directory with Bash Script`"}} linux/echo -.-> lab-387371{{"`Create Directory with Bash Script`"}} linux/read -.-> lab-387371{{"`Create Directory with Bash Script`"}} linux/mkdir -.-> lab-387371{{"`Create Directory with Bash Script`"}} shell/shebang -.-> lab-387371{{"`Create Directory with Bash Script`"}} shell/comments -.-> lab-387371{{"`Create Directory with Bash Script`"}} shell/quoting -.-> lab-387371{{"`Create Directory with Bash Script`"}} shell/variables_usage -.-> lab-387371{{"`Create Directory with Bash Script`"}} shell/cond_expr -.-> lab-387371{{"`Create Directory with Bash Script`"}} shell/read_input -.-> lab-387371{{"`Create Directory with Bash Script`"}} shell/globbing_expansion -.-> lab-387371{{"`Create Directory with Bash Script`"}} end

Directory Creation

Create a Bash script named make_directory.sh that takes a new directory name from the user. If the directory name does not exist in the current location, the script should create the directory. Otherwise, the script should display an error message.

Requirements

  • The Bash script should be named ~/project/make_directory.sh.
  • The script should take a new directory name from the user.
  • If the directory name does not exist in the current location, the script should create the directory.
  • If the directory name already exists in the current location, the script should display an error message.

Example

To run the script, use the following command:

bash make_directory.sh

Output:

Enter directory name
new_directory
Directory created successfully

Summary

In this challenge, you have learned how to create a new directory using the mkdir command in Bash. You have also learned how to write a Bash script that takes user input and performs conditional checks to create a new directory or display an error message.

Other Linux Tutorials you may like