Tuesday, October 19, 2010

Merging several PDF files in Linux

I often google on "How to merge multiple files into a single PDF". Well, it is quite a simple task to put together, or combine, PDF files without installing "Pdftk". All you need is just the GhostScript package. Here is a sample command taken from one of my bash-scripts:

#!/bin/bash
gs \
    -dBATCH \
    -dNOPAUSE \
    -q \
    -sDEVICE=pdfwrite \
    -r600x600 \
    -sPAPERSIZE=a4 \
    -sOutputFile=merged-file.pdf \
    file-1.pdf \
    file-2.pdf \
    file-3.pdf

Thursday, June 10, 2010

Decrypting mojibake in Linux

Today I received an e-mail, which contained something completely unreadable.

=D0=9D=D0=B5 =D0=BF=D1=8B=D1=82=D0=B0=D0=B9=D1=82=D0=B5=D1=81=D1=8C =D0=BE=
=D1=82=D0=B2=D0=B5=D1=87=D0=B0=D1=82=D1=8C =D0=BD=D0=B0 =D1=8D=D1=82=D0=BE =
=D0=BF=D0=B8=D1=81=D1=8C=D0=BC=D0=BE! =D0=9E=D1=81=D1=82=D0=B0=D0=B2=D0=BB=
=D1=8F=D0=B9=D1=82=D0=B5 =D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=
=D1=80=D0=B8=D0=B8 =D0=BD=D0=B0 =D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=
=D0=B5 =D1=8D=D1=82=D0=BE=D0=B9 =D0=B7=D0=B0=D1=8F=D0=B2=D0=BA=D0=B8

When using Windows, I get used to use the "Stirlitz" utility (ru.wikipedia.org) to automatically decrypt mojibake (or "krakozyabry" as we call the stuff in Russian).

As far as I know, all these equal signs, "A"s, "B"s, "C"s, etc., are used in the so called "Quoted-printable" encoding.

At my OpenSUSE 11.2 I found a small utility called recode that is well suitable to solve the task. I saved an e-mail as 66.eml and pipelined it to recode. The latter successfully decoded QP into UTF-8:

$> cat 66.eml | recode /qp

Saturday, May 1, 2010

Monitoring SMART hdd data in linux

When I was using Windows, I used to manually monitor S.M.A.R.T. status of my hard disk drive, say, once a month with Everest utility program. Is there such a tool for GNU/Linux? The smartmontools package provides smartd SMART disk monitoring daemon and smartctl SMART disks control and monitor utility.

The command

# smartctl -i /dev/sda

prints the following information (-i) on my HDD (/dev/sda):

smartctl 5.39 2009-08-08 r2872~ [x86_64-unknown-linux-gnu] (openSUSE RPM)
Copyright (C) 2002-9 by Bruce Allen, http://smartmontools.sourceforge.net

=== START OF INFORMATION SECTION ===
Model Family: Hitachi Travelstar 5K160 series
Device Model: HITACHI xxx######x#xx##
Serial Number: xx#x##x#xx#xxx
Firmware Version: SBDIC7JP
User Capacity: 120,034,123,776 bytes
Device is: In smartctl database [for details use: -P show]
ATA Version is: 7
ATA Standard is: ATA/ATAPI-7 T13 1532D revision 1
Local Time is: Sat May 1 23:01:43 2010 MSD
SMART support is: Available - device has SMART capability.
SMART support is: Enabled


And the command

# smartctl -A /dev/sda

list all the familiar SMART attributes (-A) and their values:

smartctl 5.39 2009-08-08 r2872~ [x86_64-unknown-linux-gnu] (openSUSE RPM)
Copyright (C) 2002-9 by Bruce Allen, http://smartmontools.sourceforge.net

=== START OF READ SMART DATA SECTION ===
SMART Attributes Data Structure revision number: 16
Vendor Specific SMART Attributes with Thresholds:
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
1 Raw_Read_Error_Rate 0x000b 100 100 062 Pre-fail Always - 0
2 Throughput_Performance 0x0005 100 100 040 Pre-fail Offline - 4564
3 Spin_Up_Time 0x0007 253 253 033 Pre-fail Always - 0
4 Start_Stop_Count 0x0012 099 099 000 Old_age Always - 3007
5 Reallocated_Sector_Ct 0x0033 100 100 005 Pre-fail Always - 0
7 Seek_Error_Rate 0x000b 100 100 067 Pre-fail Always - 0
8 Seek_Time_Performance 0x0005 100 100 040 Pre-fail Offline - 0
9 Power_On_Hours 0x0012 081 081 000 Old_age Always - 8418
10 Spin_Retry_Count 0x0013 100 100 060 Pre-fail Always - 0
12 Power_Cycle_Count 0x0032 099 099 000 Old_age Always - 2653
191 G-Sense_Error_Rate 0x000a 100 100 000 Old_age Always - 0
192 Power-Off_Retract_Count 0x0032 100 100 000 Old_age Always - 73465872
193 Load_Cycle_Count 0x0012 089 089 000 Old_age Always - 118737
194 Temperature_Celsius 0x0002 148 148 000 Old_age Always - 37 (Lifetime Min/Max 14/45)
196 Reallocated_Event_Count 0x0032 100 100 000 Old_age Always - 0
197 Current_Pending_Sector 0x0022 100 100 000 Old_age Always - 0
198 Offline_Uncorrectable 0x0008 100 100 000 Old_age Offline - 0
199 UDMA_CRC_Error_Count 0x000a 200 253 000 Old_age Always - 0
223 Load_Retry_Count 0x000a 100 100 000 Old_age Always - 0


Please note, that all mentioned commands are executed in superuser mode.

By the way, manual page on -A (--attributes) key provides comprehensive information on interpretation of SMART attributes for newbies.

Computations in linux command line

The bc command is useful to perform some simple arithemetical calculations in console. It may be used in scripts too.

For example, percent values from my previous post have been obtained as

$> echo '265594/292891' | bc -l
$> echo '262938/292891' | bc -l

Argument of echo contains an arithmetic express, and echo pipes it to the bc with standard math library attached (-l).

Do you want to know the Euler's number e (the base of the natural logarithm)?

$> echo 'e(1)' | bc -l

Let's print first 80 digits of π (pi) using arctangent function (a(1.0)):

$> echo 'scale=80; 4.0*a(1.0)' | bc -l

The bc has interactive mode as well; just type

$> bc

In order to quit from interactive mode, type quit or exit.

Optimizing PNG with optipng and pngcrush

There several tools to perform lossless optimization of images in PNG format.
My favourite GNU/Linux distro repositories contain two ones: optipng and pngcrush.

Pngcrush is invoked as follows:

$> pngcrush -e .opt.png -brute *.png

The command tries to reduce size of all PNG files from current directory (*.png) using brute-force search (-brute). Of course, you may omit the -brute key and use default heuristics (which is much faster and produce comparable compression). Resulting files will have .opt.png suffix (-e .opt.png), i.e. vpn-nm-7.png becomes vpn-nm-7.opt.png.

In order to get more information on pngcrush command line keys type:

$> pngcrush -h | less

Another program, optipng, is a bit more friendly, for my taste.
Its syntax is quite similar:

$> optipng -keep -o7 *.png

The command optimizes all PNG images from current directory (*.png) using exhastive search (-o7) and keeps backups (-keep).

As usual, in order to get more information on optipng type:

$> man optipng 

Finally, I have written down compression ratios of pngcrush and optipng on my eight small test PNG files obtained from KSnapshot.

pngcrush - 265594/292891 bytes - 90.7%
optipng  - 262938/292891 bytes - 89.8%

Query recently installed packages in openSUSE

The following command prints the list of recently installed packages in openSUSE GNU/Linux:
$> rpm -qa --last
I believe it should work in other RPM-base distros as well. Alternative title for this entry could be the following: list installed rpm-packages by date.

Thursday, April 22, 2010

sshfs: mounting remote directory to your filesystem

You can work with filesystems at remote machines as if the were parts of your local system. The magic wand is a sshfs command.

$> sshfs -C username@example.com: ~/example

This command mounts username's home directory at example.com host to local directory ~/example. -C option enables compression to save your internet traffic.

Use the following command to unmount remote directory:

$> fusermount -u ~/example

As usual, for more details see

$> man sshfs