Thursday, 14 May 2015

How To Shrink Your Virtualbox VM And Free Up Space For Your Hard Disk - Make Tech Easier

Ever needed to share VirtualBox machines, transfer them over the internet or back them up?

This article describes how to reduce the size of your virtual machine hard disk. Space savings are incredible, especially if you've been adding/deleting files to the VM hard drive a lot.

What the article does not say (because it is obvious): zip your virtual machine after performing the described steps and before transferring/backing it up.

Link: How To Shrink Your Virtualbox VM And Free Up Space For Your Hard Disk - Make Tech Easier

Sunday, 10 May 2015

Migrate a VMware disk to Virtualbox: VMware player does not work on the Dell XPS 13 2015 Developer Edition

The Dell XPS 13 2105 Developer Edition is a wonderful machine. It comes with plenty of punch in a small laptop but the most interesting thing is that it comes with Ubuntu loaded on it.

If you plan to use VMware player on it, you should know that I got plenty of issues as soon as I started using it: freezes every day or so, the sound was distorted and copy/paste wan't working 100%, I won't get in the details why I chose VMware player over Virtualbox in the first place.

For my office work (read: Microsoft Office 2013 ;-), I needed and purchased licences from Microsoft and wasn't too keen on creating a new virtual machine so I decided to migrate it. The best (and only) working solution I found was to convert the virtual harddrive and use it in VirtualBox. Here are the steps I followed (source):

Install qemu

sudo aptitude install qemu

Convert the virtual disk to a raw image

qemu-img convert /path/to/original.vmdk converted.bin

Convert the raw image to a VirtualBox vdi format:

VBoxManage convertdd converted.bin converted.vdi

 I then recreated a VirtualBox virtual machine around that harddrive and was good to go.
Note: I had to call the Microsoft support to (re)activate (virtual hardware had changed) Windows. I suppose they don't mind as long as you don't use the other virtual machine (which I don't intend to do since it was crashing my computer).

Please let me know if it helps (in the comment section below).

Friday, 31 October 2014

Recover everything between two lines matched with grep

I frequently need to recover everything between two lines (that I can match with grep).

some text
random text
random text
other text
Let's say I want to retrieve the 'random text' lines but have no clue of what it looks like. I however know that the previous line contains the unique 'some text' string and that the line just after the random text I want to retrieve contains the unique 'other text' string.
Note the previous and next lines can contain any other text on them as long as I have a pattern I can match with grep.

Here is how I solve that problem;

Find the pattern before and return it with 999999 lines after

grep 'some text' -A 999999

Remove the first line of the previous results (start at line two)

tail -n +2

Find the pattern following our random text and return it with 999999 lines before it 

grep 'other text' -B 999999

Skip the last line of these results

head -n -1

Put together we get

cat file.txt |  grep 'some text' -A 999999 | tail -n +2 | grep 'other text' -B 999999 | head -n -1

Friday, 24 October 2014

Write your (Word) document with 'style' using keyboard shortcuts

This is a quick tip for people who don't want to start clicking on the Heading/Normal styles all the time when writing a long document (full of titles). This is especially handy when making a quick fist draft taking care of the structure but without looking too much at the format.

Here are the most useful keyboard shortcuts I use in these cases. Change the current selection or line to:

  • Heading 1: Ctrl+Alt+1
  • Heading 2: Ctrl+Alt+2
  • Heading 3: Ctrl+Alt+3
  • Normal: Ctrl+Shft+N
Thes are keyboard shortcuts any good MSWord document writer should know.

Friday, 10 October 2014

Useful commands when parsing HTML from the command line

These are commands I'm using all the time when working with HTML files from the command line:

Replacing <BR> tags with actual carriage returns:

sed 's_<[bB][rR][^>]*>_\n_g'
Note this command works with
  • Capitalised and small letter tags e.g.: <BR> or <br>
  • Tags with additional parameters (which is nonsense), e.g.: <br class="something">
  • Tags with spaces and a closing slash, e.g.: <br />

Replacing &nbsp; with spaces

sed 's_&nbsp;_ _g'

Remove heading space at the begining of each line

sed 's_^[\t ]*__g'

Remove all remaining html tags

sed 's_<[^>]*>__g'

You can then use all these commands together:
cat file.html | sed 's_<[bB][rR][^>]*>_\n_g' | sed 's_&nbsp;_ _g' | sed 's_^[\t ]*__g' | sed 's_<[^>]*>__g'

Saturday, 4 October 2014

Change the browser in the html header with wget

Ever wanted to download a page or make an http request with a custom header?

The parameters below let wget pretend it is a regular browser:

wget --referer="" --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070725 Firefox/" --header="Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" --header="Accept-Language: en-us,en;q=0.5" --header="Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" --header="Keep-Alive: 300"
This can be useful to check e.g. if the server you're configuring blocks the right browsers and not the other ones.

Note: the blue referer lets you specify the page you were previously on.

Tuesday, 10 June 2014

SHELL: Generate a random string

# bash generate random 32 character alphanumeric string (upper and lowercase)

STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)