Tech stuff and info dump

grep: how to use grep to search history (linux)

May 31st, 2010

I often remember only part of something I did before and want to remember how I’ve done something. To easily search through the history, the glorious grep can be invoked like this:

history | grep phrase_to_search_for

If the phrase to search for involves spaces or special characters, then quotes can be used around the phrase:

history | grep ‘phrase to search for’

To search for something with quotes, you can surround your phrase-to-search-for with the ‘other’ kind of quote.

For example,

history | grep ‘phrase to “search” for’

will match

phrase to “search” for

and

history | grep “phrase to ‘search’ for”

will match

phrase to ‘search’ for


Filed under: Command line,linux
May 31st, 2010 11:43:58

linux: find out what you’ve recently typed at the command line

May 31st, 2010

If you open a terminal window (in Ubuntu 10.04, go to Applications->Accessories->Terminal) and type

history

this will print out a list of things that have been recently typed at the command line.

If you’re curious, the history is stored in a file called .bash_history which you can view as a file. In Ubuntu, for example, you can open it with gedit by opening a terminal window and typing

gedit .bash_history

at the command line.

Note: if you have two (or more) terminal windows open and are typing commands in all of them, the history of one terminal window won’t know about what you’ve typed in the other window. If you close, say terminal window A, leave terminal window B open. and then open terminal window C, typing history in terminal window C will display what you’ve typed in A but not in B.

Examples of using grep to search through the history are here.


Filed under: Command line,linux
May 31st, 2010 11:43:53

How to list partitions and basic partition information: linux

May 31st, 2010

To find out basic information about partitions, open a terminal window (Applications -> Accessories -> Terminal in Ubuntu 10.04 and others) and type

fdisk -l

at the command line.

(If nothing happens, try typing

sudo fdisk -l

at the command line and entering your password when prompted.)

Something like this should be displayed:

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x66666666

Device Boot Start End Blocks Id System
/dev/sda1 * 1 2613 20988891 c W95 FAT32 (LBA)
/dev/sda2 2614 17532 119830471+ f W95 Ext’d (LBA)
/dev/sda3 17532 19458 15471448 12 Compaq diagnostics
/dev/sda5 13566 17532 31856640 7 HPFS/NTFS
/dev/sda6 2614 13194 84991819+ 83 Linux
/dev/sda7 13195 13565 2980026 82 Linux swap / Solaris

Here, /dev/sda6 is my linux partition.

In Ubuntu 10.04, you can navigate to System -> Administration -> Disk Utility to get a big graphical display of this information (and more).


Filed under: Command line,linux
May 31st, 2010 11:09:40

How to tell how much disk space is left: linux

May 31st, 2010

Open a terminal (Applications -> Accessories -> Terminal in Ubuntu 10.04 and others) and type

df

at the command line.

Something like this will be displayed:

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 83657080 25098664 54308828 32% /
none 504088 296 503792 1% /dev
none 508500 372 508128 1% /dev/shm
none 508500 96 508404 1% /var/run
none 508500 0 508500 0% /var/lock
none 508500 0 508500 0% /lib/init/rw

The number listed on the top line (/dev/sda6) under ‘Available’ is the amount of space left on my linux partition.

For more human-readable numbers, try:

df -h

which will display something like this:

Filesystem Size Used Avail Use% Mounted on
/dev/sda6 80G 24G 52G 32% /
none 493M 296K 492M 1% /dev
none 497M 372K 497M 1% /dev/shm
none 497M 96K 497M 1% /var/run
none 497M 0 497M 0% /var/lock
none 497M 0 497M 0% /lib/init/rw

The df command won’t show space available on unmounted drives.

If you want to see basic information about the partitions on a machine, see this post.


Filed under: Command line,linux
May 31st, 2010 10:58:03

python: generate a list of uppercase or lowercase letters in the alphabet

May 30th, 2010

To create a list of lower-case letters in the alphabet in python:

map(chr, range(97, 123))

To create a list of upper-case letters in the alphabet in python:

map(chr, range(65, 91))

Examples:

>>> map(chr, range(65, 91))
[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’]

>>> a=map(chr, range(97, 123))
>>> a
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]


Filed under: python,script
May 30th, 2010 16:59:09

Ubuntu 10.04: change focus when mouse moves to different window

May 30th, 2010

I like the focus to change when I move my mouse so that I can type in one window whilst having another window on top.

To make this happen in Ubuntu 10.04, go to System -> Preferences -> Windows and click the tickbox next to the statement, “Select windows when the mouse moves over them”.


Filed under: desktop,GNOME,Ubuntu 10.04
May 30th, 2010 16:51:40

Save python history across sessions (linux, Ubuntu)

May 30th, 2010

To get python to save history across sessions you put the file .pystartup in your home directory and you add the line

Open a terminal and go to your home directory.

(In Ubuntu 10.04 and other versions, navigate to Applications -> Accessories -> Terminal to bring up a terminal window. You should already be in your home directory.)

Add the line

export PYTHONSTARTUP=/home/name_of_home_directory/.pystartup

