Word wrap a text file

Wrapping a text file to a fixed number of character is easy in Linux:


fold -w 80 -s input.txt > output.txt

Above -w 80 set the character limit to 80 which is standard. The flag -s tells to break lines at white space, not between words.

Convert text file to ASCII character encoding

It’s often needed to convert text between different character encodings, for example from UFT8 to conventional ASCII.
Linux systems come with command line tool iconv that does the job:


iconv -f utf8 -t ascii input.txt > output.txt

To see a list of all supported encodings do


iconv -l

Changing file permissions recursively

Every now and then you’ve got a massive directory tree whose reading permissions need to be changed. For example, all files readable to all users, all directories accessible to all users, all executables executable by all users.

This is easy enough to do with find and chmod commands. Assuming that the root of the directory tree is somedir:

Set all directories to rwxr-xr-x (a.k.a. 755)
find somedir -type d -print -exec chmod 755 {} \;

Make all files readable
find somedir -type f -print -exec chmod go+r {} \;

Change all 744 files, executable by the owner, to 755, executable by all:
find somedir -type f -perm 744 -print -exec chmod 755 {} \;

Java or GTK applications crashing in KDE

In Kubuntu 13.10 Java applications seemed to crash at start up with the following error:

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f8f34cb9b3c, pid=25450, tid=140253340165888
#
# JRE version: Java(TM) SE Runtime Environment (7.0_45-b18) (build 1.7.0_45-b18)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [libgobject-2.0.so.0+0x31b3c] g_type_check_instance_is_a+0x3c
#

The same error occurred regardless of the Java Runtime Environment installed. Some GTK applications are also affected.

As mentioned (and in many other bugs) this is related to the KDE oxygen-gtk theme.

Solution:
In System Settings -> Application Appearance -> GTK change GTK2 and GTK3 themes to something else than oxygen-gtk.

KBibtex in Ubuntu Oneiric 11.10

KBibtex is a useful bibtex bibliography management tool for the KDE desktop.
However, it seems that the package is missing in the Ubuntu 11.10 repositories.

A quick remedy (as tweeted here) is to install the package from the Debian Sid distribution: kbibtex.
To install on the command line (check the exact name of the .deb file)
sudo dpkg -i kbibtex_0.4-1_amd64.deb

Alternatively .deb packages can be installed in the GUI (e.g. by opening the file in Dolphin).

The package should be compatible with Ubuntu 11.10, and seems to work fine.

The 0.4 version has improved a lot and has a very useful online search tool for acquiring bibtex files. Google scholar and other data bases can be used.
Other tools include a reference preview (though bibtex2html) and a PDF preview.

Autocrop images with imagemagick

Often one has to crop excess margins from an raster image. Common image graphical editors have autocrop tool, but if many files need to be processed, opening and saving files one by one quickly becomes tedious.

A better solution is to use convert tool that is a part of the powerful Imagemagick toolkit:

convert image.jpg -trim -bordercolor White -border 20x10 +repage cropped_image.jpg
This command reads the original file, removes (trims, crops) white borders, adds 20 pixel white borders horizontally and 10px vertically, and stores the file as cropped_image.jpg.

It is also possible to process all image files in the current directory with the following one line command:
for i in `ls *.jpg`; do convert $i -trim -bordercolor White -border 20x10 +repage cropped_`basename $i`; done

If you want to convert to another image format, it can be done conveniently at the same time (-quality option controls the jpg quality):
for i in `ls *.gif`; do convert $i -trim -bordercolor White -border 20x10 -quality 92 +repage cropped_`basename $i gif`jpg; done
Above basename utility is used for trimming suffix from the file name.

Extract images from a pdf file

In Linux pdfimages utility extracts raster images from a pdf file.

Extract all raster images from pdf file, save in jpg format (creates bar-000.jpg, bar-001.jpg, etc.):
pdfimages -j file.pdf image

The -j option is to convert the images to jpeg format if possible.
Other options can be found in the manual.

Using wget to download multiple files from a website

The following command downloads all files pdf files from http://www.host.com/some/path/ to currenct directory
wget -r -l1 -nd -nc -A.pdf http://www.host.com/some/path/

The options are:
-r Makes it recursive for subfolders
-l1 set maximum recursion, 1 levels of subfolders
-nd no directories — copies all matching files to current directory, discards directory information
-nc Do not download it file already exists
-A.pdf Accept only certain files (with pdf suffix in this case). Specify comma-separated lists of file name suffixes or patterns.

Script for downloading bibtex file using DOI

Digital Object Identifier (DOI) and bibtex files are common for anyone citing scientific articles. The DOI is used to uniquely identify any object, such as an electronic document, while bibtex is used to cite an article in Latex environment.

An article can be found on the internet via the address dx.doi.org/ followed by the DOI, e.g. http://dx.doi.org/10.1016/j.ocemod.2004.08.002. However, it takes a while to browse the publishers website, locate the appropriate bibtex file and save it to disk.

Here is a very handy Python script that fetches a bibtex file from NASA’s Astrophysics Data System using the article’s DOI.

It requires Python Beautiful Soup XML/HTML parser.
In Ubuntu-like linux distributions it can be installed easily:
sudo apt-get install python-beautifulsoup

Example:

>>doi2bibtex.py 10.1016/j.ocemod.2004.08.002
@ARTICLE{2005OcMod...9..347S,
author = {{Shchepetkin}, A.~F. and {McWilliams}, J.~C.},
title = "{The regional oceanic modeling system (ROMS): a split-explicit, free-surface, topography-following-coordinate oceanic model}",
journal = {Ocean Modelling},
year = 2005,
volume = 9,
pages = {347-404},
doi = {10.1016/j.ocemod.2004.08.002},
adsurl = {http://adsabs.harvard.edu/abs/2005OcMod...9..347S},
adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}

Fixing screen brightness keys in MacBook + Ubuntu 10.10

After upgrading to Ubuntu 10.10 natty, the keyboard backlight keys and screen brightness keys did not work in macbook pro.

Solution:
Make sure that Mactel-support PPA repository in enabled. It is usually deactivated during the upgrade process.
Install nvidia-bl-dkms package:
sudo apt-get update 
sudo apt-get install nvidia-bl-dkms