Skip to content

Instantly share code, notes, and snippets.

View ernstki's full-sized avatar

Kevin Ernst ernstki

  • Earth, Sol
View GitHub Profile
@ernstki
ernstki / imapfrm.py
Last active July 13, 2026 21:27
imapfrm - Print selected headers from an IMAP inbox
#!/usr/bin/env python3
##
## imapfrm - Print selected headers from an IMAP mailbox
##
## Uses sensible defaults that should work with DavMail[1], but you must
## define IMAP_LOGIN in the environment (or hard-code it below).
##
## The name is meant to vaguely invoke `frm` from the GNU Mailutils suite,
## and earlier historical utilities, although I made no attempt to match its
## options or output format.
@ernstki
ernstki / watchmeincrement.py
Last active July 3, 2026 19:26
Watch for new files created matching a pattern, move them somewhere else with an auto-incrementing numerical prefix
#!/usr/bin/env python3
##
## Authors: Kagi Quick, gemini/gemini-3.1-pro-preview, Kevin Ernst
## Date: 3 July 2026
## Licence: WTFPL
## Homepage: https://gist.github.com/ernstki/ba50c42ec22984afdf870030fdb84132
##
## Usage: python watchincrement.py "*.pdf" ~/Documents/00_TO_READ
##
import re
@ernstki
ernstki / !flatpak-du.md
Last active June 21, 2026 17:36
A `du` for Flatpak

flatpak-du: like du for Flatpaks

This utility is intended to produce more consistent and useful output than flatpak list, while papering over some of the more irritating quirks of flatpak list --show-details, such as:

  • showing completely different columns depending on whether standard out is a terminal or not
  • inability to control column headers; shown when stdout is a terminal, hidden otherwise
@ernstki
ernstki / bigdirs
Last active June 6, 2026 02:16
GNU Awk script to report large directories, N levels deep in a hierarchy
#!/usr/bin/gawk -E
##
## List the top N directories (N levels deep) by cumulative file size
##
## Usage:
##
## find . -type f -printf "%p\t%s\n" | gawk -E bigdirs [options]
##
## # or, if `bigdirs` is in your search path
## find some/path -type f -printf "%p\t%s\n" | bigdirs [options]
@ernstki
ernstki / !Makefile to assist in managing local packages with GNU Stow.md
Last active May 10, 2026 03:33
Makefile to assist in managing `/usr/local` with GNU Stow

GNU Stow doesn't like to replace (or remove) cache files like share/glib-2.0/gschemas.compiled and share/icons/hicolor/icon-theme.cache.

If another package installed those, but the package you're trying to stow has its own, you'll get an error like this:

$ sudo stow seahorse-41.0/
WARNING! stowing seahorse-41.0 would cause conflicts:
  * existing target is neither a link nor a directory: share/glib-2.0/schemas/gschemas.compiled
  * existing target is neither a link nor a directory: share/icons/hicolor/icon-theme.cache
All operations aborted.
@ernstki
ernstki / !Useful ufw application profiles.md
Last active April 19, 2026 06:43
Useful ufw application profiles

Put these in /etc/ufw/applications.d and use them like this:

sudo ufw allow from 192.168.1.0/24 to any app lms
sudo ufw allow from 192.168.1.0/24 to any app barrier

References

@ernstki
ernstki / watchme.py
Last active February 25, 2026 02:20
Very, very simple watchdog-based Python interpreter launcher that works with the Geany editor; watches your script for changes and re-runs it
#!/usr/bin/env python3
##
## watchme.py - monitor *.py files or a given path and re-run 'python <path>'
##
## Author: Kevin Ernst
## Date: 4 February 2026
## License: Apache License Version 2.0
## (https://github.com/gorakhargosh/watchdog/blob/master/LICENSE)
## Homepage: https://gist.github.com/ernstki/759383313185aae343bc87b989c44241
##
@ernstki
ernstki / Unsafelinks.pm
Last active March 9, 2026 16:23
Convert Microsoft Outlook "safelinks" URL back into human-readable URLs
@ernstki
ernstki / !elementary OS Contractor tricks.md
Last active May 10, 2026 00:58
Stupid Linux `.desktop` and elementary OS Contractor tricks

elementary OS's Contractor service uses standard Linux .desktop files (spec) with a .contract extension to provide context (secondary-click) menu entries for specific MIME types. System-wide apps put these in /usr/share/contractor, whatever the Flatpak version of that is, and you can create your own in ~/.local/share/contractor. Below are some examples.

Contractor actions for all MIME types

If you want a specific Contractor action to apply to all filetypes, including device special files, directories, whatever, you can exploit the ! (exclusion) operator in the MimeType key, like this:

MimeType=!nothing;
@ernstki
ernstki / !multised.md
Last active October 20, 2025 01:14
Multi-line replacement in sed - https://stackoverflow.com/q/75220171

Multi-line text replacement with sed

Original question: "Catch an entire function with grep" on Stack Overflow

It is possible to use (GNU) grep to do multiline matching[^fn1], but you're probably making more work for yourself that way, because sed can do basically everything grep can plus make replacements.

Using grep -zoP to do multiline matching is fundamentally the same trick as what's proposed below: turning your file into one long string, separated by