to the .bashrc file, replacing name_of_home_directory with the name of your home directory.

(In Ubuntu 10.04 and other versions, you can open this file by typing

gedit .bashrc

at the command line. This will bring up a simple editor. Just add the line given above to the bottom of the file, save the file and close the gedit window.

The name of your home directory is the string that appears before the @ sign in your command line prompt, assuming you haven’t done anything to change this. For example, your prompt might look like this:

name_of_home_directory@mycomputer:~$

You can also find it by going to Places->Home Folder. The window that pops up will have the text

name_of_home_directory – File Browser

at the top.)

That should do it!

Thanks to archduke.


Filed under: Customisation,python
Tags:
May 30th, 2010 15:00:13

OpenOffice Calc: How to turn off auto-correct for (C) (R) and -> <- arrows

May 26th, 2010

Recently, I wanted to include a string like this:

Latin (C)

in an OpenOffice spreadsheet.

Unfortunately, every time I entered ‘Latin (C)’, the ‘(C)’ would get changed to a copyright symbol. Ctrl-z would erase the entire entry, rather than revert the copyright symbol to the string I typed in the first place.

To turn off this behaviour, I went to ‘Tools -> Autocorrect…’ and selected the ‘Replace’ tab. This gives a big list of all the auto-corrections OpenOffice will make.

I clicked on each item that I didn’t want OpenOffice to ‘fix’ for me and clicked ‘Delete’ to remove the entry.

Alternatively, one can go to ‘Tools -> Autocorrect…’, select the ‘Options’ tab and uncheck the box next to ‘Use replacement table’. Doing this should prevent OpenOffice from making any of the changes listed in the ‘Replace’ list. (It is worth having a look through the other tickboxes here as there were several other things I wanted to untick, like fractionifying fractions and making URLs into links.

This worked on OpenOffice Calc 2.4.1 running on Ubuntu 8.04 (Hardy Heron).


Filed under: Customisation,Fixes,OpenOffice
May 26th, 2010 16:04:55

Ubuntu 10.04: Get rid of the whizzy desktop-switching slide animation and other graphical effects

May 18th, 2010

I prefer a simple desktop environment free from whizzy graphical effects. In particular, I didn’t like the whooshing between desktops. I think of my desktops as discrete spaces and didn’t want them to appear connected in any way. In particular, I didn’t want to be able to move a window halfway off the left-hand side of the screen and then find it on the desktop to the next desktop over. How disorienting!

To disable the default

Navigate to System -> Preferences -> Appearance
Click on the ‘Visual Effects’ tab
Choose the ‘None’ option
Click ‘Close’

This freed me from all the unwanted effects.

(To disable only the desktop whooshing and not other graphical effects, perhaps it would be worth exploring ubuntu-tweak. After Ubuntu Tweak is installed, it can be accessed via Applications -> System Tools. I suspect playing around with “Compiz Settings” under the ‘Desktop’ heading could help. I may try this if I find a nice feature missing now that I’ve turned off visual effects, but for now, I’m happy.)


Filed under: Customisation,Ubuntu 10.04
May 18th, 2010 17:49:43

Ubuntu 10.04 printing fix: getting rid of the light yellow/cream background when there should be a white background

May 18th, 2010

Update 4 June 2010: the latest round of updates took care of the problem. If you’ve not yet updated, try System -> Administration -> Update Manager. Thanks for the heads-up, Reinout van Rees!

Ever since installing 10.04, I’ve found that I occasionally get rogue light yellow/cream-coloured backgrounds when I print images with a white background. This hasn’t happened with my machines running 8.04 or 9.04.

I’ve had this problem printing from OpenOffice and from Firefox; in both cases, I was printing a .jpg with a white background. I verified with xmag that there was no colour in the background that I was trying to print.

There is a Debian bug report and an Ubuntu launchpad bug report with more information and a temporary work around.

Open a terminal window (Applications -> Accessories -> Terminal) and enter the following:

cd /usr/lib/cups/filter/

to move to the /usr/lib/cups/filter directory.

Optional: make a back-up of the file that needs to be edited by typing

sudo cp pstopdf pstopdf.backup

and entering your log-in password if prompted.

Open the file pstopdf in your favourite text editor. This can be done using gedit by typing

sudo gedit pstopdf

and entering your log-in password if prompted.

There should be a line that looks like this:

PS2PDF_OPTIONS=”$PS2PS_OPTIONS -dColorImageFilter=/FlateEncode \
-dPDFSETTINGS=/printer -dUseCIEColor”

Delete -dUseCIEColor so that the line appears like this:

PS2PDF_OPTIONS=”$PS2PS_OPTIONS -dColorImageFilter=/FlateEncode \
-dPDFSETTINGS=/printer”

Save the file.

This worked for me with a Lexmark C544 printer. However, I don’t know what is causing the problem and I don’t know anything about what these options do. Fingers crossed, a ‘proper’ solution will be on its way….

Thank you, vbi, for the work-around!


Filed under: Fixes,printing
May 18th, 2010 13:14:07