diff --git a/bash/lib/_remind.sh b/bash/lib/_remind.sh new file mode 100644 index 0000000..6db02a2 --- /dev/null +++ b/bash/lib/_remind.sh @@ -0,0 +1,172 @@ +r_init() { + r_HELP="" +} + + +## --------- +## FILES +## --------- + +r_files() { + files_HELP=( + "FILES" + "--------" + "topfiles [dir] - finds the largest files in the directory provided (or current dir if not specified)" + "topdirs - finds largest directories on the system" + ) + for (( i=0; i<${#files_HELP[@]}; i++ )); + do + r_HELP+="${files_HELP[$i]}\\n"; + done +} + +## --------- +## PROCESSES +## --------- + +r_processes() { + process_HELP=( + "PROCESSES" + "--------" + "findpid - finds the process id the process containig " + "pid - returns process info about specified" + "" + "memhogs - see processes ranked by memory usage" + "cpuhogs - see processes ranked by cpu usage" + "" + "my_ps - see list of ALL processes under my user" + ) + for (( i=0; i<${#process_HELP[@]}; i++ )); + do + r_HELP+="${process_HELP[$i]}\\n"; + done +} + +## --------- +## NETWORK +## --------- + +r_network() { + net_HELP=( + "NETWORK" + "--------" + "myip - returns the external ip address our LAN is using" + "myiip - returns the internal network ip address this computer is using" + "netexp - ping the modem to see ip addresses of other computers on the network" + "" + "findport - find process id using port" + "openports - list all open ports" + "suopenports - list all open ports (visible to super user)" + "" + "users - list currently connected users to this computer" + ) + + for (( i=0; i<${#net_HELP[@]}; i++ )); + do + r_HELP+="${net_HELP[$i]}\\n"; + done +} + + +r_install() { + install_HELP=( + "INSTALLATION" + "-----" + ) + + install_HELP+=( + "yum list installed - view installed packages" + "sudo yum update -v - update packages and yum" + ) + + for (( i=0; i<${#install_HELP[@]}; i++ )); + do + r_HELP+="${install_HELP[$i]}\\n"; + done +} + +r_search() { + search_HELP=( + "SEARCH" + "------" + "grep - print lines matching in " + "grep -v - print lines without " + "grep -r - print lines in files inside (recursively)" + "" + "sed -n '/abc/p' - print lines that contain abc" + "sed 's/old-text/new-text/g' - replace all instances of 'old-text' with 'new-text'" + "sed -e '/FOO/s/love/sick/' - replace 'love' with 'sick' if line contains 'FOO'" + "" + "awk '{print \$1,\$4}' - Print the 1st and 4th word" + "awk '{print NR,\$0}' - Print the line number, followed by the whole row" + "awk '{ if (NF > max) max = NF; line=\$0 } END { print line }' - Print line with most words" + "awk 'length(\$0) > 10' - Print lines that are larger than 10 characters " + "" + "find -name '' - find file matching pattern recursively below path" + "qfind - search directories below recursively for file with name = file_name" + ) + + for (( i=0; i<${#search_HELP[@]}; i++ )); + do + r_HELP+="${search_HELP[$i]}\\n"; + done +} + + +function remind () { + INTRO=( + "which topic?" + "[A]ll" + "[f]iles" + "[p]rocesses" + "[n]etwork" + "[i]nstall" + "[s]earch" + ) + + clear + if [[ $1 == "" ]]; then + echo "${INTRO[*]}" + read response + else + response=$1; + fi + + r_init + case $response in + A) + r_files + r_processes + r_network + r_install + r_search + ;; + f) + r_files + ;; + p) + r_processes + ;; + n) + r_network + ;; + i) + r_install + ;; + s) + r_search + ;; + esac + + + case $response in + A) + echo -e $r_HELP | less + ;; + *) + echo -e $r_HELP + ;; + esac +} +alias r\?=remind +alias \?=remind diff --git a/bash/my_profile b/bash/my_profile index 1886a3e..080b17e 100644 --- a/bash/my_profile +++ b/bash/my_profile @@ -73,6 +73,8 @@ alias .5='cd ../../../../../' alias .6='cd ../../../../../../' alias ~="cd ~" +alias clsls="clear; ls" + alias clr="clear" alias cls="clear" alias c="clear" @@ -240,49 +242,4 @@ alias inbox="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --ne # 9. HELPER FUNCTIONS # --------------------------------------- -## based on https://coderwall.com/p/pn8f0g/show-your-git-status-and-branch-in-color-at-the-command-prompt -function git_branch { - local git_status="$(git status 2> /dev/null)" - local on_branch="On branch ([^${IFS}]*)" - local on_commit="HEAD detached at ([^${IFS}]*)" - - if [[ $git_status != "" ]]; then - local branch_msg="("; - else - local branch_msg=" " - fi - - if [[ $git_status =~ "Untracked files:" || $git_status =~ "Changes not staged for commit:" ]]; then - branch_msg+="> " - elif [[ $git_status =~ "" ]]; then - branch_msg+="" - fi - - if [[ $git_status =~ $on_branch ]]; then - local branch=${BASH_REMATCH[1]} - branch_msg+="$branch" - elif [[ $git_status =~ $on_commit ]]; then - local commit=${BASH_REMATCH[1]} - branch_msg+="$commit" - fi - if [[ $git_status =~ "Your branch is ahead of" ]]; then - branch_msg+=" +" - local num=`git status | grep -o '[0-9]\+'` - branch_msg+="$num" - elif [[ $git_status =~ "" ]]; then - branch_msg+="" - fi - - if [[ $git_status =~ "Changes to be committed:" ]]; then - branch_msg+="*" - elif [[ $git_status =~ "" ]]; then - branch_msg+="" - fi - - if [[ $git_status != "" ]]; then - branch_msg+=")" - fi - - echo "$branch_msg" -} diff --git a/bash/server_profile b/bash/server_profile old mode 100644 new mode 100755 index d2954ab..8c8bba0 --- a/bash/server_profile +++ b/bash/server_profile @@ -1,20 +1,134 @@ # .bash_profile for servers (Displays the server name in the Command Prompt) -SERVER_NAME="Example Server Name" +# message on signout? +# -# Get the aliases and functions -if [ -f ~/.bashrc ]; then - . ~/.bashrc -fi +# ------------------------------- +# 1. ENVIRONMENT CONFIGURATION +# ------------------------------- -# User specific environment and startup programs +## Set Path +############## -PATH=$PATH:$HOME/.local/bin:$HOME/bin +#?: will we do anything here? -export PATH +# Set Default Editor +# ------------------------------------------------------------ +export EDITOR=/usr/bin/vim -## [\e] == esc character -## \a\ == bell character (between these sets the terminal title) -PS1="\[\e]0;(${SERVER_NAME}): \w\a\][\u@\h \w] \$" +# My Scripts + +CUR_DIR=$(dirname "$BASH_SOURCE[0]") +export PATH="$PATH:$CUR_DIR/../scripts" + +source $CUR_DIR/lib/_remind.sh + +## Set PROMPT +############## + +# start underline and print current directory +PS1="$(tput sgr 0 1) \`pwd\`" + +# add spaces based on width +PS1+="\`printf '%*s' \$(max 1 \$(( \$((\$(tput cols) - 15)) - \$( pwd | wc -m | grep -o '[0-9]\+') - \$(gitstatus | wc -m | grep -o '[0-9]\+') - $(whoami | wc -m | grep -o '[0-9]\+') - $(hostname | wc -m | grep -o '[0-9]\+')))) ''\`" + +# add git status and stop underline +PS1+="\`gitstatus\`$(tput sgr0)| " + +# print user and hostname +PS1+="\u @ \h\n| =>" +export PS1 +export PS2="|>" + + +# ------------------------------- +# 2. TERMINAL ALIASES AND FUNCTIONS +# ------------------------------- +alias cls=clear +alias clsls="cls; ls" + +alias ls="ls -FGlAhp" +alias less="less -FSRXc" + +function mcd() +{ + mkdir $1; + cd $1; +} + +alias ivm=vim + +# --------------------------- +# 3. FILE AND FOLDER MANAGEMENT +# --------------------------- + +topfiles() { + du -a "$@" 2> /dev/null | sort -n -r | head -n 10 +} + +# --------------------------- +# 4. SEARCHING +# --------------------------- + +alias qfind="find . -name " + + +# --------------------------- +# 5. PROCESS MANAGEMENT +# --------------------------- + +findPid () { lsof -t -c "$@" ; } + +alias memhogstop='top -l 1 -o rsize | head -20' +alias memhogsps='ps wwaxm -o pid,stat,vsize,rss,time,comm | head -10' +alias memhogs=memhogsps + +alias cpuhogs='ps wwaxr -o pid,stat,%cpu,time,comm | head -10' + +my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,start,time,bsdtime,comm ; } + +alias users='cat /etc/passwd | cut -d ":" -f 1' + +# --------------------------- +# 6. NETWORKING +# --------------------------- + +alias myip='dig +short myip.opendns.com @resolver1.opendns.com' + +myiip () { + local ip info + ip="" + i=0 + while [[ -z $ip && $i < 10 ]]; do + ip=`ifconfig eth${i} | grep -e "inet" | awk '{print $2}'` + if [[ ! -z $ip ]]; then + break + fi + i=$((i + 1)) + done + echo $ip +} + +netexp () { + echo "Pinging Network..." + ping `myiip | sed 's/\([0-9]*\.[0-9]*\.[0-9]*\.\)[0-9]*/\1255/'` -c5 -q > /dev/null + eval 'arp -a' +} + + +findPort () { + lsof -nP -iTCP:$1 | grep LISTEN ; +} + +alias netcons="lsof -i" +alias openports="lsof -i | grep LISTEN" +alias suopenports="sudo lsof -i | grep LISTEN" +alias sudopenports=suopenports + +alias on="w -h" + +# --------------------------------------- +# 7. HELPER FUNCTIONS +# --------------------------------------- diff --git a/motd.sh b/motd.sh new file mode 100755 index 0000000..46e2a74 --- /dev/null +++ b/motd.sh @@ -0,0 +1,160 @@ +#! /bin/bash + + +TREEHOUSE=( +" ()" +" ()" +" vvv^^^()vvvv" +" vvvvvv^^v()vvv^^vvvv" +" vvvvvvvv^^^^^^^^^^^^^()vvv^^^vvvv" +" vvvvvvvv^^^v^^^vvvvvv^^vvv()vvvv^^^vvvvvvv" +" ^vvvvvvvvv^^^^vvvvvv^^^()^vvvvvvvv^^^vvvvv^v" +" vv^^vvv^^^vvvvvv^vvvvv^vvv()v^^^vvvvvvv^^vv^" +" vv^vvvv^^vvv^^vvvvv^^vvvvv^()#vvv^vvv^^vvvv^v" +"^vvvv^^vvvvvv^vv^vvv^^^^^^__[]__#^^^vvvvv^^^^" +" ^^vvvvv^^vvvvvvvvv^^^^/\@@@@@@\\#vvvv^^^" +" ^^vvvv^^^^^vvvv/__\@@@@@@\^vvvv^v" +" ;^^vvvvvvv/____\@@@@@@\\vvvvvvv" +" ; \_ ^\|[ -:] ||--| | _/^^" +" ; \ |[ :] ||_/| |/" +" ; \\\\||___:]______/" +" ; \ ;=; /" +" ; | ;=;|" +" ; () ;=;|" +" (() || ;=;|" +" / / \;=;\\" +) + +MSG=( + "" +) +OG_LENGTH=${#MSG[*]} + +# Fetch Weather +RES=$(curl "https://wttr.in/Portland?0" 2> /dev/null) +OLDIFS=$IFS +IFS=$'\n' +WTTR=($RES) +IFS=$OLDIFS + +# Add Weather to top of motd +wttr_length=${#WTTR[@]} +for (( i=0; i<${wttr_length}; i++ )); +do + MSG[$((${i}+$OG_LENGTH))]+=${WTTR[$i]} +done + + +HOST=$(hostname) +GROUP_COUNT=$(cat /etc/group | wc -l) +USER_COUNT=$(cat /etc/passwd | wc -l) +USERS=$(w -h | wc -l) + +CRONS=$(crontab -l | grep -e '^[^#]' | wc -l) +PATH_SIZE=$(echo $PATH | tr ':' '\n' | wc -l) + + +#HD_SIZE=$(sudo du -hd1 / 2>/dev/null | grep -P "^\d.?\dG" | sort -n | tail -n1) + +OIFS=$IFS; +IFS=$'\n' +HISTORY=($(cat ~/.bash_history | tail -3)) +IFS=$OIFS; + +HISTORY_DIRS=($(cat ~/.bash_history | grep '^cd' | cut -d' ' -f2 | tail -3)) + +# TODO: WIP on custom message (server details/stats) +# mounted drives +# size of disk +# unique apps/dirs +# [x] users/groups +# [x] crontabs +# [..] history +# [..] path +# [..] current users/ips/ports/connections + + +MSG+=( + "" + " (defined in /etc/profile.d/motd.sh)" + "|----------------------------------------------|" + "| host: ${HOST}" + "| users: ${USER_COUNT} | groups: ${GROUP_COUNT} | online: ${USERS} | crons: ${CRONS}" + "| pathsize: ${PATH_SIZE} ( 'compgen -c' to view )" + "| disk size: $( echo ${HD_SIZE%/} | xargs) ( 'topdirs to view )" + "|- (history) -- cmd ----------- dirs ----------|" + "| ${HISTORY[2]:0:20}" + "| ${HISTORY[1]:0:20}" + "| ${HISTORY[0]:0:20}" + "|----------------------------------------------|" +) + +length=${#MSG[@]} +start=$((${length}-4)) +end=$((${length}-1)) +for (( j=$start; j<${end}; j++ )); +do + i=$((${length}-${j}-3)) + + # Find Visible Character Count + CHAR=$(echo -n -e ${MSG[$j]}| sed "s/$(echo -n -e "\e")[^m]*m//g" | tr -d ' ' | wc -c) + + # Find Visible White Space + WHITE_SPACE=$(echo -n -e ${MSG[$j]} | tr -d -c ' ') + + # Pad end with spaces + PADDING=$((25-$CHAR-${#WHITE_SPACE})) + MSG[$j]+=$(printf ' %.0s' $(seq 1 $PADDING)) + + MSG[$j]+="| ${HISTORY_DIRS[$i]:0:20}" +done + +if [[ $(hostname) == "dev-junk.com" ]]; then + # if no term then do nothing + if [ -z $TERM ]; then + echo "No Terminal" + # If width of terminal > 100 characters (add treehouse to motd) + elif [ $(tput cols) -gt 93 ]; then + # Loop over treehouse and add to printed message + length=${#TREEHOUSE[@]} + for (( j=0; j<${length}; j++ )); + do + # Find Visible Character Count + CHAR=$(echo -n -e ${MSG[$j]} | sed "s/$(echo -n -e "\e")[^m]*m//g" | tr -d ' ' | wc -c) + + # Find Visible White Space + WHITE_SPACE=$(echo -n -e "${MSG[$j]}" | tr -d -c ' ' ) + + # Pad end with whitespace + PADDING=$((50-$CHAR-${#WHITE_SPACE})) + MSG[$j]+=$(printf ' %.0s' $(seq 1 $PADDING)) + + # Add Tree House + MSG[$j]+=${TREEHOUSE[$j]} + + #MSG[$j]=$(pad 50 ${MSG[$j} ${TREEHOUSE[$j]}) + done + fi +fi + +printf -v PRINT "%s\n" "${MSG[@]}" +echo "$PRINT" + + + +# TODO: Abstract this +pad() { + # Find Visible Character Count + CHAR=$(echo -n -e $2 | sed "s/$(echo -n -e "\e")[^m]*m//g" | tr -d ' ' | wc -c) + + # Find Visible White Space + WHITE_SPACE=$(echo -n -e $2 | tr -d -c ' ') + + # Pad end with spaces + PADDING=$(($1-$CHAR-${#WHITE_SPACE})) + + echo "$(printf ' %.0s' $(seq 1 $PADDING))| $3" +} + +# pad "20" "test" 'test34' + diff --git a/scripts/topdirs b/scripts/topdirs new file mode 100755 index 0000000..ad65656 --- /dev/null +++ b/scripts/topdirs @@ -0,0 +1,16 @@ +#! /bin/bash + +GIG_FOLDERS=$(sudo du -h / 2>/dev/null | grep -P "^\d.?\dG" | sort -n | tail -n30) + +UNIQUE=() +while IFS= read -r line; do + DIR_NAME=$(echo "$line" | cut -d $'\t' -f 2) + COUNT=$(echo "$GIG_FOLDERS" | grep -o "$DIR_NAME" | wc -l) + if [ $COUNT -eq 1 ]; then + UNIQUE=("${UNIQUE[@]}" "$line") + fi + #echo ${LINE#\d*G} +done <<< "$GIG_FOLDERS" + +printf "%s\n" "${UNIQUE[@]}" + diff --git a/zsh/lib/_remind.zsh b/zsh/lib/_remind.zsh index 35e985d..3d2b09e 100644 --- a/zsh/lib/_remind.zsh +++ b/zsh/lib/_remind.zsh @@ -13,6 +13,10 @@ r_files() { " FILES " "------- " "numFiles - count number of files in current directory" + "topdirs - lists the largest directories on the machine" + "" + "du -a -d1 - show file size (depth=1) in current directoy or wherever passed" + "df -h - show size and amount of storage available on mounted disks" ) if [[ "$OSTYPE" == "darwin"* ]]; then diff --git a/zsh/lib/alias_osx.zsh b/zsh/lib/alias_osx.zsh index c7af555..c1245bd 100644 --- a/zsh/lib/alias_osx.zsh +++ b/zsh/lib/alias_osx.zsh @@ -5,6 +5,8 @@ alias clr="clear" alias cls="clear" alias c="clear" +alias clsls="cls; ls" + ## File Management alias finder="open -a Finder ./"