Remove all blank lines

grep '.' 

Remove single blank lines

cat -s # Remove single blank lines

Remove leading spaces

create_stream |  sed -e 's/^[ \t]*//' | consume_stream

Remove everything after first space

create_stream |  grep -o '^\S*' | consume_stream

Convert all png to jpg

for i in *.png; do 
   convert "$i" "${i/png/jpg}"
done

Pretty Print XML

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | \
    python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()'

Remove empty lines

create_stream | awk 'NF' | consume_stream

grep

find . -exec grep -i "hi rik" '{}' /dev/null \; -print

Use sudo with password as parameter

The -S switch makes sudo read the password from STDIN. This means you can do:

echo mypassword | sudo -S command
  • sudo -i gives you the root environment, i.e. your ~/.bashrc is ignored.
  • sudo -s gives you the user’s environment, so your ~/.bashrc is respected.
  • sudo su

Replace spaces in filenames with underscore

find . -type f -name "* *.md" -print0 | \
  while read -d $'\0' f; do mv -v "$f" "${f// /_}"; done

Reading configs in Bash

configExample.sh

# Try to read the configuration from:
#   1. the working directory
#   2. The user's home directory
# (in that order)
config_file=".slack.conf"
if [ -f "$(pwd)/$config_file" ]; then
  source "$(pwd)/$config_file"
elif [ -f "$HOME/$config_file" ]; then
  source "$HOME/$config_file"
fi

~/.slack.conf

Configuration file (~/.slack.conf) is of the following format:

# Your Slack hook URL
SLACK_HOOK_URL=https://hooks.slack.com/services/BEEF4UP4/BEEF2830/BEEFnA8zI4lbaMjR6zQ8FoWr