Random Wisdom

How To ...

Create links with absolute paths in Linux

by Mostafa 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:

#!/bin/sh

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

# 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
Leave a Comment :, , , , , more...

Squeezing space in LaTeX

by Mostafa on Oct.29, 2009, under How To ..., LaTeX

Academic papers and articled often come with a predefined maximum page count and it is common to find that it’s a limit that is easily exceeded. Under such circumstances, it becomes necessary to pull a few “dirty tricks” that squeeze out every last bit of available space.

The most common approach is to simply redefine the ‘\baselinestretch’ variable in the preamble of the document. The parameter controls the scaling of the space between the bottom of two successive lines of text. Therefore, the definition used to squeeze that space by 2% is:

\renewcommand{\baselinestretch}{0.98}

While that trick alone is sufficient in most cases, it is useful to be aware of other spacing parameters that can be adjusted. The Cambridge University Engineering Department has a nice page with lots of details. I personally find ‘\textfloatsep’ to be one of the more useful ones:

\addtolength{\textfloatsep}{-5mm}

It is used to reduce the amount of space that is usually left between a float and the adjacent text block (e.g. end of caption of a top-figure and the text below).

Leave a Comment :, , , more...

Just Host cPanel cleanup script for Greasemonkey

by Mostafa 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:

Before applying greasemonkey script

Before applying greasemonkey script

After:

After applying greasemonkey script

After applying greasemonkey script

3 Comments :, , , , , , more...

Embedding fonts in a PDF document

by Mostafa 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).

[Source]

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

Re-encoding MP3 files using LAME

by Mostafa 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/
  done

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.

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

Processing files using ‘find’

by Mostafa on Mar.26, 2008, under How To ..., Linux, Software

In its most basic form, find is often used to locate files that are subsequently piped through a complex set of commands for processing. However, this particular method is easily broken by files that contain spaces in their names.

This is where the ‘exec’ option provided by find comes in handy. From the man-page:

-exec command ;
       Execute  command;  true  if 0 status is returned.  All following
       arguments to find are taken to be arguments to the command until
       an  argument  consisting of ‘;’ is encountered.  The string ‘{}’
       is replaced by the current file name being processed  everywhere
       it occurs in the arguments to the command, not just in arguments
       where it is alone, as in some versions of find.  Both  of  these
       constructions might need to be escaped (with a ‘\’) or quoted to
       protect them from expansion by the shell.  See the EXAMPLES sec-
       tion  for examples of the use of the ‘-exec’ option.  The speci-
       fied command is run once for each matched file.  The command  is
       executed  in  the  starting  directory.    There are unavoidable
       security problems surrounding  use  of  the  -exec  option;  you
       should use the -execdir option instead.

An example that recursively removes all *.doc files from the current directory would be:

$ find . -name \*.doc -exec rm {} \;
Leave a Comment :, more...

Embedded multimedia in LateX/Prosper

by Mostafa on Dec.01, 2007, under How To ..., LaTeX

The movie15 package by Alexander Grahn is useful for this purpose. The movies embedded, however, cannot be viewed with any PDF reader on Linux.

Leave a Comment :, , more...

Extracting Audio/Video

by Mostafa on Dec.01, 2007, under How To ..., Linux, Software

It’s really easy to extract either audio or video from a multimedia file using ‘ffmpeg‘. To extract audio only:

$ ffmpeg -i inputfile -vn -acodec copy outputfile

And for video only, replace ‘-vn‘ with ‘-an‘ and ‘-acodec‘ with ‘-vcodec‘.

ffmpeg is also commonly used as a transcoding tool.

Leave a Comment :, , more...

SSH Blacklisting

by Mostafa on Nov.29, 2007, under How To ..., Linux, Software

After getting around 1500 failed ssh login attempts a day for a while on a server I manage, I decided to look into tools that automatically blacklist offending IPs.

Sshblack fits the bill perfectly. A HOWTO (including an init-script) for REDHAT-like systems is available from the OSS Watch Wiki.

Leave a Comment :, , , more...

Logo in a LaTeX document header/footer

by Mostafa on Oct.22, 2007, under How To ..., LaTeX

Easily accomplished using the ‘fancyhdr‘ package:

\usepackage{fancyhdr}
\renewcommand{\headheight}{0.6in}
\setlength{\headwidth}{\textwidth}
\fancyhead[L]{}% empty left
\fancyhead[R]{ % right
   \includegraphics[height=0.53in]{img-file}
}
\pagestyle{fancy}

All of that goes into the preamble of the document.

3 Comments :, , , , more...