Archive for the ‘Tips’ Category

Change exif-time of photographs in batch

After switching time zones I always seem to have the problem of forgetting to update the internal clock of my cameras which screws up the exif-data in the pictures. But I found the program exiv2 (available in Ubuntu’s repositories) that has a very simple syntax for manipulating exif-data.

A simple

exiv2 -a -6  adjust *

turns the clock back 6 hours of all pictures in the current folder. Full documentation at http://linux.die.net/man/1/exiv2

Simple way of keeping track of bibtex-sources

Sample referencesIt’s always been a painful task, trying to fill in all the bibtex-fields when adding a new source to your LaTeX-document. www.CiteULike.org simplifies this by allowing you to search the scientific publications in its database and exporting as bibtex.

That search engine can be a real time saver when trying to find your sources in BibTex-format. They also offer registred members a library of their own which they can export very easily. This has changed the way I work when writing reports. As soon as I want to cite some paper, a quick search for it on CiteULike adds it to my library. The entire library can then be exported as a bibtex-document which is perfect. What is not so good is that they add a lot of extra information.

But, that’s why I have a python-script  which downloads my current library, filters out unwanted fields in the bibtex-file and saves as a file. For each LaTeX-document I write, I always point the bibliography to the same file and voilá.

updateReferences.py (Adjust the three variables in the beginning to your environment and needs).

The next step would be to automatically call this updateReferences-script when bibtex is run…

CiteULike example

Putting Chrome’s cache in the ram

The Chrome web browser is changing the way we browse with it’s speed and focus on content. But, it’s killing my laptop’s battery and hard drive with its constant reading and writing to disk.

Luckily, Linux has a folder mounted in the ram, which means that reads and writes to this folder don’t affect the harddisk (it’s also about two orders of magnitude faster than my notebook disk). The downside is that the data disappears when the power goes out. That folder is most often called /dev/shm/ and I wanted to put Chrome’s cache in this folder, to avoid the constant writing to disk. But I wasn’t prepared to lose all my preferences, extensions, and saved data when the computer was powered down. So I wrote this script that copies the folder from /dev/shm/ mounted in the ram to the disk and this script runs whenever I exit Chrome.

This is how I put Chrome’s cache in the ram

mkdir /dev/shm/.personal_synced # The folder in ram

mkdir ~/.personal_synced # The mirrored folder on disk

I wrote this simple script that fires once every hour (I’m pretty sure it can be written in a simpler way, but I’m certainly a bash-newbie):

#!/bin/bash
# Sync the ramdisk to harddrive

foldername=”.personal_synced”
ramdisk=”/dev/shm/”$foldername
harddisk_copy=”/home/anders/”
backup=”/home/anders/”$foldername”.old”

# Remove the old backup
echo “removing $backup”
rm -fr “$backup”
# Make a copy of the old one
echo “Copies the latest backup $harddisk_copy$foldername”
mv -f “$harddisk_copy$foldername” “$backup”
# Copy from ram
echo “Copies $ramdisk to harddrive, $harddisk_copy”
cp -R “$ramdisk” “$harddisk_copy”

I want to make this sync-script fire when I exit Chrome. At first I tried to make it fire on shutdown but that proved harder than I thought. The following script synchronizes the ram-disk when Chrome exits, and I just replace the symbolic link in /usr/bin/google-chrome with this script.

#!/bin/bash
# Start Chrome with cache in the ram

/opt/google/chrome/google-chrome -user-data-dir=”/dev/shm/.personal_synced/google-chrome” “$@” && /home/anders/Scripts/syncRamdisk

All that is missing is a script that copies the disk-folder to ram when we boot our computer.

# The synced folder
cp -R /home/anders/.personal_synced /dev/shm/
chown -R anders /dev/shm/.personal_synced

# The chrome cache. I don’t want to sync this so it’s placed outside .personal_synced
mkdir /dev/shm/google-chrome
chown anders /dev/shm/google-chrome

I simply added the script to my Startup Applications, under System->Preferences.

Moving Chrome’s cache to our automatically synced folder

By moving the .config/google-chrome-folder to the synced folder, and replacing it with a symlinks, the work will be complete (I also move the cache to the ramdisk, but not to the synced folder. I see no point in spending time and space on keeping the cache):

mv ~/.cache/google-chrome /dev/shm/google-chrome
ln -s /dev/shm/google-chrome ~/.cache/google-chrome

mv ~/.config/google-chrome /dev/shm/.personal_synced/
ln -s /dev/shm/.personal_synced/google-chrome ~/.config/google-chrome

Notes:

/dev/shm/ is readable by anyone, it’s not very secure to put stuff in that folder.

Update: I adjusted the scripts, I had problems with root owning the folders which caused some problems.

Matlab and Ubuntu 10.04

Matlab 2009bWhen I started Matlab 2009b on my new install of Ubuntu Lucid Lynx (10.04) I had the problem that some keys weren’t working. Most notably, the [] didn’t work which is quite bad when writing Matlab-code. Also, the terminal outputs this message:

Warning: X does not support locale en_HK.utf8

The problem is that the locale is set to utf8 (small caps) while Matlab requires UTF-8. By specifying the locale before we start Matlab we solve the problem.

Remove the symbolic link to matlab in /usr/local/bin/matlab with

sudo rm /usr/local/bin/matlab

and create a script in its place:

sudo gedit /usr/local/bin/matlab:

and paste this as it’s content:

#!/bin/bash
# Start Matlab with the right encoding
echo “Starting Matlab with en_HK.UTF-8 coding”
LC_CTYPE=”en_HK.UTF-8″ /usr/local/Matlab2009b/bin/matlab

where you change en_HK to something that seems appropriate and is part of the list produced by locale -a. My Matlab is installed in /usr/local/Matlab2009b/.


Mount image-files in Ubuntu

Ubuntu is really great, it has alot of smart things that makes me more productive.
mountdone.png

But a not so simple thing is mounting an image-file. There are a few ways to do it, but I was surprised that this solution worked as good as it did.

To mount an iso- or img-file I just right-click the file and click Scripts->Mount. And voila!

By using fuseiso and creating scripts which are saved in Nautilus’s script-folder this is easy to achieve. I got the idea of using fuseiso through this guide at Ubuntu Unleashed, but I took the concept further and made it clickable. Nothing advanced but I know how hard it is to find this information when you need it.

Step 1

Fuseiso is the utility that actually will mount your file. It’s in Ubuntu’s official repositories, install fuseiso either by clicking the link or typing

sudo apt-get install fuseiso

Step 2

Create a folder where you want the contents of your iso-file to be found. It is important to remember that you should not use this folder for anything, don’t put files or so here. It is merely for displaying the contents of image-files which are somewhere else.

I chose /media/Isofolder but anything should work. Since /media is owned by the root I must create the folder by preceding mkdir /media/Isofolder with sudo, like this:

sudo mkdir /media/Isofolder

If you create the folder in your home-folder, sudo won’t be necessary. When creating the folder with “sudo”, root will be the owner of the folder. I’ll change owner to myself. It’s more easy that way, but not entirely necessary. I think read-rights is enough…

Anyway, to make myself owner, I’ll use

sudo chown username /media/Isofolder

Once again, this should not be necessary if Isofolder is placed in your own home-folder. But then no other user would be able to see the contents of the image-file.

Step 3

Now it is time to create the scripts that will call mountiso and umount.

First, open Gedit or any other text editor and paste this text:

#!/bin/bash
# Script to mount image-files like img or iso.
# Uses fuseiso
# Before use, create the isofolder:
# “sudo mkdir/media/Isofolder” and “sudo chown username /media/Isofolder”

# first clear the Isofolder
gksu umount /media/Isofolder

# Load the iso!
fuseiso “$*” /media/Isofolder

Everything after # is a comment. Take care when copying the code, the citation signs might not copy properly.

A short explanation of the commands:

  • umount: unmounts the Isofolder (if there already was something mounted). It must be performed as root, so I preceed it with gksu which creates a popup where I can enter my password.
  • After that, fuseiso tries to mount what I clicked on to /media/Isofolder.

Save this file in ~/.gnome2/nautilus-scripts/ and name it Mount. (~ means your homefolder). Scripts in this folder will pop-up in Nautilus when right-clicking.

#!/bin/bash
# See mount for details

gksu umount /media/Isofolder

Save this file in the same folder as Unmount.

mount1.png

Step 4

Make the scripts executable, either by right clicking them and modifying their properties, or by typing

cd ~/.gnome2/nautilus-scripts/
chmod +x Mount
chmod +x Unmount

Chmod +x adds executable (x) rights to the files.

Done

It should now be done, just right click any image-file and choose Scripts->Mount to mount it to /media/Isofolder.

Edit: I added a part about not using the Isofolder and changed a few formulations.
Edit: Confirmed to work in Ubuntu 9.10 Karmic Koala.