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? :(


the subshell is your friend

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

Today I tried to find a way to track the time of a shell-command and to log that runtime into a file. I was already aware of the time command which is a bash build-in. Due to that it passes its output directly to the user without using either stdout or stderr and therefore there’s no “easy” way to redirect the output directly into a file.

But bash als provides subshells and in this case that’s how you can use the output :P (which is passed on stderr in this case).

(time sleep 5) 2> time.log


… much better way to use your shell

Posted: April 15th, 2009 | Author: | Tags: | Comments Off YouTube Preview Image


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