Masatoshi Nishiguchi
Software Engineer

Simple bash script to create a new git project repository

shell git unix

I learned basics on Linux operating system following Linux Foundation’s online lectures a few month ago. After writing more than several practice scripts for the sake of learning, I decided to write a practical custom command to create a new project directory and initialize it as a local git repository.

I jus thought it was a great opportunity to utilize what I learned. Just as usual, I started by googling around for general ideas. As it turned out, it was pretty straightforward. All we need to do is just place the script in /usr/local/bin to make my custom scripts globally available.

My goals

Creating git_mkdir script

#!/bin/bash

# Ask the user for a directory name to be created.
echo "Enter the directory name to be created:"
read dirname

# Create a new directory with that name and CD into it.
mkdir $dirname
cd ./$dirname

# Report the working directory.
echo "$dirname has been created"
echo "Moved into $( pwd )"

# Create a few files.
touch index.html
echo "# $dirname" > README.md
echo ".DS_Store" > .gitignore

# Initialize git.
git init
git add -A
git commit -m "Initial commit"

# Report the filenames that have been created.
ls -hartl

# Tell the user that this script is done.
echo "Done!"

How to run a custom script?

sh path/to/the/script

How to make a shell script global?

# Move to the directory that contains the script file
cd path/to/script/dir

# Make a symlink to /usr/local/bin/
ln -sf $PWD/git_mkdir /usr/local/bin/

# Verify the symlink creation
ls -a /usr/local/bin/ | grep git_mkdir

Now I can run the command from anywhere.

git_mkdir

Conclusion

I am very happy about this script so far because I was able to factor out and automate the initial steps of creating a new project repository. With this script, I don’t need to manually initialize the repository and create README any more!