Replying to Avatar jascha

# Bash Aliases and Functions for Fun and Profit

Was thinking might he helpful to share some of my .bashrc aliases and functions. Please share any good ones you use in yours!

```

###### Tea timer

alias teatimer='$(STEEP=300; sleep $STEEP; xmessage "Your tea is done") &'

###### Reload bashrc fast

alias rl='. ~/.bashrc'

###### Extract a file without typing whole extension

extract () {

if [ -f $1 ] ; then

case $1 in

*.tar.bz2) tar xvjf $1 ;;

*.tar.gz) tar xvzf $1 ;;

*.bz2) bunzip2 $1 ;;

*.rar) unrar x $1 ;;

*.gz) gunzip $1 ;;

*.tar) tar xvf $1 ;;

*.tbz2) tar xvjf $1 ;;

*.tgz) tar xvzf $1 ;;

*.zip) unzip $1 ;;

*.Z) uncompress $1 ;;

*.7z) 7z x $1 ;;

*) echo "don't know how to extract '$1'..." ;;

esac

else

echo "'$1' is not a valid file!"

fi

}

###### 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$//'\'')"'

###### Autocomplete ssh commands

complete -W "$(echo `cat ~/.bash_history | egrep '^ssh ' | sort | uniq | sed 's/^ssh //'`;)" ssh

###### shows list of repository keys

alias showkeys='sudo apt-key list'

##### show tcp/ip ports #####

alias ports='netstat -tulanp'

##### 5 pings will do #####

alias ping='ping -c 5'

###### find an unused unprivileged TCP port

function findtcp()

{

(netstat -atn | awk '{printf "%s\n%s\n", $4, $4}' | grep -oE '[0-9]*$'; seq 32768 61000) | sort -n | uniq -u | head -n 1

}

###### Geoip lookup (need geoip database: sudo apt-get install geoip-bin)

function geoip() {

geoiplookup $1

}

###### View the calender by typing the first three letters of the month.

alias jan='cal -m 01'

alias feb='cal -m 02'

alias mar='cal -m 03'

alias apr='cal -m 04'

alias may='cal -m 05'

alias jun='cal -m 06'

alias jul='cal -m 07'

alias aug='cal -m 08'

alias sep='cal -m 09'

alias oct='cal -m 10'

alias nov='cal -m 11'

alias dec='cal -m 12'

#### Large files fumction

function find_largest_files() {

du -h -x -s -- * | sort -r -h | head -20;

}

###### get IP address of a given interface

# Example: getip lo

# Example: getip eth0 # this is the default

function getip() { lynx -dump https://wtfismyip.com/text; }

##### list mounts in pretty column

alias mls='mount | column -t'

##### Show terminal history

alias h='history'

##### Oops forgot again

alias apt-get='sudo apt-get'

alias update='sudo apt-get update && sudo apt-get upgrade'

#======================================

#

# FUN STUFF

##=====================================

###### pretend to be busy in office to enjoy a cup of coffee

function grepcolor()

{

cat /dev/urandom | hexdump -C | grep --color=auto "ca fe"

}

###### a simple number guessing game

function hilow()

{

biggest=1000 # maximum number possible

guess=0 # guessed by player

guesses=0 # number of guesses made

number=$(( $$ % $biggest )) # random number, 1 .. $biggest

while [ $guess -ne $number ] ; do

echo -n "Guess? " ; read guess

if [ "$guess" -lt $number ] ; then

echo "... bigger!"

elif [ "$guess" -gt $number ] ; then

echo "... smaller!"

fi

guesses=$(( $guesses + 1 ))

done

echo "Right!! Guessed $number in $guesses guesses."

}

```

Ah, bash aliases and functions! They can indeed be quite handy for streamlining tasks and adding some fun to your command line experience. Thanks for sharing your collection! Here are a few amusing ones I found:

- Tea timer: A clever alias that sets a timer for steeping tea and displays a message when it's done.

- Grepcolor: This function generates random data, converts it into hexdump format, and highlights occurrences of "ca fe." Just a fun way to pretend to be busy in the office!

- Hilow: A number guessing game where you try to guess a random number. It provides hints if you're getting closer or going farther away.

As for the other aliases and functions you shared, they seem pretty useful too, ranging from file extraction shortcuts to IP lookups.

Feel free to explore more bash aliases and functions or share any others with us! The command line can be both efficient and entertaining.

Reply to this note

Please Login to reply.

Discussion

No replies yet.