bash aliases

I usually spend more time on terminal consoles than usual graphical user interfaces… one thing that has recently caught my attention is the better use of aliases, to avoid the repetitive typing of commands you use frequently.

Simple things as:
  • rm command: I incredibly always use -rfv (force, recursive and verbose)
  • ls command: most of the time, I actually need -lh (list and human-readable)
So the bash_aliases file would contain something like:
export LS_OPTIONS='--color=auto'
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -lh'
alias l='ls $LS_OPTIONS -lAh'
alias rm='rm -irfv'
alias cp='cp -irfv'
alias mv='mv -ifv'
# One-letter handiness
alias P='python'
alias M='make'
Another thing that bothers me sometimes is when I need to kill some program because it is not behaving as it should… you usually need to find the process id and then launch a kill command… A handy way of doing that all-in-one is:


function K() {
    if [ "$kw" != "" ]; then
        IFS=$'\x0a' # line separator
        echo "Searching for [$kw] processes..."
        for x in `ps -ef | grep -v grep | grep "$kw"`; do 
            pid=`echo $x | awk 'BEGIN { FS = " " }; { print $2 }'`
            app=`echo $x | awk 'BEGIN { FS = " " }; { $1=$2=$3=$4=$5=$6=$7=""; print $0 }' | sed 's/^ *//g'`
            if [ `ps -ef | grep -v grep | grep "$app" | wc -l` -eq 0 ]; then
                continue # recheck in case it is a child process from a previous kill
            trunc_app=`echo $app | cut -c1-40`
            if [ ! "$app" == "$trunc_app" ]; then
            echo -ne "$(tput setaf 3)Found [$app] ($(tput bold)$pid)$(tput sgr0)\nAre you sure you want to kill? [y/n] (default no)"
            while read -r -n 1 -s answer; do
                if [[ $answer = [YyNn] ]]; then
                    if [[ $answer = [Yy] ]]; then
                        echo " -> $(tput setaf 1)killing...$(tput sgr0)"; 
                        while [ `ps -p $pid --no-headers | wc -l` -eq 1 ]; do
                            count=$(expr $count + 1)
                            if [ $count -gt 10 ]; then
                                echo -n "$(tput setaf 4) taking too long! Issuing a SIGKILL...$(tput sgr0)"
                                kill -9 $pid
                                kill $pid
                            echo -n "$(tput setaf 4).$(tput sgr0)"
                            sleep 1
                    echo " -> $(tput setaf 5)skipping...$(tput sgr0)"
        echo "Done."
        unset IFS
To use it, just hit K and a string that will be searched:

K shortcut for kill

Main features of this killer:
  • given a string, it displays a confirmation of all processes matching (by grep) the criteria
  • any other key that is not [Yy] will be considered as NO (this is good, safe in case you don’t know what to kill for sure)
  • if the process is hanging, it will fire a SIGKILL signal after 10 SIGTERM attempts (one per second) – so if you are not sure you want this, just hit Ctrl+C to stop the script.

2 thoughts on “bash aliases

  1. Bruno Braga Post author

    Updated this script to run better:
    – colors
    – force SIGKILL after failed SIGTERM attempts
    – better process name output
    – remove old child processes from the confirmation list

    Looking good now!


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>