Skip to content

Instantly share code, notes, and snippets.

@gagregrog
Last active January 10, 2020 19:36
Show Gist options
  • Save gagregrog/0c953b6ad3ae01323a7c80cec5a2a360 to your computer and use it in GitHub Desktop.
Save gagregrog/0c953b6ad3ae01323a7c80cec5a2a360 to your computer and use it in GitHub Desktop.
Bad bash snippets that I'm liable to forget

bad bash snippets

mkmonths() {}

Create a folder for every month of the current year, or a provided year, in the current directory.

mkmonths () {
	y=$([ $# -eq 1 ] && echo "$1" || echo $(date +"%Y"))
	for i in {1..12}; do mkdir "$([ "$i" -gt 9 ] && echo "$i" || echo "0$i")-${y}"; done
}

Use

Make current year's months.

mkmonths

Make a given year's months.

mkmonths 2019

search() {}

Search for a text string in all files, excluding node_modules, from the current or a provided directory.

search () {
  if [ $# -eq 0 ]
    then
      echo "Provide a word in quotes to search for optionally followed by a directory to search in."
    else
      if [ $# -eq 1 ]
        then
          grep --exclude-dir=node_modules -Rl $1 .
        else
          grep --exclude-dir=node_modules -Rl $1 $2
      fi			
  fi
}

### Use

```bash
search "import React from 'react'"
search "import cv2" ~/Documents/programming/python

pyclean() {}

Remove compiled python files from the current directory.

pyclean () {
    find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment