Linux command line bootable startup disk creator

Creating a bootable USB drive from the Linux command line

When you need to (re)install a (new) host with Linux, Windows or other kind of operating system provided with an ISO file, burning a CD or DVD is not always an option. Therefore, a USB disk drive may come in handy.

Unfortunately, using a GUI program on Windows or Linux will not always work as you expect. Also, a GUI will not always be available (like on a production server). A solution to this, which always works in my experience, is dd: a command line tool which make you able to convert and copy a file. This should be available by default on the most common Linux distributions. Using ‘dd’, you are actually ‘burning’ the ISO to the USB drive, like you are burning a CD or DVD, except that the USB drive can be rewritten over and over again.

Creating the disk

Download your favorite operating system or anything you want to ‘burn’ to your USB drive (as long as it is a ISO file).

First, check which device you need to write your ISO file to (in most cases it will be /dev/sdb):

cat /proc/partitions

This should show you a list with the sizes of the available devices and partitions. For the next steps, I will use /dev/sdb as the device name to write the ISO file to, replace /dev/sdb for the device name which is applicable for you.

You don’t need to format the USB drive first, since ‘dd’ will do that automatically. Make sure that there are no important files on the USB drive you want to keep, since the drive will be overwritten completely in the next step.

Make sure that the device is unmounted:

sudo umount /dev/sdb

Replace ‘/dev/sdb’ with the device name which is applicable for you.

Second, use ‘dd’ to copy the files from the ISO to the USB drive:

sudo dd if=/home/user/Downloads/CentOS-6.5-x86_64-minimal.iso of=/dev/sdb

This may take some time, depending on the size of your ISO file and the writing speed of your USB drive.

Optionally, you can also set the block size with the ‘bs‘ parameter to 4MB (default) or 1MB, if you like:

sudo dd bs=4M if=/home/user/Downloads/CentOS-6.5-x86_64-minimal.iso of=/dev/sdb

Third, use ‘sync’ to apply the changes:

sudo sync

Done! Now, put your USB drive in the host you want to install and boot it.

PHP Match dates which may differ a few seconds from each other

At my work, I needed to compare two CSV files containing CDR’s (Call Detail Records). To made it quick and easy, I made a PHP script to do this.

Some CDR’s were missing in our file or vice versa. The CDR’s which were not missing needed to be compared with each other. The problem was that I could only match on the phone numbers and the dates. The phone numbers will always be the same, but the other CSV contained CDR’s which dates deviates from the dates in the other file. To make sure I got the right CDR’s to match, I made a method which could match on dates which may deviate a few seconds:

function datesMatch($dateOne, $dateTwo) {	
	$dateOne = trim($dateOne);
	$dateTwo = trim($dateTwo);

	if(is_numeric($dateOne) && is_numeric($dateTwo)) {	
		$dateOne = new DateTime(substr($dateOne, 0, 4).'-'.substr($dateOne, 4, 2).'-'.substr($dateOne, 6, 2).' '.substr($dateOne, 8, 2).':'.substr($dateOne, 10, 2).':'.substr($dateOne, 12, 2));
		$dateTwo = new DateTime(substr($dateTwo, 0, 4).'-'.substr($dateTwo, 4, 2).'-'.substr($dateTwo, 6, 2).' '.substr($dateTwo, 8, 2).':'.substr($dateTwo, 10, 2).':'.substr($dateTwo, 12, 2));

		$difference = $dateOne->diff($dateTwo);

		if(		$difference->y == 0 && 
				$difference->m == 0 && 
				$difference->d == 0 &&
				$difference->h == 0 &&
				$difference->i == 0 &&
				$difference->s <= 4
			) {
			return true;

	return false;

The ‘4’ in this case shows that I allow dates to deviate 4 seconds from each other. This means that $dateOne may be four seconds in the past or in the future, compared with $dateTwo and vice versa. The $difference variable is actually a DateInterval class.

By the way, I had to check if the date variables were numeric, since the data in the CSV files was not that consistent.

I hope this will be useful for you too. 🙂

Regular expression for matching URL

For a piece of software I wrote, I needed a security check which validates URLs.

It needed to match any URL starting with HTTP or HTTPS, the host could be a IP or a hostname (like localhost) and it also should match port numbers (like :8080 for example) and trailing directories (/directory/anotherdirectory).

Basically, it should validate the following URLs:

  • http://localhost/directory/directory
  • http://localhost:8080/directory/directory
  • http://srv01.domain.tld:10005/directory/directory
  • https://srv01.domain.tld/

I found a sort-of solution on StackOverflow which seems to be compliant with the RFC1123 standard. Those solutions are not really what I want, but together, they will be:


I also added some extra’s to match a full URL, including HTTP/HTTPS, port numbers and trailing directories:


I tested this one on RegexPlanet and it seems to work perfectly.

This is how it looks like in Java code. I splitted up the regex for better readability and maintenance (mind the extra escaping backslashes, which are needed when you use regexes in Java):

// Regular expression is based on the RFC1123 standard:
// Solution found at:
private final static String URL_PATTERN_FRONT = "^(https?)://";
private final static String URL_PATTERN_IP = "(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))";
private final static String URL_PATTERN_HOSTNAME = "|((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])))";
private final static String URL_PATTERN_REAR = "(:[1-9][0-9]+)?(/)?([/?].+)?$";


Hopefully, this will work for you, too. 🙂

Trust Direct Webscan 19200 on Linux

The problem

A few years ago, I bought a Trust Direct Webscan 19200 flat-bed scanner. I used it quite regularly at that time with my desktop PC, called “Atlantis”, running Windows XP.

Trust Direct Webscan 19200

Trust Direct Webscan 19200

Last year I upgraded my PC hardware and software and currently I’m running Windows 7 on Atlantis. Some time ago, I needed to scan a document and I realized I did not install any drivers for the scanner since I have not needed to scan anything since my PC upgrade. When I tried to install the drivers from the Trust scanner driver CD, the setup came up with a message saying that the scanner is not compatible with any Windows OS newer than Windows XP.

I have tried to get the scanner operational again on Windows 7 using several work-arounds, without any luck, unfortunately.


Besides Atlantis I also have a home server, called “Discovery”, running Debian Linux. Trust does not have official drivers for Linux, though I have seen some successful setups using scanners on Linux on the web before, so I decided to give it a try.

After some searching on the web I found this page, which practically contains all the information needed to get a scanner up and running on Linux. Well.. on Debian based Linux distributions to be precise, like Debian itself and Ubuntu of course. Setting up a scanner on other Linux or Unix distributions might turn out to be a bit different.

First, I checked this page to see if my scanner was compatible. In my case it seemed to be well supported and there is a suitable USB driver available for it. If you click on the link in the “backend” column, you will get a page where you can download most of the USB drivers suitable for your scanner.

Step by step

To keep a long story short, I will describe step-by-step what I did to get my scanner working on Debian.

First I checked if my scanner was compatible by reviewing the list on this page:

The Trust Direct Webscan 19200 scanner has the support status “Good”, so I clicked on the link in the “backend” column to get to this page where I was able to download the correct USB driver. In my case, I needed the PS1Dfw.usb variant according to the list, but it turned out I needed the ps1fw.usb variant eventually. If there is no suitable variant for your scanner, you can probably snag it from the Windows installation CD-ROM alternatively.

Now I know that my scanner was compatible and the suitable USB drivers are available, I was able to proceed with the installation.

I installed SANE:

sudo apt-get install sane

Then I cd’t to the directory the USB driver needs to be installed:

cd /usr/share/sane/gt68xx/

I downloaded the USB driver to that directory:

sudo wget

Then I modified the /etc/sane.d/gt68xx.conf and added the following row below the settings for the scanner:

firmware "ps1fw.usb"

I connected my scanner and used the following command to check if SANE could recognize my scanner:

sudo sane-find-scanner

SANE recognized the scanner and said it was a Mustek BearPaw 1200 CU (instead of a Mustek BearPaw 1200 CU Plus according to the list on the webpage, therefore I realized I had the wrong USB driver the first time).

I used the following command to check if SANE was able to ‘see’ the scanner:

sudo scanimage -L

It turned out to be all OK, so I tried to make a first scan:

sudo scanimage -v -p --format=tiff > /var/www/tmp/out.tiff

By default it will create a black-and-white image (Gray option) of the scanned image. Eventually, after some trying and testing, I created a little script which is optimized to my needs:

#!/usr/bin/env bash

scanimage -L

scanimage --mode Color --depth 8 --resolution 300 -v -p --format=tiff > /var/www/tmp/out.tiff

The scanned image will have color and a resolution of 300dpi, which is sharp enough in my opinion. The /var/www/tmp/out.tiff is the location where the output image will be placed. I used the /var/www/ directory so I was able to download the raw TIFF files directly with my web browser on Atlantis.

You can tweak the settings if you like. Raising the depth and/or resolution will result in much larger output files. For example, I have tried a depth of 12 (48 bits per pixel) with a resolution of 600dpi. The output TIFF file was over 200 Megabytes!

For now, this script needs to run by the root user, unfortunately. I am trying to get it to work with non-root users, then I will be able to call the script via a web page, like a PHP script or something. My goal is to create a web-interface which makes me able scan a document by a single click on a web page. That same page should push the scanned image to my browser when it is done scanning (like downloading a file from a website). When I have figured that out, I will update this post to show you what I have done. 🙂


Update (2015-03-30):

I have updated the scan script:

#!/usr/bin/env bash
# Should be run as 'root' or via 'sudo'

DATE=$(date +%Y-%m-%d_%H:%M:%S)


echo "Current file location set to $DIRECTORY$ORIGINAL_FILENAME"

scanimage -L

# Resolutions from 300 DPI to 2400 DPI are most common. Usually, 300 DPI is good
# enough in most cases and resolutions beyond 2400 DPI are used for small original 
# images.
scanimage --mode Color --depth 8 --resolution 300 -v -p --format=tiff > $DIRECTORY$ORIGINAL_FILENAME


The scanned image will now have a file name based on the current date and time. Also, the scanned TIFF image will be converted to JPG format right after the scanning has been done (using the convert command from imagemagick package) and the original TIFF file will be removed afterwards. This will save disk space (since the TIFF files can become quite large depending on the scan resolution) and you will have the scanned image in a more common file format right away. 🙂