Tidbits

Occasionally I discover neat little tricks, and I blog about them. It makes more sense to consolidate them onto a single page, and here it is...

ssh

If an SSH session hangs, try ~ . and you can sometimes disconnect it. Likewise, ~ CTRL+Z can send it into the background.

mysql

'pager less' at the mysql prompt will cause the mysql command line to page the results of all mysql queries.

deleting lines with sed

I've often used grep -v to remove a line from a file. However, that streams the content of the file out, rather than changing the file in-place.

You can do an in-place removal of a line with sed:

sed -i "/YOURSTRING/d" yourfile.txt

will remove any lines containing YOURSTRING from file yourfile.txt.

Epoch dates in bash

Want to find out what date a unix epoch date refers to:

date --date=@1229593587

Want to go the other way? Find out the unix epoch time now? That's simple:

date +%s

Bash and environment variable line endings

If we do:

CONTENT=$(man ls)

we end up with $CONTENT containing some (arbitrary in this case) multiline text.

Let's say we want to get the number of lines. We might do:

echo $CONTENT | wc -l

However, from this we get 1.

Hmm. Okay, so we try:

echo "$CONTENT" | wc -l

Now we get 214. Hmm. You gotta love bash for its little ideosyncracies.