Linux: Open a file with the default application

If you are on a Linux shell and want to open a file with whatever default application is configured for that type, this often works:

xdg-open Quote.odt
xdg-open invoice.pdf
xdg-open index.html

Pro Tip

Make an alias so you have a simpler API (like Mac OS): alias open=xdg-open or alias x=xdg-open.

Background

You can choose your "Default applications" via UI in the Ubuntu "Settings" application (gnome-control-center). This is just a very rough setting (e.g. open Photos with Shotwell Viewer).

If a certain file...

netstat: Sum open connections by IP (and sort it)

The following sums up all connections (ESTABLISHED, TIME_WAIT, FIN_WAIT, etc.) and sorts it:

netstat -n | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

Works on FreeBSD and Linux.

Sending TCP keepalives in Ruby

When you make a simple TCP connection to a remote server (like telnet), your client won't normally notice when the connection is unexpectly severed on the remote side. E.g. if someone would disconnect a network cable from the server you're connected to, no client would notice. It would simply look like nothing is being sent.

You can detect remote connection loss by configuring your client socket to send TCP keepalive signals after some period of inactivity. If those signals are not acknowledged by the other side, your client will terminat...

Install PhantomJS under Ubuntu

PhantomJS has no binary builds for Linux, and building it yourself is painful.

However, you can install it through npm:

sudo npm install -g phantomjs

Detect recently added vdisks in a VMware ESXi linux guest

After adding a vdisk to an ESXi linux guest you will assert that you can't find a new device in the running VM. You have to force a SCSI rescan.

  1. find your host bus number with: grep mtpspi /sys/class/scsi_host/host?/proc_name
  2. output should look like this: /sys/class/scsi_host/host2/proc_name:mptspi where the bold part is what you're searching for
  3. force an SCSI rescan with echo "- - -" > /sys/class/scsi_host/<host>/scan and insert the host found in point two for <host>
  4. cat /proc/scsi/scsi should now show the new ad...

Browse Amazon S3 buckets with Ubuntu Linux

There are some frontends available, but they all suck, are no longer maintained or are hard to install.

As a surprisingly comfortable alternative I have found a command line tool s3cmd:

sudo apt-get install s3cmd

When you run s3cmd the first time it will ask you for your access key ID and secret access key. This information is cached somewhere so you only need to write them once. To reconfigure later, call s3cmd --configure.

Once you're done setting up, s3cmd gives you shell-like commands like s3cmd ls or `s3cmd del som...

Linux: Kill a process matching a partial name

This is useful to kill processes like ruby my-script.rb:

pkill -f my-script.rb

With great power comes great responsibility.

Ubtuntu: "FATAL: Could not load /lib/modules/...-generic/modules.dep: No such file or directory"

If you get this error (probably because you want to load some modules):

# modprobe xt_comment
FATAL: Could not load /lib/modules/3.2.0-40-generic/modules.dep: No such file or directory

The reason could be that apt-get autoremove already removed /lib/modules/.../modules.dep even if you still using this kernel version:

# uname -r
3.2.0-40-generic
# ls -l /lib/modules/
total 24
drwxr-xr-x 4 root root 4096 Oct 22 17:05 3.2.0-68-generic
drwxr-xr-x 4 root root 4096 Jan  7 16:38 3.2.0-69-generic
drwxr-...

Install or update Chromedriver on Linux

Option 0: Download from the official page (preferred)

Chromedriver must be available in your path. You can add ~/bin to your path like this:

echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bash_profile

If you're also using Geordi, disable automatic updating of chromedriver in ~/.config/geordi/global.yml:

a...

Linux: Running a program with a different locale than your default

When your system is not running on English, you may sometimes want to run some applications and not use your system locale.
Use cases are scripts that parse output, or just using the (possibly) more common English labels or error messages. Here is how to do that.

I will use the date command and print the current weekday, just for the sake of an example.

Changing the locale using environment variables

Most often, setting LC_ALL for your command should be enough. The following was run on a system using a German locale.

$ date +%...

Linux: Get your public IP address from the shell

curl http://ipecho.net/plain; echo

Device sizing and network throttling are coming to Chrome DevTools

See screenshot here.

This is great news because network throttling is very painful in Linux.

The features are already in Chrome Canary, so expect them to come to your Chrome sources soon.

rbenv: How to update list of available Ruby versions on Linux

When you tell rbenv to install a Ruby it does not know about, you will get an error message.

$ rbenv install 2.1.2
ruby-build: definition not found: 2.1.2

You can list all available versions with `rbenv install --list'.

If the version you're looking for is not present, first try upgrading
ruby-build. If it's still missing, open a request on the ruby-build
issue tracker: https://github.com/sstephenson/ruby-build/issues

(Fun fact: Recent versions of ruby-build will give you a more helpful error message which...

How to simulate limited bandwidth in Google Chrome and Firefox

Your development machine is usually on a very good network connection.
To test how your application behaves on a slow network (e.g. mobile), you can simulate limited bandwidth.

Chrome

  • Open the dev tools (Ctrl+Shift+I or F12) and switch to the "Network" tab
  • In the row below the dev tool tabs, there's a throttling dropdown which reads "Online" by default.
  • Inside the dropdown, you will find a few presets and an option to add your own download/upload/latency settings.

Firefox

  • Open the dev tools (Ctrl+Shift+I or F12) and switc...

Linux: Disable Bluetooth on startup

In /etc/rc.local add:

service bluetooth stop

Skype screen sharing: Select which monitor to share

If you have multiple monitors and want to share a given screen with your call partner, drag the caller window to the desired monitor, then start sharing.

This is for Skype 4.2+ on Linux.