Random Wisdom


Linux Power Management

by on Feb.13, 2014, under How To ..., Linux, Software

Just came across TLP – Linux Advanced Power Management which looks quite interesting. Not least because it contains built-in support for tpacpi-bat which I have discussed previously as a solution for setting the battery charge thresholds on my ThinkPad E530.

Pre-built binary packages are provided via a Yum repository system which is also a nice touch — making this a very easy solution to get set up with. The default configuration file saved in /etc/defaults/tlp looks sensible, although I did have to uncomment the battery charge thresholds.

Leave a Comment :, , more...

Create links with absolute paths in Linux

by on Jan.16, 2010, under How To ..., Linux, Software

The default behaviour of the linking command (ln) is a little strange under certain circumstances. Since it creates the links using the literal value of the target, symbolic links created using relative path structures can often fail. Consider the following:

$ ln -s targetfile ../src/targetfile_link

Without a doubt, ‘targetfile_link’ will be a broken symlink since it links to a target that it assumes is in the same directory:

$ cd ../src && ls -l targetfile_link
lrwxrwxrwx 1 mafgani mafgani 5 2010-01-16 18:19 targetfile_link -> targetfile

This is quite unfortunate since it clearly clashes with the way that the linking mechanism should work intuitively.

The solution is to force ln into automatically appending the absolute path to the target files. This can be achieved by using a simple shell script that acts as a wrapper for the real linking command:


# Step through the supplied arguments and append the absolute
# path to targets that exist
for ARG in $@
  if [ -e $ARG ]; then
    LNARGS="${LNARGS} ${PWD}/${ARG}";
    LNARGS="${LNARGS} ${ARG}";

# Execute the actual link command with the modified args
exec /bin/ln ${LNARGS};

There are two known caveats:

  • The link is ‘sub-optimal’ if created from within the destination directory (the absolute path contains ‘../’s). It will still work however.
  • The links will always be absolute. If that is undesirable, save the script as ‘absln’ or something other than ‘ln’.

Using ‘absln’ instead of ‘ln’ in the previously described scenario now produces a working symlink:

$ absln -s targetfile ../src/targetfile_link
$ cd ../src/ && ls -l targetfile_link
lrwxrwxrwx 1 mafgani mafgani 16 2010-01-16 19:13 targetfile_link -> /tmp/files/targetfile
1 Comment :, , , , , more...

Graphics format conversion

by on Dec.09, 2009, under LaTeX, Linux, Software

Up until now I have been using the ‘convert‘ tool that comes with ImageMagick to switch between image formats — mainly for creating EPS files from JPG/PNG (raster format) files for use with LaTeX. Then I came across sam2p.

It is a light-weight utility that does one thing only and it does it well: convert between image formats. I’ve been using it for a while now and find that it can greatly reduce files sizes with minimal drop in quality. I’ve even used it to process existing EPS files just to get the reduction in file size. Best of all, it is multi-platform — executables are available for both Windows and Linux on the project homepage.

Goodbye convert and hello sam2p!

2 Comments :, , , , , , , more...

Just Host cPanel cleanup script for Greasemonkey

by on Oct.23, 2009, under How To ..., Software

I finally decided to get my own domain and signed up with Just Host for the registration and hosting. For now I’m only using it to host this blog that originally started its life on Blogger. The transition was fairly smooth, save for a few minor issues. I’ll talk more about the steps involved a future post (a draft is already in the queue) …

Just Host offers cPanel as a web frontend for managing and running site related tasks. It’s quite a nice tool but there is one big problem. The interface is literally littered with a bunch of annoying ads and affiliate links. Even more annoying is the fact that there doesn’t seem to be any way of permanently moving these offending boxes to the bottom of the screen. This is where Greasemonkey comes in.

Greasemonkey is a nice little extension for Firefox that allows the execution user scripts to change the way a website looks. userscripts.org is a great place for finding scripts that work on major/popular sites on the internet. A search there didn’t turn up anything useful so I decided to write my own. Install Greasemonkey if you don’t have it already and then click on this link to get the script installed. I’ve also put up a copy on userscripts.org.

The script works by setting the ‘display’ style of the offending div boxes to ‘none’. Firebug is another great tool that makes it a breeze to find out the IDs of the divs that need to be blacklisted.

[Update 10-Nov-2009]: Looks like some sneaky new ads injected using Javascript have shown up on the cPanel sidebar. Unlike the old ad boxes however, these lack div IDs. As a result, it is not possible to simply blacklist them. Fortunately, it also means that it is possible to turn the table around by simply blocking the sidebar divs that have a null ID. The script has been updated.


Before applying greasemonkey script

Before applying greasemonkey script


After applying greasemonkey script

After applying greasemonkey script

4 Comments :, , , , , , more...

Printing multi-page duplex documents

by on Oct.18, 2009, under How To ..., Linux, Software

The psnup tool can be used to place multiple pages on each sheet of a document. E.g., the following command places two pages from the input file into each sheet of the output:

$ psnup -l -2 input.ps output.ps

While psnup is excellent for quick “N-up” conversion jobs, it doesn’t provide much control over the layout. The pstops utility on the other hand allows for fine grained scale, rotation and placement settings for each page that goes into a sheet of the output. The command syntax is a bit more complicated on account of the page specification strings that must now be provided. The following example shows a typical command needed to prepare a document for duplex printing with two pages on each side of a sheet:

$ pstops -pa4 \
  '4:0L@0.8(21cm,-1cm)+1L@0.8(21cm,12.55cm),2R@0.8(0,29.85cm)+3R@0.8(0,16.25cm)' \
  input.ps output.ps

The command is best understood by referring to the relevant section from the manpage:

       Pstops rearranges pages from a  PostScript  document,  creating  a  new
       PostScript  file.   The  input  PostScript file should follow the Adobe
       Document Structuring Conventions.  Pstops can  be  used  to  perform  a
       large  number  of  arbitrary  re-arrangements  of  Documents, including
       arranging for printing 2-up, 4-up, booklets, reversing, selecting front
       or back sides of documents, scaling, etc.

       pagespecs follow the syntax:

              pagespecs   = [modulo:]specs

              specs       = spec[+specs][,specs]

              spec        = [-]pageno[L][R][U][@scale][(xoff,yoff)]

       modulo is the number of pages in each block. The value of modulo should
       be greater than 0; the default value is 1.  specs are the page specifi-
       cations  for  the  pages in each block. The value of the pageno in each
       spec should be between 0 (for the first page in the block) and modulo-1
       (for  the  last page in each block) inclusive.  The optional dimensions
       xoff and yoff shift the page by the specified amount.   xoff  and  yoff
       are  in  PostScript’s points, but may be followed by the units cm or in
       to convert to centimetres or inches, or the flag w or h to specify as a
       multiple  of  the width or height.  The optional parameters L, R, and U
       rotate the page left, right, or upside-down.  The optional scale param-
       eter  scales the page by the fraction specified.  If the optional minus
       sign is specified, the page is relative to the  end  of  the  document,
       instead of the start.

       If  page  specs  are  separated  by + the pages will be merged into one
       page; if they are separated by  they will be  on  separate  pages.   If
       there  is only one page specification, with pageno zero, the pageno may
       be omitted.

       The shift, rotation, and scaling are performed in that order regardless
       of which order they appear on the command line.
Leave a Comment :, , , , more...

Restricting access to SFTP / SCP

by on Oct.18, 2009, under How To ..., Linux, Software

rssh is a tool that allows SFTP/SCP for file transfers over SSH but denies shell access — useful for preventing users from running commands on the system. More details are available on the tool’s homepage.

I first came across it on this page.

Leave a Comment :, , , , , more...

Realtime collaborative text editing

by on Nov.21, 2008, under Linux, Software

A while ago, I came across Etherpad. It a web based platform that allows multiple users to simultaneously edit a single text file. Since it doesn’t seem to support any kind of mark-up at the moment, it would seem that it’s not terribly useful for word processing tasks. Perhaps it’s good for real-time collaborative coding and the creation of agenda type lists …

The software equivalent of Etherpad is Gobby. It’s a multi-platform tool that claims to run on Microsoft Windows, Mac OS X, Linux and other Unix-like platforms — making it almost as flexible as a web-based service. There are a number of other advantages:

  • Flexibility and security that comes from having absolute control over the sessions.
  • Syntax highlighting!
Leave a Comment :, , , more...

Bash process substitution

by on Oct.03, 2008, under Linux, Software

From the Advanced Bash-Scripting Guide:

“Piping the stdout of a command into the stdin of another is a powerful technique. But, what if you need to pipe the stdout of multiple commands? This is where process substitution comes in.

Process substitution feeds the output of a process (or processes) into the stdin of another process.”

The syntax is:


Example: comparing the head of two files using diff

$ diff -u <(head -n3 /var/log/dmesg) <(head -n3 /tmp/dmesg)
--- /proc/self/fd/63 2009-05-26 19:52:45.144544140 +0100
+++ /proc/self/fd/62 2009-05-26 19:52:45.149544007 +0100
@@ -1,3 +1,3 @@
-Initializing cgroup subsys cpuset
-Initializing cgroup subsys cpu
-Linux version (mockbuild@xenbuilder2.fedora.redhat.com)
 (gcc version 4.3.2 20081105 (Red Hat 4.3.2-7) (GCC) )
 #1 SMP Mon Mar 23 23:37:54 EDT 2009
+Linux version (brewbuilder@hs20-bc2-4.build.redhat.com)
 (gcc version 4.1.2 20070626 (Red Hat 4.1.2-13))
 #1 SMP Thu Sep 27 18:48:03 EDT 2007
+BIOS-provided physical RAM map:
+ BIOS-e820: 0000000000000000 - 000000000009f000 (usable)

The diff header clearly shows that file descriptors are used as the underlying mechanism.

Leave a Comment :, , more...

Embedding fonts in a PDF document

by on Oct.03, 2008, under How To ..., LaTeX, Linux, Software

It is often a good idea (or a requirement) to embed the used font faces in a PDF document. This is easily accomplished using ps2pdf during the final stage of conversion of a document from PS to PDF:

$ ps2pdf -sPAPERSIZE=a4 -dPDFSETTINGS=/printer -dCompatibilityLevel=1.3 \
         -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true \
         'input_file.ps' 'output_file.pdf'

An explanation of the command options can be found in the Ps2pdf.htm file in the Ghostscript documentations (or here).


Leave a Comment :, , , , more...

Re-encoding MP3 files using LAME

by on Jun.06, 2008, under How To ..., Linux, Software

I have some MP3 files encoded at a constant bitrate of 320kbps that my phone seems to have trouble playing smoothly. So, I looked into LAME.

The files I had were named using the following scheme:

01 - Title of track 01.mp3
02 - Title of track 02.mp3

I used the BASH for-loop construct to process the files:

$ for A in *.mp3;\              # Process one mp3 at a time
  do B=${A%.mp3};\              # Extract track number and title
     C=${B#?? -};\              # Extract the title
     D=${B%% - *};\             # Extract the track number
     lame --vbr-new -V0 -q0\    # Variable-bitrate, high-quality
          --mp3input\           # Inputs are MP3 files
          --tt "$C"\            # ID3v2 tags: title
          --ta 'Artist Name'\   # ID3v2 tags: artist
          --tl 'Album Title'\   # ID3v2 tags: album
          --ty 2007\            # ID3v2 tags: year
          --tn "$D"\            # ID3v2 tags: track no.
          --tg 'GENRE'\         # ID3v2 tags: genre
          "$A" processed/"$A";\ # Keep filename and save in ./processed/

Since no bit-rate bounds are explicitly provided, the re-encoded files can contain anything between 32kbps and 320kbps. The LAME man-page provides an extensive list of options and their meanings.

1 Comment :, , , , more...