Skip to content

Instantly share code, notes, and snippets.

@armarti
Last active August 14, 2017 10:29
Show Gist options
  • Save armarti/feed12b1fcc606a94d851d03ff200131 to your computer and use it in GitHub Desktop.
Save armarti/feed12b1fcc606a94d851d03ff200131 to your computer and use it in GitHub Desktop.
# easily add aliases to ~/.bash_aliases
function alias2bash {
cat ~/.bash_aliases > ~/.bash_aliasesx;
echo -e "\n# ($(date)) $1\nalias $2=\"$3\"\n" > ~/.bash_aliasesy;
cat ~/.bash_aliasesx ~/.bash_aliasesy > ~/.bash_aliases;
rm ~/.bash_aliasesx ~/.bash_aliasesy;
nubash;
cat ~/.bash_aliases;
}
# easily add functions to ~/.bash_aliases
function function2bash {
cat ~/.bash_aliases > ~/.bash_aliasesx;
echo -e "\n# $(date) \n# $1\nfunction $2 { $3 ;}\n" > ~/.bash_aliasesy;
cat ~/.bash_aliasesx ~/.bash_aliasesy > ~/.bash_aliases;
rm ~/.bash_aliasesx ~/.bash_aliasesy;
nubash;
cat ~/.bash_aliases;
}
# easily add a routine to bash that will be run during login
function routine2bash {
cat ~/.bash_aliases > ~/.bash_aliasesx;
echo -e "\n# ($(date)) $1\n$2\n" > ~/.bash_aliasesy;
cat ~/.bash_aliasesx ~/.bash_aliasesy > ~/.bash_aliases;
rm ~/.bash_aliasesx ~/.bash_aliasesy;
nubash;
cat ~/.bash_aliases;
}
# ping google once to test internet connection
alias gping='ping google.com -c 1'
# activate new commands in here w/0 neeing to log out/in
alias nubash='source ~/.bashrc'
# copy this user's .bash_aliases to all users
function copybash {
X=$(stat -c %U ~);
#if [ "$X" != "root" ]; then
# echo "Run 'sudo -E copybash'";
# return;
#fi;
if [ "$1" != "" ]; then
TGT_HOME=$(eval echo ~$1);
sudo cp ~/.bash_aliases $TGT_HOME/.bash_aliases &> /dev/null; #the '&> /dev/null' suppresses the "cp: '/fileA' and '/fileA' are the same file" error
sudo chown $1: $TGT_HOME/.bash_aliases;
#source $TGT_HOME/.bashrc; #this runs on the current user's ~, would need to use sudo to sun the command as the tgt_user, not worth it
eval echo ~/.bash_aliases copied to $TGT_HOME;
else
for i in $(dir -A -d /home/*/ /root*/); do
TGT_USER=$(stat -c %U $i)
if [[ "$USER" != "$TGT_USER" ]]; then
sudo mv $i.bash_aliases $i.bash_aliases.old &> /dev/null;
sudo chown $TGT_USER: $i.bash_aliases.old;
echo -e "\n$i.bash_aliases renamed to $i.bash_aliases.old"
sudo cp ~/.bash_aliases $i.bash_aliases &> /dev/null; #must run 'sudo copybash root' to copy to root's home
sudo chown $TGT_USER: $i.bash_aliases;
#source $i.bashrc;
eval echo ~/.bash_aliases copied to $i;
fi;
done;
fi;
echo "";
}
# find something in the current directory
function llfind { ll -A -h | grep -i $1; }
# copy the dcmorse .bash_alias to whatever user I am
#alias cpbash='cp /home/dcmorse/.bash_aliases ~/.bash_aliases && source ~/.bashrc'
#alias cpbash='rm ~/.bash_aliases && cp /home/dcmorse/.bash_aliases ~/.bash_aliases
# open directory & look inside
function cdll { cd $1 && ll -A -h; }
# show login message's source script
function welcome-script {
for i in $(dir /etc/update-motd.d/*);
do echo -e "# ---------- $i ---------- #\n"; cat $i; echo "";
done && echo -e "\n# ---------- /etc/motd.tail ---------- #\n" && cat /etc/motd.tail && echo -e "\n";}
# show login message
alias welcome='for i in $(dir -A /etc/update-motd.d/*); do "$i"; done; "/etc/motd.tail";'
# Sun Jan 3 00:51:35 SYOT 2016
# #find something in chosen directory
function cdllfind { cd $1 && ll -A -h | grep -i $2; }
# Thu Jan 7 22:21:54 SYOT 2016
# show only uncommented items in a file
function ucomments { THE_FILE= && cat $1 | grep -Pv '^\s*#' | grep -v -e '^$' ;}
# (Fri Jan 8 01:12:53 SYOT 2016) restart duh
alias restart="sudo shutdown -r now"
# (Fri Jan 8 20:23:02 SYOT 2016) gets public IP
alias pubip="curl http://dynamicdns.park-your-domain.com/getip && echo \"\""
# Fri Jan 8 23:15:52 SYOT 2016
# a "relilient" hostname resolver from http://serverfault.com/a/543544
function getHostname { local hostname_short=armarti
local hostname_short=`/bin/hostname --short`
if [ $? -ne 0 ]; then
echo "Could not execute 'hostname --short' -- exiting" >&2; exit 1
fi
local hostname_long=`/bin/hostname`
if [ $? -ne 0 ]; then
echo "Could not execute 'hostname' -- exiting" >&2; exit 1
fi
if [[ $hostname_long =~ ^"$hostname_short"\..+$ ]]; then
# "hostname_long" is a qualified version of "hostname_short"
echo $hostname_long
else
# both hostnames are "short" (and are equal)
if [[ $hostname_long != $hostname_short ]]; then
echo "Cannot happen: '$hostname_long' <> '$hostname_short' -- exiting" >&2; exit 1
fi
local domainname=`/bin/domainname`
if [ $? -ne 0 ]; then
echo "Could not execute 'domainname' -- exiting" >&2; exit 1
fi
if [[ domainname == '(none)' ]]; then
# Change according to taste
echo "${hostname_short}.localdomain"
else
echo "${hostname_short}.${domainname}"
fi
fi;
}
# (Sat Jan 9 14:58:15 SYOT 2016) custom functions available
alias functavail="ucomments ~/.bash_aliases | grep -w 'function' | cut -d' ' -f2 | grep -v functavail | sort"
# (Sat Jan 9 15:01:41 SYOT 2016) custom functions available
alias aliasavail="ucomments ~/.bash_aliases | grep -w 'alias' | cut -d' ' -f2 | cut -d= -f1 | sort"
#send all traffic through ppp0
alias pppon="sudo route del default enp3s0 && sudo route add default dev ppp0 && ifconfig"
# edit this file
alias editbash="nano $HOME/.bash_aliases"
# Wed Feb 24 21:47:25 GST 2016
# installs stuff
function aptui { sudo aptitude install $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:48:01 GST 2016
# builds dependencies
function aptub { sudo aptitude build-dep $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:48:27 GST 2016
# removes
function aptur { sudo aptitude remove $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:48:51 GST 2016
# purges
function aptup { sudo aptitude purge $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:49:34 GST 2016
# installs stuff with apt-get
function apti { sudo apt-get install $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:50:17 GST 2016
# builds dependencies with apt-get
function aptb { sudo apt-get build-dep $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:50:47 GST 2016
# removes with apt-get
function aptr { sudo apt-get remove $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:51:03 GST 2016
# purges with apt-get
function aptp { sudo apt-get purge $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:51:31 GST 2016
# updates with apt-get
function aptu { sudo apt-get update $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Feb 24 21:51:53 GST 2016
# updates
function aptuu { sudo aptitude update $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Thu Feb 25 00:54:10 GST 2016
# why the f*#! is this installed, or why whould it be installed
function aptuw { aptitude why $1; }
# (Sun Mar 6 09:54:07 EST 2016) amarti
alias amarti="cat ~/amarti"
# singapore
alias singapore="cat ~/singapore"
# (Sun Mar 6 19:27:50 SYOT 2016) follow syslog as its written
function followsyslog { tail --follow="/var/log/syslog" --lines=10 | grep --color=always --line-buffered --regexp=$1
}
# shows todo list
function show_todo { echo -e "\nTODOs"; grep -ne. ~/.todo 2>/dev/null; echo ""; }
# add to todo list
function new_todo { echo $1 >> ~/.todo; show_todo; }
# deletes a task from toto list
function delete_todo { sed -i "$1 d" ~/.todo; show_todo; }
# (Sat Jan 9 15:04:20 SYOT 2016) shows available functions & aliases
echo -e "\n--- custom functions available ---"
echo $(functavail)
echo ""
echo "--- custom aliases available ---"
echo $(aliasavail)
echo ""
#pppon
echo PATH = \"$PATH\"
show_todo
# Tue May 31 16:08:46 EDT 2016
# like ll but shows less stuff
function lr { ls -lcBh $1 ;}
# search for a directive in all nginx server blocks
function nginxsearch() {
FULLPATH_FILES=$(ls -d1 /etc/nginx/sites-available/*.*)
FULLPATH_DIRS=$(ls -d1 /etc/nginx/sites-available/**)
echo ""
for f in $FULLPATH_FILES;
do
echo ***$f;
grep "\(\sserver_name\|$1\)" $f;
echo ""
done
echo ""
}
# Tue Sep 6 20:17:53 BST 2016
# searches the path for some keyword
function search_path { /home/bbrasky/.local/bin/search_path.sh $1 ;}
# Wed Sep 7 03:59:33 BST 2016
# upgrade with aptitude
function aptug { sudo aptitude update && sudo aptitude upgrade ;}
# Wed Sep 7 22:10:24 BST 2016
# shows description of a package
function aptus { aptitude search $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
function aptush { aptitude show $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Sep 7 23:27:44 BST 2016
# see why a package is or should be installed
function aptuw { aptitude why $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Wed Sep 7 23:28:39 BST 2016
# see why a package is not installed or should be uninstalled
function aptuwn { aptitude why-not $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Fri Sep 9 20:35:19 BST 2016
# reinstall with aptitude
function apturi { sudo aptitude reinstall $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# Fri Sep 9 21:26:27 BST 2016
# see versions of a package with aptitude
function aptuv { aptitude versions $1 $2 $3 $4 $5 $6 $7 $8 $9 ;}
# search sources, display only matching filenames
function search_sources { #( search term(s) in quotes , additional grep options)
grep --color=always $2 -sRnli "$1" "/etc/apt"
}
# search sources, dont show commented stuff
function search_sources+ { #( search term(s) in quotes , additional grep options)
grep --color=always $2 -Rnsve "^\s*#\|^//" "/etc/apt" | grep --color=always -i "$1"
echo ""
grep --color=always $2 -Rnsve "^\s*#\|^//" "/var/lib/apt" | grep --color=always -i "$1"
}
# search sources, show commented stuff
function search_sources++ { #( search term(s) in quotes , additional grep options)
grep --color=always $2 -Rnsie "$1" "/etc/apt"
echo ""
grep --color=always $2 -Rnsie "$1" "/var/lib/apt"
}
# (Sat Dec 3 17:47:40 GMT 2016) does full-upgrade with aptitude
alias aptufu="sudo aptitude update && sudo aptitude full-upgrade"
# Tue Sep 6 20:17:53 BST 2016
# searches systemd locations for some keyword
function search_systemd { /home/bbrasky/.local/bin/search_systemd.sh $1 ;}
# from http://unix.stackexchange.com/a/236711
# should allow rollback of an apt-get install
function apt-history
{
case "$1" in
install)
cat /var/log/dpkg.log | grep 'install '
;;
upgrade|remove)
cat /var/log/dpkg.log | grep $1
;;
rollback)
cat /var/log/dpkg.log | grep upgrade | \
grep "$2" -A10000000 | \
grep "$3" -B10000000 | \
awk '{print $4"="$5}'
;;
*)
cat /var/log/dpkg.log
;;
esac
}
# 20161214
# easier to fix broken stuff on desktop
alias fix_desktop='/home/bbrasky/.local/bin/fix_desktop.sh'
alias desktop='/home/bbrasky/bin/fix_desktop.sh'
# (Thu Dec 15 18:55:40 GMT 2016) finds out swap usage of >=1 process by PID
# alias swapusage="~/.local/bin/swap_usage.sh"
function swapusage
{
grep --max-count=2 --only-matching --no-filename --extended-regexp '^Name:\s+.+$|^VmSwap:\s+.+$' /proc/*/status | cut -d: -f2 | tac | sed 's/^\s\+//g' | sed --regexp-extended ':a;N;$!ba;s/\n([^0-9]+)/\t\t\1/g' | grep --color=never '^[0-9]' | sort --numeric-sort --reverse;
}
# (Mon Dec 19 05:40:41 GMT 2016) show info about the terminal im on
alias mytty='MYTTY=$(tty); who -a|grep "${MYTTY/\/dev\//}\s\+"'
# 20161220
# get the command line directive that created a process
alias getcmdline='/home/bbrasky/.local/bin/getcmdline.sh'
# 20161222
# make param, pvt and pub ec files
alias makeec='/home/bbrasky/.local/bin/makeec.sh'
# 20161222
# input integer, output unicode
function int2unicode
{
R=0
if [[ !# == 0 ]]; then R=$RANDOM; else R=$1; fi
R_HEX=$(printf %x $R)
R_UNICODE=$(printf "\u$RX") && echo -n "$R_HEX" >> "$1"
echo "$R --> $R_HEX --> $R_UNICODE"
}
# 20170109
# See what the terminal colors mean
# http://askubuntu.com/a/17300
function termcolors
{
eval $(echo "no:global default;fi:normal file;di:directory;ln:symbolic link;pi:named pipe;so:socket;do:door;bd:block device;cd:character device;or:orphan symlink;mi:missing file;su:set uid;sg:set gid;tw:sticky other writable;ow:other writable;st:sticky;ex:executable;"|sed -e 's/:/="/g; s/\;/"\n/g')
{
OLDIFS=$IFS
IFS=:
for i in $LS_COLORS
do
echo -e "\e[${i#*=}m$( x=${i%=*}; [ "${!x}" ] && echo "${!x}" || echo "$x" )\e[m"
done
IFS=$OLDIFS
}
}
# Tue Sep 6 20:17:53 BST 2016
# searches systemd locations for some keyword
function search_modules { /home/bbrasky/.local/bin/search_modules.sh $1 ;}
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -ahlF --group-directories-first --dereference-command-line-symlink-to-dir'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
set casesensitiveset constantshowset cutset fill -8set historylog
set morespace
set multibufferset noconvert
set nonewlinesset nowrap
set positionlogset preserveset punct "!.?"set regexp
set tabsize 4include "/usr/share/nano/*.nanorc"
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# include cabal stuff
export PATH="$HOME/.stack/programs/x86_64-linux/ghc-8.0.1/bin:$HOME/.cabal/bin:$PATH"
# set PATH so it includes user's private bin directories
export PATH="$HOME/.local/bin:$PATH"
export CXXFLAGS="$CXXFLAGS -fPIC"
export CFLAGS="$CFLAGS -fPIC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment