General Notes (Σωρός)

convert (imagemagick): “not authorized” problem

Once I tried to convert(1) an image to pdf, I got an error message of the type

not authorized ..... @ error/constitute.c/ReadImage/412

This is solved by changing /etc/ImageMagick[-6]/policy.xml.

Comment out the line:

<policy domain="coder" rights="none" pattern="MVG" />

Change the following line:

<policy domain="coder" rights="none" pattern="PDF" />

to be like this:

<policy domain="coder" rights="read|write" pattern="PDF" />

Funally, add the line:

<policy domain="coder" rights="read|write" pattern="LABEL" />

How to insert a backslash in a latex text


If you want to type “bar” you must write \testbackslash{bar}.

Emacs: Create and store a macro

Create a macro:

  • C-x ( start recording a macro.

  • C-x ) stop recording a macro.

Run a macro:

  • C-x e execute last macro, once.

  • C-u 37 C-x e execute last macro 37 times.

How to give name to the last macro:

  • M-x name-last-kbd-macro

How to insert the lisp code of a macro inside a buffer:

  • M-x insert-kbd-macro <RET> macroname <RET>

bash: get matching substring

The bash command to get a substring that matches a regular expression is:


Returns the length of the mathing substring.

The command is part of coreutils.

Check laptop battery on linux

Use the upower command. UPower provides an interface to enumerate power sources on the system and control system-wide power management.

At the command line run upower -e to enumerate the the object paths of the available devices (including battery). Then run upower -i <object_path> to get the info. For example (in my laptop):

$ upower -i /org/freedesktop/UPower/devices/battery_CMB1
  native-path:          CMB1
  vendor:               Fujitsu
  model:                CP656340-01
  serial:               02B-S170905000249S
  power supply:         yes
  updated:              Πεμ 04 Οκτ 2018 11:39:20 μμ EEST (52 seconds ago)
  has history:          yes
  has statistics:       yes
    present:             yes
    rechargeable:        yes
    state:               fully-charged
    warning-level:       none
    energy:              70,0312 Wh
    energy-empty:        0 Wh
    energy-full:         70,0312 Wh
    energy-full-design:  72 Wh
    energy-rate:         0 W
    voltage:             12,819 V
    percentage:          100%
    capacity:            97,2656%
    technology:          lithium-ion
    icon-name:          'battery-full-charged-symbolic'

Check CPU/HDD temperature on Linux (ubuntu)

For CPU Temperature:

  1. Install lm-sensors: sudo apt-get install lm-sensors

  2. Detect sensors: sudo sensors-detect

  3. To see cpu temperatures run: sensors

For HDD Temperature:

  1. Install hddtemp: sudo apt-get install hddtemp

  2. To see hdd temperature: sudo hddtemp /dev/sda

Emacs: Predefined color names

M-x list-colors-display

Insert a line in the middle in multiple files

Suppose you want to insert a line at 10th line of all files in current directory (e.g. all txt files). The following command creates a backup for each file with the extension “.bak” and changes the files inplace.

sed -i'.bak' '10iThis is the inserted line' *.txt

Convert Encoding

Convert from ISO-8859-7 to UTF-8:

iconv --from-code=ISO-8859-7 --to-code=UTF-8 iso-file.txt > utf8-file.txt

To list all available encodings supported by iconv:

iconv -l

iconv is part of glibc.

Crop a pdf

apt-get install texlive-extra-utils

pdfcrop --margins "<left> <top> <right> <bottom>" input.pdf output.pdf

Convert multiple odt files to pdf

for F in `ls -1 *odt`; do soffice --convert-to pdf $F --headless; done

Python, Perl: Inplace filtering


import fileinput
import sys

def process_line(s):
    # do something to a string
    return s

for fname in sys.argv[1:]:
    for line in fileinput.input(files=fname, inplace=True, backup='.bak'):


python3 ./ <FILES>


perl -pi.bak -e 's/<REGEX>/<REGEX>/g' <FILES>

Passwordless SSH

Suppose you are logged in machine A and you need passwordless access to an account on machine B.

(1) Create RSA keys:

ssh-keygen -t rsa -f B_key

This will create two files: B_key and The first is the private key and the second is the public key of the RSA protocol.

(2) Copy the contents of into the file authorized_keys in the .ssh directory of the remote account:

cat | ssh B_user@B_machine 'cat >> .ssh/authorized_keys'

(3) Login to machine B:

ssh -i B_key B_user@B_machine

Python: Get second Maximum of list of integers without comparison, loop or recursion

Function sm() returns the second maximum of a list of integers without any comparison or loop.

from functools import *

# Returns 1 if a>b, else 0.
gt = lambda a,b: ( (b-a)>>((b-a).bit_length()) ) & 0x1
gtn = lambda a,b: (b == None) or gt(a,b)

# a<=b. Returns 1 if a<y<b, else 0.
btn = lambda a,b,y: gtn(y,a) and gtn(b,y)

# (neq) Returns 0 if a=b, 1 otherwise --not needed.
neq = lambda a,b: gt(a,b)+gt(b,a)
eq = lambda a,b: not neq(a,b)

# t=(a,b). Returns (x2,x1) where
# x1 is max of a,b,y and x2 is second max of a,b,y.
m2 = lambda t,y: (t[1],y) if gtn(y,t[1]) \
else ( (y,t[1]) if btn(t[0],t[1],y) \
else (t[0],t[1]))

# Executes m2() cumulatively on l.
sm = lambda l: reduce(m2,l, (None,None))[0]

Note that in Python 3 Integers have unlimited length, so we need bit_length function to determine how many bits we need to shift.

Python: one line sign function of a number:

sgn = lambda x: x and (1,-1)[x<0]

In Python a boolean variable is subclass of integer taking two values, 0 for False and 1 for True. Also, every boolean expression returns the value of last evaluated argument

Windows: convert a drive from FAT32 to NTFS

This command converts drive E: to NTFS:

c:>convert E:/FS:NTFS

Unlimited bash history

Edit /etc/bash.bashrc:

export HISTTIMEFORMAT="%y%m%d%H%M%S "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $ $USER "$(history 1)" >> $HOME/.bash_eternal_history'

Create a script to use it as history command:


if [ $1 == all ];
    # Removes duplicate lines
    awk '!a[$0]++' ~/.bash_eternal_history;
    # Removes the first 4 columns of the complete output, showing only command
    awk '!a[$0]++' ~/.bash_eternal_history | awk '{for (i=5;i<=NF;i++){printf "%s ", $i}; printf "\n" }';

Generally speaking, it’s very useful to have the history of all commands you have ever typed. However bash keeps only a limited number of commands. There are two environment variables, HISTSIZE and HISTFILESIZE [bash(1)] which define the maximum number of commands and the maximum number of lines in the history file. In theory, there is no upper bound to the value you can assign to them. In real life though, mysterious vanishment of history data file has been reported around.

A workaround for this, is defining the envirnment variable PROMPT_COMMAND [bash(1)]. A good idea is to define it at the central bashrc file of the system, /etc/bash.bashrc, so that unlimited history will be available for all users:

export HISTTIMEFORMAT="%y%m%d%H%M%S "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $ $USER "$(history 1)" >> $HOME/.bash_eternal_history'

The first line defines the format of the date. The second line, for each expression that we type, tells bash to print the process id ($$) of current bash session, the username ($USER) and the last line of bash history, which is what we just typed. When we cat the file that our unlimited history is stored, we notice that there are a lot of multiple records of the same command. This happens because when we simply press “enter”, the expression $(history 1) has always the value of the last command we typed. We workaround this by creating a little script and use that as a history command:

awk '!a[$0]++' ~/.bash_eternal_history

In this, awk [awk(1)] filters out the duplicated lines.

How to limit memory of a process

This can be done with cgroups

As root:

cgcreate -g memory:/mylimit
echo  $((1000*1024*1024)) > /sys/fs/cgroup/memory/mylimit/memory.limit_in_bytes
cgclassify -g memory:mylimit PID

Alternatively, a process can be moved in a group with the command:

echo PID > /sys/fs/cgroup/memory/mylimit/tasks

Also cgroup_enable=memory swapaccount=1 must be passed as parameters to the kernel (e.g. at GRUB_CMDLINE_LINUX_DEFAULT variable in /etc/default/grub)

Emacs Org Mode: Sparse Trees

  • Manual

  • C-c / (org-sparse-tree) prompts for an extra key to select a sparse-tree

  • C-c / r or C-c / / (org-occur) creates a sparse tree with match pretty anywhere (see the manual for details).

Python: Send authenticated email with starttls

import smtplib
from email.mime.text import MIMEText

fromAddress = '...'
toAddress = '...'
Subject = 'Αρνάκι'
smtp_user = '...'
smt_password = '...'
smtp_host = '...'
port = 587

msg = MIMEText('Αρνάκι άσπρο και παχύ')
msg['Subject'] = Subject
msg['From'] = fromAddress
msg['To'] = toAddress

server = smtplib.SMTP(smtp_host, port)
server.login(smtp_user, smtp_password)
server.sendmail(fromAddress, toAddress, msg.as_string())

Gmail: How to forward all emails including spam

1. Settings --> Filters --> Create new Filter
2. From: *
3. Create filter with this search
4. check: Never send it to Spam

Ubuntu Mate: How to send custom desktop notification

The command is

notify-send "Message"

emacs orgmode: show only current tree

There are two options for this:

  • org-tree-to-indirect-buffer which creates a new buffer showing only the subtree on which cursor is located. Both buffers edit the same file; any change in one buffer reflects into the other.

  • org-narrow-to-subtree gives view of the current subtree in the current buffer. widen restores full view.

Create Random password with Python

Creates a random password of 10 characters with letters, numbers and punctuation:

import string
import random

pw_length = 10
print(''.join(random.choice(string.ascii_letters + \
                         string.digits + \
                         string.punctuation) \
                         for x in range(pw_length) \
          ) \

Convert elm to mbox in order to read it with mutt

formail -b < message.eml > message.mbox
mutt -R -f message.mbox

Swap without tmp variable

Traditional way:

tmp = a
a = b
b = tmp

1st way without tmp:

a = a + b
b = a - b
a = a - b

2nd way (bitwise logical and):

a = a ^ b
b = a ^ b
a = a ^ b

Concatenate pdf files

pdftk file1.pdf file2.pdf ... cat output newfile.pdf

How to comment out lines in emacs org mode

  • Lines starting with zero or more whitespace characters followed by one ‘#’ and a whitespace are treated as comments and, as such, are not exported.

  • Likewise, regions surrounded by ‘#+BEGIN_COMMENT’ … ‘#+END_COMMENT’ are not exported.


How to create a mysql database

create database <DATABASENAME>;

How to create a user for a database

create user '<USERNAME>'@'<HOST>' identified my '<PASSWORD>'


grant all privileges on <DATABASENAME>.* to '<USER>'@'<HOST>'

Copy a table from one database to another:

CREATE TABLE db2.table SELECT * FROM db1.table;

php: include vs incude_once, require vs require_once

  • _once: checks if file is already included.

  • include: includes blindly the file and continues on errors

  • require: exits on errors

Connection Python MSSQL.

It is done with Microsoft backed PyODBC. There’s also pymssql, but it seems to be unmaintained.

We first need to install microsoft odbc driver for MSSQL. Then install pyodbc with all necessary dependencies. Documentation, sais everythnig.

Python: split a string by two characters

>>> import re
>>> result = re.split('[,:]+', 'A:3, B:2, C:14')
>>> print(result)
['A', '3', ' B', '2', ' C', '14']

Python: strip all strings in a list

>>> my_list = ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
>>> map(str.strip, my_list)
['this', 'is', 'a', 'list', 'of', 'words']