A Couple of Useful Snippets from My Shell Config

I’ve been slowly and incrementally working on cleaning up my shell config. I’ve been procrastinating over my persist and forget system for quite a while now. I have it up and working here just fine, but I built it and its extra git repository in place. I need to automate the installation and configuration process and I would also like to make sure it isn’t likely to eat anyone’s file before I release it.

In the mean-time, though, I have been digging through my configuration and I’ve found a few gems that I would like to list here. I use these under zsh but I am pretty sure they will work fine under bash as well.

Quickly share files on the public Internet with Python and SSH

I see ‘python -m SimpleHTTPServer’ one liner all the time. By itself this one is pretty useless for me. I almost never need to share something with anyone on my local network. I use this function instead:

My webshare function
1
2
3
4
5
6
7
8
9
10
11
12
webshare () {
  local SSHHOST=patshead.com
  local SSHPORT=9999

  python -m SimpleHTTPServer &
  local PID=$!

  echo http://$SSHHOST:$SSHPORT | xclip
  echo Press enter to stop sharing, http://$SSHHOST:$SSHPORT copied to primary selection
  /usr/bin/ssh -R $SSHPORT:127.0.0.1:8000 $SSHHOST 'read'
  kill $PID
}

This does require you to have a server out on the Internet that you can ssh into. For my purposes, this fills a gap somewhere between a pastebin and something like Dropbox. I use it when I have a handful of files I want someone to take a look at.

A smarter tail command

I lifted this one from commandlinefu.com:

My tail function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
tail() {
  local thbin="/usr/bin/tail"
  local fc lpf thbin

  if [ "${1:0:1}" != "-" ]; then
    fc=$(($#==0?1:$#))
    lpf="$((($LINES - 3 - 2 * $fc) / $fc))"
    lpf="$(($lpf<1?2:$lpf))"

    [ $fc -eq 1 ] && $thbin -n $lpf "$@" | /usr/bin/fold -w $COLUMNS | $thbin -n
  else
    $thbin "$@"
  fi
}

I really like this one. This tail wrapper checks the size of the terminal window and makes sure it shows you just a bit less than a screen full of output. It even takes long lines into account.

Automatically managing a “scratch” directory

This function was heavily inspired by an entry on Marcin Kulik’s blog:

`` bash My function to create a new scratch directory function ns { local cur_dir="$HOME/tmp/scratch/current" local new_dir="$HOME/tmp/scratch/date +‘%s’`”

mv $cur_dir $new_dir mkdir $cur_dir cd ~/scratch echo “New scratch dir ready” }

I’m always creating new scratch directories to work in, so I really liked his idea of creating a function to help manage the process.

I didn’t change his implementation too much. The only real difference is that my symlink never changes. I’m just moving the old, real scratch directory out of the way and putting a new one back in its place.