bash

Send mail with sendmail

#!/bin/bash SENDMAIL=/usr/sbin/sendmail RECIPIENT=tosomeone@example.com FROM=me@example.com cat <<EOF | $SENDMAIL -t ${RECIPIENT} From: ${FROM} To: ${RECIPIENT} Subject: testmail some test text as body of the email. EOF

UTF-8 Character List

Common: “ ” ‘ ’ – — … ‐ ‒ ° © ® ™ • ½ ¼ ¾ ⅓ ⅔ † ‡ µ ¢ £ € « » ♠ ♣ ♥ ♦ ¿ � Math: - × ÷ ± ∞ π ∅ ≤ ≥ ≠ ≈ ∧ ∨ ∩ ∪ ∈ ∀ ∃ ∄ ∑ ∏ ← ↑ → ↓ ↔ ↕ ↖ ↗ ↘ ↙ ↺ ↻ ⇒ ⇔

Linux Snippets

Find and chmod files or folders find . -type d -exec chmod 755 {} \; find . -type f -exec chmod 644 {} \; Find a directory and display on screen find . -type d -name 'linux' 2>/dev/null Find/Grep for a string across multiple files with different extensions find . -name "*.php" | xargs grep -niP 'thingy' find \( -name "*js" -o -name "*jsp" -o -name "*jspf" \) | xargs grep -niP 'thingy' Find and replace find .

Log Rotate

Simple: Create following file Change the MAXSIZE & LOGDIR (see file) Add a cron job Create following file: nano /var/www/log/logrotate.sh #!/bin/bash MAXSIZE=1024 LOGDIR=/var/www/log/ if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" 1>&2 exit 1 fi cd $LOGDIR for FILENAME in *.log; do SIZE=$(du -b $FILENAME | cut -f 1) if(($SIZE>$MAXSIZE)); then TIMESTAMP=`date +%Y%m%d` NEWFILENAME=$FILENAME.$TIMESTAMP mv $FILENAME $NEWFILENAME touch $FILENAME chown www-data.

Network Safety Restart Bash Script

If you ever work on a remote servers network settings then this script may safe you from having to call support, and waiting on them. When started/executed it Sleeps first for 1 hour Then it renames the ”/etc/network/interfaces” file by adding the current time stamp to the end of the file It renames a file called ”/etc/network/interfaces.org” to ”/etc/network/interfaces” And finally it restarts the server. It also warns you a couple minutes before it does all that so you can terminate the program.

Linux bash history

Have you ever executed something on the linux shell and didn’t remember later how it was done? Well if you remember just part of it you can search for it: history | grep -i "[search string]"

git branch on bash line

This little code, if placed in to your ~/.bash_profile file will reveal what git branch you are working on. parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/' } if [[ $EUID -ne 0 ]]; then PS1="w$(parse_git_branch) $ " fi

Linux backround process

Running as background process with nohub nohup scp & > nohup.out 2 > &1 nohup scp -r -p root@www.example.com:/var/www/ /var/www/ & >nohup.out 2>&1 nohup scp -r -p root@www.example.com:/var/www/logs /var/www/ & >nohup.out 2>&1

Linux find & replace HowTo

Find and chmod files or folders find . -type d -exec chmod 755 {} ; find . -name "*.php" | xargs grep -niP 'thingy' Find a directory and display on screen find . -type d -name 'linux' 2>/dev/null Find/Grep for a string across multiple files with different extensions find ( -name "*js" -o -name "*jsp" -o -name "*jspf" ) | xargs grep -niP 'thingy'