>> Noel O'Boyle

Shell scripts

The shell is like the command-prompt in Windows. Sort of. My favourite shell is bash.

Killing background jobs

If you use CTRL+Z, 'bg' or '&' to pause or send jobs to background, you may want to kill these jobs at some point. Typing 'jobs' will give you the current list of stopped or running jobs. The number in square brackets (for example, "[1]") is the job number, and this particular job can be killed using kill %1.

Zipping several folders

I wanted to make separate zip files for all of the folders in a directory.

for x in *; do zip -r ${x}.zip $x; mv ${x}.zip ../tar; done

Splitting a dataset into four subsets

Because of the size of my dataset I wanted to split it in four, so I could run two in parallel on two dual-processor machines. The dataset was contained in 3542 files. Here is my not-entirely-elegant solution:

ls mydir/* > tmp.txt
split --lines=886 tmp.txt mysplit
cd mydir
mkdir ../split1
cp `cat ../mysplita` ../split1

Push and pop directories

Ever want to jump between two or more folders easily without having to type the complete path every time? Or want to quickly open a new shell window in the same folder as your current window? Wondering what the story is with all the questions?

The following scripts allow you to perform these tasks easily. Simply type

push here

in any folder, and typing

pop here

will change directory back to that folder. You may prefer to use a name other than 'here'. The folder names are stored in a simple text file (see the scripts for details). Place 'push' and 'mypop' into any folder on the path, and add the following to your ~/.bashrc:

function pop {
   cd `mypop $1`

Here's mypop.


# Pops the directory associated with 'tag' and makes it the current
# directory.
# requires the following in your .bashrc:
# alias pop 'cd `mypop \!^`'
# Then you can use: pop mytag
# or echo `mypop mytag`, or cat `mypop mytag`/readme.txt


if [ -z "$1" ]; then
    echo "  Syntax: pop n (where n is a tag)"
    exit 1

if [ -f $mylist ]; then
    grep "^$1" $mylist > $mylist.tmp
    if [ ! -s $mylist.tmp ]; then
        echo "No such tag"
        awk '{print $2}' $mylist.tmp
    echo "  There isn't anything to pop!"
    exit 1

And push.


# Pushes the current directory into a 'memory slot' indexed by a tag
# See also 'pop'


if [ -z "$1" ]; then
    echo "  Syntax: push n (where n is a tag)"
    exit 1

if [ -f $mylist ]; then
    grep -v "^$1" $mylist > $mylist.tmp

echo "$1 `pwd`" >> $mylist.tmp
sort $mylist.tmp > $mylist
rm $mylist.tmp

Check for other users

If you want to see whether any other users are logged on (and what they are doing), and you dislike the lack of information that 'users' gives, try this:

 ps -Af | egrep `ls /home | awk 'BEGIN {ORS="|"} {print $0}' | awk '{print substr($0,0,length($0)-1)}'` | grep -v `whoami`

It works by taking the usernames from the list of folders in /home, and grepping against the output of 'ps'.

Get rid of beep

Put this in your startup script to prevent your computer beeping every time you autocomplete, or hit backspace too many times:

xset -b