Random

Some useful things (to me, not necessarily to you) I keep having to redo because they get lost in spacetime. All credit goes to the brave souls at StackOverflow.

Bash

Compressing all pdfs in a folder (and subfolder)

In a recent version of the ghostscript (or gs for close friends), a script I have always used stopped working, so here’s what I’ve been doing. This works for Ubuntu (including the Linux subsystem version in Windows 11).

#!/bin/bash
for f in $(find . -name '*.pdf')
do 
  echo "Processing $f"
  # Grab filename without extension (just whatever comes before the first .)
  # might fail for other purposes, but works fine for .pdfs
  fnoe="${f%.*}"
  # Backup original file. This will also automatically backup any existing
  # backups so it's mostly safe.
  mv $f ${fnoe}_bckup.pdf
  # Convert to .ps
  pdf2ps ${fnoe}_bckup.pdf ${fnoe}.ps
  # Convert back to .pdf
  ps2pdf -dPDFSETTINGS=/printer ${fnoe}.ps $f
done

Replace file contents

Did your e-mail change and how you have to search through all manifests to replace it with the new one? Fear not.

You can replace string A with string B within all files, folders, and subfolders by using some regex magic, as follows.

find . -type f -exec sed -i'' -e 's/A/B/g' {} +

Rename all files using

New prefix for all the libraries you have? No worries. Based on this and this and this and maybe some others.

Rename all folders and files by replacing substring A to B using regex magic.

find . -type f -execdir rename 's/A/B/g' {} +

“Uninstalling” a cmake installed package

CMake installations create a file install_manifest.txt with all the installed file locations. You can remove them all with.

sudo xargs rm < install_manifest.txt

Running a command on a set of folders

For instance, git pull is all subfolders.

for d in */ ; do
    echo "Git pull in $d..."
    cd "$d"
    git pull
    cd ..
done