rename images by creation date with exiftool

Today I wanted to rename 270 images so that they contain the date and time of creation in their filename. I came up with a simple command using exiftool:

cd folder/with/my/images
exiftool '-filename<IMG_${DateTimeOriginal}.%e' -d %Y%m%d-%H%M%S *

That’s it! I also had two images taken at the same second, so they would have been renamed to the same filename. Instead of overwriting the first one, exiftool recognized this and told me about this problem, so that I could fix it manually.

a world without java – a javapocalypse

Some fun for in between :)

google font url for http and https

I just found something really great: For one of my wordpress themes I use some google fonts. Therefore I just added the @import lines provided by Google Fonts:

@import url(http://fonts.googleapis.com/css?family=Medula+One);

But there is one problem with this url: When opening my web page with https, browsers refused to load the font, because it is unsecure content within a secure page. So I googled around and found an incredible easy solution:

@import url(//fonts.googleapis.com/css?family=Medula+One);

By just omitting the http: or https: part of the url, the browser uses the same protocol as for the website. Now the fonts are loaded, no matter if the website is opened with http or https.

find longest path in filesystem

Today I had to find out the longest path within a directory. And here is how I found it:

#!/bin/bash

CNT=0

for D in `find * .`
do
	CNT2=`echo "$D" | wc -m`
	if [ $CNT2 -gt $CNT ]; then
		CNT=$CNT2
		echo "$CNT: $D"
	fi
done

It’s a simple bash script that searches for all files in the current directory and counts the length of each path. When it finds a path that is longer than the longest path it found before, it prints the length of the path and the path itself. (I think it’s easier to understand what the script does if you read the script itself and not my yackety-yack ;-) ).

setting command prompt for all users in Ubuntu

Changing the command prompt on Linux is easy: just change the value of PS1:

PS1='\[\033[1;33m\][\u@\h] \t \w\$\[\033[0m\] '

But where to put this line if you want to configure it system-wide for all users? Yeah, that’s the big question. On the Ubuntu machine I currently work on I found the following files:

In the user’s .bashrc I found the following part.

...
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

These lines overwrite the default settings, so I had to remove them. (This part has to be done for all users as well as for the skeleton for new users under /etc/skel/)

The second part is to set the new value for PS1 in /etc/bash.bashrc:

PS1='\[\033[1;33m\][\u@\h] \t \w\$\[\033[0m\] '

That’s it. Feel free to leave a comment, if you have a better idea to accomplish that.

move /var/* to another partition

The /var-directory of a Linux system contains variable data such as system logs and application data. Sometimes, especially when the folder gets too big, so the root partition gets out of space, I want to move this folder to another partition, but there is one thing you should know:

The /var-directory must be available at system bootup. So, you cannot just replace it with a symlink to another partition.

So, as a compromise I just move some subfolders of the /var-directory to the other disk and symlink them. Maybe you could also mount --bind them or even mount the whole /var-directory, but I don’t have any experience with that.

Nevertheless, here I point out, what you have to do if you want to move/change/whatever some parts of your /var-directory. Since many applications access this directory, you should only do this when your machine is runnig at runlevel 1:

init 1
mv /var/folder /any-other-partition/
ln -s /any-other-partition/folder /var/folder
reboot

I prefer the reboot command at the end because I want to make sure, that the system reboots and starts all services as expected (so, that my change does not break the system with the next reboot). I think calling init 2 should work too.

tunnel remote port over ssh

This is a command I use quite often. It binds a port of a host in a remote network to a port of your local machine. You can then use your local machine to connect to the remote host which can be useful if you only have access to a SSH server in the remote network (e.g. due to some firewall settings).

ssh tunnel between two networksAssume that you need to access port 80 on 10.10.10.2 in a remote network, but because of some firewall rules you are not allowed to. In the remote network, there is another machine (10.10.10.1) you can access via SSH? Great! Then you are able to tunnel port 80 of 10.10.10.2 through this SSH connection. Therefore execute the following on 192.168.0.1:

ssh -N -g -L 80:10.10.10.2:80 root@10.10.10.1

Now you can connect to 192.168.0.1:80 and you will see what you get when connecting to 10.10.10.2. You can do this from 192.168.0.1 itself or via another machine in your network (since you passed -g to the ssh command).

Tip: combine this with some entries in your /etc/hosts file and you can access the remote machine with it’s DNS name.

adding a gitolite-repo to redmine

Just found a good tutorial how to connect a gitolite-managed repository with a redmine project. Yes, it’s bad that we need a tutorial for doing simple stuff like this. Here it is: http://ambitz.com/2011/03/07/adding-a-gitolite-controlled-repository-to-redmine/

BTW: here is another good post about using git for deploying websites: http://toroid.org/ams/git-website-howto

update mac address in ubuntu

When you replace your system’s network card (or, in my case, copy a virtual machine) the physical mac address also changes. In Ubuntu all mac adresses of network interfaces are coupled with a name like eth0, eth1, wlan0, etc. So, if you replace the card, not only the mac address changes, the new network interface will also have a new name. If you used the old name to configure the adapter (for example in /etc/network/interfaces) this configuration will be ignored from now, because the configured adapter is no more installed.

You could now update all configurations e.g. from eth0 to eth1, but since I know, this system will never ever see the old network card again, I just edit the list of well known network interfaces and replace the old mac address with the new one. The appropriate file (in Ubuntu) is /etc/udev/rules.d/70-persistent-net.rules and this is how it may look like:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:11:22:33:44:55", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="66:77:88:99:AA:BB", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

And this is what it looks like after the update:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="66:77:88:99:AA:BB", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

After that you should restart the network service with /etc/init.d/networking restart (or just reboot your machine) and that’s it!

format disk with parted (shell)

WARNING! WITH THE FOLLOWING COMMANDS YOU CAN CRASH YOUR COMPUTER! BE CAREFUL WHEN USING THESE COMMANDS! THEY MAY ERASE ALL OF YOUR DATA!

Let’s say you are on a linux machine with no graphical environment and you have to format a new disk. So, you cannot use a tool like gparted. Well, that’s not a problem!

If you don’t know what you are doing, don’t do it! DON’T DO IT!

Note: I assume, that /dev/sdx is a new disk with no data. This guide is not for re-formatting a disk with existing partitions.

Run parted (you need to be root):

parted

Create the partition table and a partition

(parted) select /dev/sdx
(parted) mktable msdos
(parted) mkpart primary 0% 100%
(parted) quit

This created a partition /dev/sdx1 on /dev/sdx. Now, that we have the partition, we can format it. I prefer ext4, you may want to use another format.

mkfs.ext4 /dev/sdx1

Update: Just saw, that earlier parted versions do not support mktable, so I had to use fdisk for paritioning my drive.

That’s it!

« Previous PageNext Page »