Tag Archives: bash

Bash, aliases, and command line arguements

I was recently doing some work with gzip’d files. It got kind of repetitive to keep typing

gunzip file
head file
gzip file

I know there is a zcat but I don’t want to have to type

zcat file | head

everytime.

You can create aliases in your .bashrc file. These are quick shortcut commands.
The following does not work

alias zhead='zcat $1 | head' #does NOT work

In order for this to work you have to create a function in your .bashrc and call it from the alias.

function zh(){
    zcat $1 | head
}
 
alias zhead='zh'

10 Quick Tips

I would not agree that these are usage habits, or even unix specific. But they are nifty.

  • Make directory trees in a single swipe.
  • Change the path; do not move the archive.
  • Combine your commands with control operators.
  • Quote variables with caution.
  • Use escape sequences to manage long input.
  • Group your commands together in a list.
  • Use xargs outside of find .
  • Know when grep should do the counting — and when it should step aside.
  • Match certain fields in output, not just lines.
  • Stop piping cats.

http://www.ibm.com/developerworks/aix/library/au-badunixhabits.html?ca=lnxw01GoodUnixHabits

Which shell do you use?

We had a discussion at work today about which shell everyone uses.Everyone has their preferences. There are quiet a few to choose from: sh, bash, zsh, csh, tcsh, and ksh to name just a few. I use BASH and I don’t have a reason better than it’s the default and I have gotten used to it. One feature I love is

sudo !! #this will repeat your previous command as root

Which do you use and why?

I can never remember…

There are some Linux commands that I can never for the life of me remember. Maybe documenting them here will give me a place to come back to when I find myself at a loss.

Deleting files using find:

find . -mtime +3 -exec rm -rf {} \;
#dl all files in cur dir that have not been mod in 3 days

Test for a null string in bash?

if [ -z $VAR ]; then #returns true if the string is empty
 
if [ -n $VAR]; then #returns true if the string is NOT empty
 
if [ $VAR ]; then #returns true is variable exists