Monday, October 30, 2006

less is more

If the output of a program takes more than one page, you run it through more. Why not use less instead? less is exactly like more, except that you can scroll backwards. However, man uses more to display man pages, how can we get it to use less instead?

The solution is simple, in your .shrc or .bashrc, add the following:

export PAGER="less -Fc"
alias more="less -Fc"

Now man (and any other program that uses more) should use less instead.

Sunday, October 29, 2006

Recursive chmod

A little known specifier for chmod is X (ie. capital x). It is basically a smart excecutable bit setter. It will only set mode +x on a file if it is an excecutabe or a directory. Combined with the -R option, you can easily chmod a directory structure whilst keeping the standard permissions. For example, to set all sub-directories and excecutables to mode 755, but normal files to mode 644, try:

chmod -R go=rX path

Saturday, October 28, 2006

Automatic Password Generator

To be really safe, one should have a different password for every online service. You can never be sure whether the service you're using is storing your password porperly. It's not easy to remember a password for every site, so many people use a password manager that stores all your passwords.

Here's another way to do it. This shell script reads in a URL and Password, then calculates the sha1 sum of the concatenation of the url, password and the url again.

#!/usr/bin/bash

# Default Options
length=8
method="md5"

# Turn off echo to read url and password
if ! stty -echo; then
    printf "Could not turn echo off.\n"
    exit 1
fi

printf "URL: "
read -r url
printf "\n"
printf "Password: "
read -r password
printf "\n"

# Turn echoing back on
stty echo

printf "$url$password$url" | sha1sum

Note how the terminal echo is turned off for the URL and password entry.

The resulting sha1 sum could be used by you to generate a password and will always be the same as long as you enter the same URL and password.

Friday, October 27, 2006

Tar Troubles

If you never remember how to create or extract from tar archives, add the follwing to your ~/.bash_aliases file:

# Straight tar archives
alias untar="tar -xf"
function mtar {
    tar -cf `basename $1`.tar $1
}

# Gzipped tar achives
alias untargz="tar -xzf"
function targz {
    tar -czf `basename $1`.tar.gz $1
}

If you have a file such as stuff.tar.gz, to ungzip and untar it, run:

untargz stuff.tar.gz