Git

Git Commands

General

Loop over directory and execute command

Recursive

dirs=$(find . -type d 2>/dev/null | cut -c 3-)

# needed for zsh
setopt sh_word_split

SAVEIFS=$IFS   # Save current IFS
IFS=$'\n'      # Change IFS to new line
dirs=($dirs)   # split to array
IFS=$SAVEIFS   # Restore IFS

for dir in $dirs; do
	originalDir=$(pwd)
	cd $dir
	COMMAND ##### REPLACE THIS
	cd $originalDir
done

Replace COMMAND with your command

Example:

dirs=$(find . -type d 2>/dev/null | cut -c 3-)

# needed for zsh
setopt sh_word_split

SAVEIFS=$IFS   # Save current IFS
IFS=$'\n'      # Change IFS to new line
dirs=($dirs)   # split to array
IFS=$SAVEIFS   # Restore IFS

for dir in $dirs; do
	originalDir=$(pwd)
	cd $dir
	pwd ### REPLACE THIS
	cd $originalDir
done

Immediate Dir

dirs=$(find . -maxdepth 1 -type d 2>/dev/null | cut -c 3-)

# needed for zsh
setopt sh_word_split

SAVEIFS=$IFS   # Save current IFS
IFS=$'\n'      # Change IFS to new line
dirs=($dirs)   # split to array
IFS=$SAVEIFS   # Restore IFS

for dir in $dirs; do
	originalDir=$(pwd)
	cd $dir
	COMMAND #### REPLACE THIS
	cd $originalDir
done

Check if File Exists

if test -f "FILE"; then
    YOURCOMMAND
fi

Example:

if test -f "package.json"; then
    echo $FILE
fi

Find all directories in a folder

Recursive:

find . -type d | cut -c 3-

1 level:

find . -maxdepth 1 -type d | cut -c 3-

Ignoring Permission Errors

find . -maxdepth 1 -type d -print 2>/dev/null | cut -c 3-

Make array out of new lines

items="Item 1
Item 2
Item 3"

# needed for zsh
setopt sh_word_split

SAVEIFS=$IFS   # Save current IFS
IFS=$'\n'      # Change IFS to new line
items=($items)   # split to array
IFS=$SAVEIFS   # Restore IFS

for item in $items; do
	COMMAND #### REPLACE THIS
done

Example

items="Item 1
Item 2
Item 3"

# needed for zsh
setopt sh_word_split

SAVEIFS=$IFS   # Save current IFS
IFS=$'\n'      # Change IFS to new line
items=($items)   # split to array
IFS=$SAVEIFS   # Restore IFS

for item in $items; do
	echo $item #### REPLACE THIS
done

Make Function with Flags

function FUNCTIONNAME { #### REPLACE FUNCTIONNAME with your function name
	while test $# -gt 0; do # For each argument
	  case "$1" in
	    -h|--help)
	      echo "-h, --help                show brief help"
	      echo "-f					      boolean flag command -f"
		  echo "--action ACTION			  flag with value"
	      exit 0
	      ;;
	    -f)
	      shift
	      FLAG=true #### CHANGE THIS
	      ;;
	    --action*) #### CHANGE THIS
		  shift
	      ACTION=`echo $1 | sed -e 's/^[^=]*=//g'`
	      ;;
	    *)
		  shift
	      ARGS=`echo $1`
	      break
	      ;;
	  esac
	done
	
	[[ FLAG ]] && echo "Flagged"
	[[ ACTION ]] && echo "ACTION: $ACTION"
	[[ ARGS ]] && echo $ARGS
}

FUNCTIONNAME -f --action testing word

App Shortcuts

iTerm