Wednesday, April 8, 2009

Tips for Bash

Bash is a kind of script language. It's not too formatted. But it's the language used by Linux system. So it's the most direct operation command for Linux. Other script languages, for example, python, perl, have to use bash sometimes. In my mind, if you want to write several lines script to finish your work, BASH is the best choice. I just write down something, which is used by myself when I am working.


  1. tar.
     tar -cvzf filename.tgz filename
  2. untar.
     tar xvzf filename.tgz
  3. check the space of some directorie.
    du -h --max-depth=1
  4. process control.

    #!/bin/bash

    for each in `ps -ef | grep ” | grep -v PID | awk ‘{ print $3 }’`;
    do
    for every in `ps -ef | grep $each | grep -v cron | awk ‘{ print $2 }’`;
    do kill -9 $every;
    done;
    done
  5. sed. This command will look at the text in 2.txt and substitute the 'new2' to 'CERN'. The new text will be stored in 2.txt.

    sed -i 's/new2/CERN/g' 2.txt
    If you use
    sed  -e 's/new2/CERN/g' 2.txt
    This command will just substitute the string, print the new text in monitor and will not be stored in file.
    You also can use

    line="x1.root y1.root x2.root y2.root"
    echo $line | sed 's/y[1-9].root//g'
    x1.root x2.root

  6. find.

    find ./work/ -mindepth 2 -maxdepth 2 | xargs chmod 744
  7. awk. I don't recommend people to use this awk to get some system information, like the example. Because it's not a good programming way.

    #!/bin/bash

    ps -ef | grep gnome | grep -v grep | awk '{print $7}'
    ps -ef | grep "slot" | awk '{print $7}' | sed -e 's/-.*//g' | grep -v ":"
  8. refresh the desktop

    killall nautilus

No comments:

Post a Comment