Saturday, December 1, 2012

Helpful bash aliases

I thought I'd share a few of the most helpful bash functions and aliases I've collected throughout the years I've been using Linux and cygwin (sometimes Windows is unavoidable!)

I should probably explain that these should be entered into a file that will typically be read and executed on login. Something like .bashrc or .bash_aliases is popular. You may have to configure .bash_aliases to be called within another dot file. (.bashrc or .bash_profile).
Feel free to check my files on github to see how I do it (though it's definitely not 'correct' :D )

A lot of these were copied straight from other peoples' configs from forums or github. Some starts as a copied function and was then edited to fit my particular usage. Some are my own.

First up, extract

# ex - archive extractor
# usage: ex 
ex ()
{
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xjf $1   ;;
      *.tar.gz)    tar xzf $1   ;;
      *.bz2)       bunzip2 $1   ;;
      *.rar)       rar x $1     ;;
      *.gz)        gunzip $1    ;;
      *.tar)       tar xf $1    ;;
      *.tbz2)      tar xjf $1   ;;
      *.tgz)       tar xzf $1   ;;
      *.zip)       unzip $1     ;;
      *.Z)         uncompress $1;;
      *.7z)        7z x $1      ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

You can see it merely checks the file extension and calls the appropriate binary with the appropriate switches. Not so complicated.  One thing I would like to do in the future is hook into the bash completion scripts so a switch like this would not even be necessary. Next up we go sort of inverse from extract with roll()

# roll - archive wrapper
# usage: roll  ./foo ./bar
roll ()
{
  FILE=$1
  case $FILE in
    *.tar.bz2) shift && tar cjf $FILE $* ;;
    *.tar.gz) shift && tar czf $FILE $* ;;
    *.tgz) shift && tar czf $FILE $* ;;
    *.zip) shift && zip $FILE $* ;;
    *.rar) shift && rar $FILE $* ;;
  esac
}

As you can see it's fairly simplistic just like ex, though adding the element of shift does raise the complexity a bit. Essentially we just need to detect the type of archive based on the chosen file name and return the binary and switches to create it. Again, I think I would rather look into the bash completion code to find them, but for now this works great.

ducks is a simple alias that simplifies finding the largest subdirectories in a given argument. It duplicates aliases and functions I've written previously, multiple times, but never have gotten it like I want. Luckily I happened across another persons bash_alias with this defined and it's been better than anything I've done with this idea so far. I'm not giving up, though!



alias ducks='du -cksh * |sort -rn |head -11




I'll summarize the man page since it's just invoking a few switches.



-c says to show a complete size for all of the listings

-k says to count with a block-size of 1k

-s summarizes each entry. You could think of it as du -s * the same as du --max-depth=1 .

-h makes the results human readable. Basically you'll see 303k instead of 303012


What I want to know; why isn't it called ducksh? :) I guess duchkmd=1 doesn't have the same ring to it.

I've definitely noticed some issues with this approach. Things aren't quite sorted like you'd figure. It also cuts off the list if it gets too long. It might be nice to optionally pass that limit in yourself.


Well, that's it for now. Check back for some more bash configuration fun as well as a few ruby scripts that might make CLI maintenance on your linux machines a little less painful!

* From what I can tell some of this code has been passed around so many times that their original authors are impossible to find. If you notice your own code, or obviously modified from your code here, feel free to let me know and I'll give you credit and a link back!

No comments:

Post a Comment