Skip to content

Instantly share code, notes, and snippets.

@rbecheras
Last active March 29, 2018 08:11
Show Gist options
  • Save rbecheras/f90e2844c32acbf84ecf10187c1834e9 to your computer and use it in GitHub Desktop.
Save rbecheras/f90e2844c32acbf84ecf10187c1834e9 to your computer and use it in GitHub Desktop.
Include this function in your bashrc script to use mkcd anywhere in bash.
function mkcd () {
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo "Usage : mkcd [OPTIONS] [PATH]
Use 'mkdir -p' to create a directory then 'cd' into it
Options:
-h, --help Display help"
return 0
elif [[ -z $1 ]]; then
echo "Error: missing argument. mkcd takes exactly one argument"
return 1
else
mkdir -p $1 && cd $1
return 0
fi
}

mkcd

Install

$ cd /tmp
$ wget https://gist.githubusercontent.com/rbecheras/f90e2844c32acbf84ecf10187c1834e9/raw/ed44b447ff9af292eb00bbacc5766c809a6228a9/mkcd-function.sh
$ cp mkcd-function.sh $HOME/.mkcd-function.sh
$ file=$HOME/.mkcd-function.sh
$ echo "if [[ -f $file ]]; then include $file; fi" >> $HOME/.bashrc
$ exit

Usage

$ mkcd --help
Usage : mkcd [OPTIONS] [PATH]
Use 'mkdir -p' to create a directory then 'cd' into it
Options:
  -h, --help            Afficher l'aide

Exemple

$ pwd
/home/developer
$ mkcd foo/$(date +%F)/$(uuidgen)
$ pwd
/home/developer/foo/2018-03-29/4d2ecbb5-7737-4bdf-8aba-30cf4e303c57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment