bash: small improvement for the “cd” builtin

Posted: June 18th, 2009 | Author: | Tags: , | Comments Off

If you use the shell and walk around in directories wouldn’t it be cool to have “cd …” to move 2 levels up, “cd ….” to move 3 levels up …? I’m not sure if there’s an easier way to resolve it but the following lines work pretty nice so far and they just made it into my default .bashrc :P

cd() {
	if [[ "$1" =~ ^\.\.\.+$  ]]; then
		cd `echo "$1" | sed 's/\./..\//g' | sed 's/^\.\.\///g'`
	else
		builtin cd "$1"
	fi
}

For different older shell/bash versions you might need quotes around the regex.

Btw O’Reilly’s “Learning the Bash” is available within Google Books

Edit: another very important bookmark for bashscripting is the Advanced Bash-Scripting Guide


shell command: translate

Posted: April 28th, 2009 | Author: | Tags: , | Comments Off

“Translate, squeeze, and/or delete characters from standard input,
writing to standard output.”

… that’s the manpage description of the shell-command I just found (with the help of a good friend) … and besides sed this is a very handy way to do simple string-operations on stdin.

He used it to replace (old) Mac newline characters with Unix newline charaters within files (as seen on wikipedia):
tr '\r' '\n' < old.file > new.file

Btw I still think that it’s stupid that users still have all the hazzle with newline charaters – why can’t the OS-developers just recognize all types of newlines? :(


find the newest file within a folder….

Posted: April 14th, 2009 | Author: | Tags: , | Comments Off

These are two neat little commands to find the newest and oldest file within a folder and it’s subfolder:
find . -type f -printf "%T@ %p \n" | sort -n -k 1,1 | awk '{print $2}' | tail -n 1
find . -type f -printf "%T@ %p \n" | sort -n -k 1,1 | awk '{print $2}' | head -n 1

If you’d like to know how old/young these files are try these two:
find . -type f -printf "%T@ %C+ %p \n" | sort -n -k 1,1 | tail -n 1
find . -type f -printf "%T@ %C+ %p \n" | sort -n -k 1,1 | head -n 1

I hope that’s a good starter for the “blog” :P


track time for a subroutine within a shell-script

Posted: July 10th, 2008 | Author: | Tags: | Comments Off

The following code-snippet is a shell-script which does the following:

  1. Track the time for a block of shell commands
  2. Check if the time was less than x seconds (the example uses 10 seconds)
  3. If the block run through too fast the script waits/sleeps a few seconds
  4. Run all this within a loop so that the block of shell-commands is executed periodically

I used the script combined with a PHP script which processes a queue. The PHP script processes 1000 elements from the queue and takes about 30 seconds for that. Since just having a cronjob per minute would be not efficient enought I used this script.
The waiting-block is necessary because now and then the queue is empty … but I think there are lot’s of situations where a script like this can be usefull:

#!/bin/bash
while [ 1 -ge 0 ]; do
time_begin=`date +%s`


###BLOCK 2 TRACK - BEGIN
number=$RANDOM
let "number %= 20"
sleep $number
###BLOCK 2 TRACK - END

time_end=`date +%s`
total=$((time_end-time_begin))

if [[ $total -ge 10 ]]; then
echo "time taken was: $number : $total"
else
echo "time take was too less  $number : $total"
sleep 10
fi
done