The intention of this space is to create an easily accessible compendium of useful tips and tricks that I’ve found throughout the ages while surfing the wonders of the WWW.

Command Line Tools

adding characters to text files

Credit: Tim Zimmermann

Specially when I deal with csv files, some tools (e.g., gnuplot) sometimes have trouble parsing or reading the file. One can quickly solve this issue by adding a whitespace after every comma character using sed, e.g.:

sed -i 's/,/, /g; s/,\s\+/, /g' file

Removing Lines From Text Files

Credit: SiegeX

For deleting text lines containing a particular string, I use sed, e.g.:

sed -i '/pattern/d' ./nameOfFile

Reformatting Images

Resizing Images

Credit: Rinzwind

To quickly resize an image using convert, it’s as simple as:

convert figure.png -resize 128x128 resizedFigure.png

Convert Image Set Into Video

Credit: Hammad Mazhar

The previous list contains a pretty comprehensive list for accomplishing this by using ffmpeg.

My favorite usage goes something like this:

ffmpeg -framerate 6 -pattern_type glob -i 'LeftRightUnrectified*.bmp' -vb 20M -vcodec mpeg4 out.mp4

Convert Image Set Into GIF

I’ve tried multiple tools, such as ffmpeg, convert, etc, and the way in which I find it the easiest to customize the delay is using gm:

gm convert -delay 10 -loop 0 OpticsMount_0_Images/LeftRightUnrectified*.bmp out.gif

For further optimization, I use convert with a varying level of fuzz in the following way:

convert LeftRightUnrectifiedGif.gif -fuzz 5% -layers Optimize result.gif

Convert Video Into GIF

Credit: LordNeckbeard

Created a wrapper for using ffmpeg to create frames from a video, and then using convert to transform them into a GIF. The script is vid2gif.sh.

./vid2gif.sh TS99_15_T7-BothStatGM_OVHD.ts 480 0.5 0.2 5%

The following parameters are used:

  • 1: path to video file
  • 2: height of gif
  • 3: fps
  • 4: delay between frames
  • 5: amount of fuzz

PDF Compression (macOS & Linux)

Credit: Max Glenister

Using ghostscript, one can compress PDF files through the use of the following command:

gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/screen -dCompatibilityLevel=1.4 -sOutputFile=output.pdf input.pdf

Managing installed libraries

Credit: crenate

Using pkg-config to procure information on installed libraries, e.g., finding out one’s OpenCV version:

pkg-config --modversion opencv

Connecting to VPN using Command Line

Credit: A-B-B

When using Ubuntu 18, there seems to be an error while launching the Cisco Anyconnect application, so I opted to connect to the host using the command line:

/opt/cisco/anyconnect/bin/vpn -s connect HOST

One will then get prompted for username, password, and second password if duo authentication is required.

Git Tools

Update branches

Credit: John Szakmeister

Allows one ot update one’s local list of remote branches, which I always tend to forget how to do:

git remote update origin --prune

IPython

Apply changes after edits

Credit: Andrew

I was always very annoyed whenever changes to my files wouldn’t be recognized during an ipython session (so much time spent debugging thinking there was an error in my code, when the only issue was that the changes weren’t being taken into account). Using the autoreload extension, one is able to have the modules automatically reloaded before executing user code.

%load_ext autoreload
%autoreload 2

Gnuplot

Reading parameters from external file

Credit: stackoverflow

TODO: … Explain and post example.

C++ Tricks

Creating directory during runtime

This page is an excellent references, with its example reproduced here:

#include <sys/stat.h>

const int dir_err = mkdir("foo", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (-1 == dir_err) {
    printf("Error creating directory!n");
    exit(1);
}

creating threads within classes

Credit: Adam Rosenfield

This stackoverflow post has some pretty nifty advices: