Working Ninja

Add the following to your ~/.bash_profile for simple merging of PDFs:

pdfmerge() { 
    gs -dPDFSETTINGS=/prepress -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=$@


pdfmerge merged.pdf first.pdf second.pdf



I recently ran into a situation where I needed to copy some CSV data residing on a Windows machine to a Linux machine. The remote desktop client software I was using (on my Linux machine) wasn't set up to nativiely allow copy and paste to and from the server. It just so happened that I was recently working with netcat and thought this would be a great way to send the data to my destination machine. Sadly, Windows does not include netcat, so I dug around a bit and found that telnet would be able to take its place. So with a tool to use on both the sending and receiving side of things, I set out to implement what I'm calling "'universal' copy and paste".

So on my Linux machine (destination), I set up the netcat to listen on port 1234:

netcat -l 1234

On the Windows machine (source), I used telnet to connect to my Linux machine on port 1234, like so:

telnet 1234

I could then paste the CSV data on my Windows machine and it would immediately start outputting on my Linux machine. Ta-da, simple and "universal" copy and paste.

Finally, I would be remis if I did not mention that--while this is a working approach--it is not a secure one. The connection is not encrypted so everything is transmitted in clear-text.


A situation arose where I was unable to use nc to send some metrics to a local Graphite server. Thanksfully, Python was installed (albeit, quite a dated version) that allowed me to set up a socket to send the data over the wire.

import commands
import socket
import time

def get_device_stats(device):
    # Retrieve 1 second average from `iostat` for device.
    status, output = commands.getstatusoutput('iostat 1 2 -kd -p /dev/%s | awk NR==7' % device)
    values = output.split()
    timestamp = int(time.time())

    send_metric(device, 'iops', values[1], timestamp)
    send_metric(device, 'read', values[2], timestamp)
    send_metric(device, 'write', values[3], timestamp)

def send_metric(device, metric, value, timestamp):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('', 2003))
    # New-line character is important here, otherwise Graphite will not parse the sent data.
    s.sendall(' %s %s\n' % (device, metric, value, timestamp))

devices = ['sda', 'sdb', 'sdc']

for device in devices:



Take the following HTML snippet:

    <li>Second to Last</li>

We would like to select the second to last list item (<li>). Here's how we'd go about doing that:

ul li:nth-last-child(2) { color: blue; }

Or, the inverse (everything but the second to last element):

ul li:not(:nth-last-child(2)) { color: red; }

Objective: Sort dictionary by nested dictionary value

Let's say we want to sort the following JSON by the nested dictionary value of count.

  "Mail": {
    "count": 20,
    "users": ["lukeskywalker", "darthvader"]
  "Droid Sync": {
    "count": 5,
    "users": ["lukeskywalker"]

Method 1: lambda

apps_sorted = sorted(apps.items(), key=lambda x: (x[1]['count']))

Method 2: key function

def sort_by_count(x):
    # key=lambda x: (x[1]['count'])
    key, data = x
    return data['count']

apps_sorted = sorted(apps.items(), key=sort_by_count)



First, list the shares available from the server:

smbclient -L <server> -U <user>

Next, make sure cifs-utils is available (so we can pass in the credentials option):

sudo apt install cifs-utils

Finally, add the desired share to /etc/fstab:

//ip/share /mnt/point cifs vers=3,credentials=/path/to/credentials,noexec 0 0

Email contents of a file:

echo -e "Subject: subject\n\n" | cat - /file | sendmail address

Email stdout:

echo -e "Subject: subject\n\n$output" | sendmail address

Here's a simple shell wrapper that prints all sockets opened by Firefox.

1. Add the following function to your ~/.bashrc:

function ffsocks() {
    netstat -anp --inet | awk '/firefox/ { print $5 }'

2. Reload your shell (or run source ~/.bashrc) to make ffsocks callable from your shell environment.

3. Finally, execute ffsocks!

$ ffsocks

The power of shell wrappers comes from their ability to "codify" a more complex command (or series of commands, what flags are used, etc) and also in their reusable nature (we can call ffsocks any time we want!). Thus, shell wrappers are a good option for complex commands that are called often (or for those hard to remember ones that you don't call too often--I guess they're just all around good to use whenever!).


By default, a ModelForm is populated with all its objects from the database. Sometimes it's desirable to limit these results as they are returned to the view. After setting up our ModelForm ( there is only one thing we need to do to make this happen. Within our view (, we update the queryset as follows:

class PollForm(ModelForm):
    class Meta:
        model = Poll
        fields = ('question',)

form = PollForm()
form.fields['question'].queryset = Poll.objects.filter(question__startswith="How")

This will filter Polls that start with "How".



The following example looks for the "idle" time before backlight brightness is reduced.

Find what you're looking for:

gsettings list-recursively | grep idle

Get the value:

gsettings get org.gnome.settings-daemon.plugins.power idle-brightness

Set the value:

gsettings set org.gnome.settings-daemon.plugins.power idle-brightness 120