<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Hacking on Ó hAoḋa</title><link>https://ohaodha.ie/tags/hacking/</link><description>Ó hAoḋa — Hacking RSS Feed</description><generator>Hugo -- gohugo.io</generator><language>en-ie</language><copyright>Copyright © 2024 Aindréas Ó hAodha. Content licensed under CC BY-NC-SA 4.0. Code licensed under GPLv3.</copyright><lastBuildDate>Sat, 15 Aug 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://ohaodha.ie/tags/hacking/index.xml" rel="self" type="application/rss+xml"/><item><title>How I Listen to Music</title><link>https://ohaodha.ie/blog/how-i-listen-to-music/</link><pubDate>Sat, 15 Aug 2026 00:00:00 +0000</pubDate><guid>https://ohaodha.ie/blog/how-i-listen-to-music/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>Several years ago now, I decided to stop using Spotify as my primary means of listening to music, for reasons which I may or may not expand on in a future blog post, but which primarily boil down to three key points:&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Privacy concerns:&lt;/strong> while the annual &lt;a href="https://support.spotify.com/td-en/article/spotify-wrapped/">Spotify Wrapped&lt;/a> analysis is a lot of fun, it troubles me that Spotify has this kind of data on the general population as a whole. Listening habits can be used to infer emotional states, religious beliefs, &amp;amp; political affiliations&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup>, and Spotify can further identify names and geographical locations of their users from their account information.
Although I don&amp;rsquo;t find it particularly concerning that Spotify may have this data on &lt;em>me&lt;/em>, I find it objectionable that a large corporation would have this kind of data on broader society &amp;mdash;&lt;sup id="fnref:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup> and I don&amp;rsquo;t think it&amp;rsquo;s implausible that this sort of data &lt;em>could&lt;/em> be used to manipulate or predict significant societal events like elections.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Audio quality:&lt;/strong> Spotify infamously &lt;del>has&lt;/del> &lt;em>had&lt;/em> limited audio quality, with the highest audio quality setting (up until recently) being 320 kbps&lt;sup id="fnref:3">&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref">3&lt;/a>&lt;/sup>, which is quite good, but not audiophile quality.
However, Spotify have recently released lossless support which largely negates this point, although the &lt;a href="https://www.pocket-lint.com/review-spotify-lossless-audio-option/">ever-dissatisfied audiophile community seem to disagree&lt;/a>.
I&amp;rsquo;m not going to claim, however, that I have either the audio equipment or the hearing sensitivity to detect these subtle differences:
I&amp;rsquo;ve &lt;em>very rarely&lt;/em> upon occasion noticed a slight difference between my own lossless-audio files and Spotify&amp;rsquo;s default-quality audio, but the truth of the matter is that I simply do not have audio hardware of sufficient quality to detect these differences.
I build my music library with lossless audio not because I can definitely tell the difference, but in the interest of future-proofing, as I expect (or hope) that I will one day own the hardware necessary to detect these differences.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Access to music not on Spotify&lt;sup id="fnref:4">&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref">4&lt;/a>&lt;/sup>:&lt;/strong> not all of the music I want to listen to is on Spotify.
My then-favourite singer &lt;a href="https://www.youtube.com/channel/UCS42Pk262iOKUXUW9XW_SOg">Mars Argo&lt;/a> wasn&amp;rsquo;t on Spotify at the time, and although she since has returned from her extended disappearance, the majority of her discography still isn&amp;rsquo;t on Spotify.
There are a number of other songs and artists to which I would like to listen but are still not on Spotify, and with an increasing number of artists now electing to &lt;a href="https://www.thestreet.com/entertainment/spotify-faces-global-boycott-over-ceos-military-investments">boycott Spotify&lt;/a>, I imagine this issue is more relevant than ever.
Furthermore, I went through a brief phase of editing songs I listened to with the goal of removing parts which I didn&amp;rsquo;t like or extending parts I did like, and this requires you to have that audio file locally&lt;sup id="fnref:5">&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref">5&lt;/a>&lt;/sup> (although I rarely do anything like that these days).&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>For these reasons, as well as a general migration in my life away from proprietary software to FOSS solutions, I decided to build a locally-stored music library consisting of lossless audio files.
I recently lost my library of music files while running reckless &lt;code>rm&lt;/code> commands while sleep-deprived circa 04:00 one morning, which is one of the primary drawbacks of having everything locally, but anyone who&amp;rsquo;s not an absolute fool would be smart enough to have a back-up &lt;del>(except me)&lt;/del>.
So, while I&amp;rsquo;m rebuilding my music collection from scratch, I was inspired to write about my system in detail, as it&amp;rsquo;s grown quite sophisticated over the years.&lt;/p>
&lt;h2 id="how-i-organise-my-music-library">How I Organise My Music Library&lt;/h2>
&lt;p>The majority of music players organise music by the tags within the file metadata, deriving song titles, album titles, and band names in this manner;
however, this is something that I strongly dislike, as it&amp;rsquo;s it&amp;rsquo;s less explicit than a directory-based organisation, requires specialist programs to edit and update properly, and can be very inconsistent, as not all music files one obtains will be tagged properly &amp;mdash; arguably the majority won&amp;rsquo;t be tagged properly.
Therefore, while I make an effort to properly tag my music library for maximum portability, I primarily use a &lt;strong>directory-based organisation&lt;/strong> for my music:
each file is stored under the directory structure of Band Name ☞ Album Name ☞ Track Name.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">~/media/music
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">├── &amp;#39;Addison Rae&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">│ └── &amp;#39;(2025) Addison&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">│ ├── &amp;#39;01. New York.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">│ ├── &amp;#39;01. New York.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">│ ├── &amp;#39;02. Diet Pepsi.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">│ ├── &amp;#39;02. Diet Pepsi.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">│ ├── &amp;#39;03. Money Is Everything.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">│ ├── &amp;#39;03. Money Is Everything.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">│ ├── &amp;#39;04. Aquamarine.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">│ ├── &amp;#39;04. Aquamarine.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">│ &amp;lt;etc&amp;gt; ...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">├── &amp;#39;Alice in Chains&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">│ ├── &amp;#39;(1995) Alice in Chains&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;01. Grind.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;01. Grind.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;02. Brush Away.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;02. Brush Away.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;03. Sludge Factory.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;03. Sludge Factory.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;04. Heaven Beside You.flac&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">│ │ ├── &amp;#39;04. Heaven Beside You.lrc&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">│ │ &amp;lt;etc&amp;gt; ...
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>There are three primary pet peeves which annoy me about other directory structures I&amp;rsquo;ve seen for music libraries:&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Not numbering tracks:&lt;/strong> I want the file path to contain all the information about the track, including the artist name, the album name, the track&amp;rsquo;s number within the album, and the track title.
This makes it easy to manage the music library using standard file tools such as &lt;code>ls&lt;/code>, &lt;code>mv&lt;/code>, &lt;code>rm&lt;/code>, etc. and ensures that when a program is run upon the files in a directory, e.g., &lt;code>mpv 'The Velvet Underground'&lt;/code>, the program handles each track in the appropriate order.
Putting the album release year before the album title achieves a similar effect.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>No leading zeros in track numbering:&lt;/strong> if there are more than 9 tracks in a directory, the track numbering &lt;strong>must&lt;/strong> start with a leading zero for tracks 0&amp;ndash;9.
This ensures that the tracks are naturally self-ordering when using programs like &lt;code>ls&lt;/code> or &lt;code>sort&lt;/code>, which order items alphabetically:
failure to include a leading zero will result in item 12 appearing before item 2.
Each track number should have the same number of digits as every other track number to ensure proper sorting.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Not making use of the available character set:&lt;/strong>
you&amp;rsquo;ll often see filenames in which every non-alphanumeric character has been replaced with an underscore &lt;code>_&lt;/code> for no apparent reason.
While I understand removing characters which are especially difficult to handle in filenames, such as single-quotes &lt;code>'&lt;/code>, double-quotes &lt;code>&amp;quot;&lt;/code>, and forward-slashes &lt;code>/&lt;/code> (although single and double-quotes can be used in filenames without issue if escaped properly), and I myself remove these characters from filenames in my library, it seems to me to be a terrible waste to remove characters that are easy to handle in filenames such as parentheses &lt;code>()&lt;/code>.
Furthermore, removed characters are invariably replaced with underscores &lt;code>_&lt;/code> to create horrific, unreadable, and ugly filenames.&lt;/p>
&lt;p>There&amp;rsquo;s a near-superstitious hysteria around non-alphanumeric characters in filenames when the majority of non-alphanumeric ASCII characters are perfectly fine for most modern filesystems.
There are, of course, some characters which you do have to remove for certain filesystems, but these characters are usually few in number and vary from filesystem to filesystem.
The popular choices for desktop UNIX-like operating systems (such as ext4 and btrfs) are very permissive with the characters which they allow, and the only outright forbidden character is &lt;code>/&lt;/code>.
Of course, if you require portability to more arcane filesystems like FAT (for Android) or exFAT (for Windows), you&amp;rsquo;re quite hamstrung with the characters you can use, as they forbid &lt;code>\&lt;/code>, &lt;code>/&lt;/code>, &lt;code>:&lt;/code>, &lt;code>*&lt;/code>, &lt;code>?&lt;/code>, &lt;code>&amp;quot;&lt;/code>, &lt;code>&amp;lt;&lt;/code>, &lt;code>&amp;gt;&lt;/code>, and &lt;code>|&lt;/code>&lt;sup id="fnref:6">&lt;a href="#fn:6" class="footnote-ref" role="doc-noteref">6&lt;/a>&lt;/sup>.
However, these still leave a large characterspace to play with, especially when you consider that any modern filesystem will support Unicode, even if it didn&amp;rsquo;t originally.&lt;/p>
&lt;p>The only characters I recommend removing from filenames for most Linux-based desktop operating systems are the aforementioned single-quotes &lt;code>'&lt;/code>, double-quotes &lt;code>&amp;quot;&lt;/code>, and forward-slashes &lt;code>/&lt;/code>;
however, I do not recommend just deleting them or replacing them with underscores &lt;code>_&lt;/code>, but replacing them with the following more appropriate characters which are far less offensive to the eye:&lt;/p>
&lt;ul>
&lt;li>Replace &lt;strong>&lt;code>'&lt;/code>&lt;/strong> (&lt;code>U+0027 APOSTROPHE&lt;/code>) with &lt;strong>&lt;code>’&lt;/code>&lt;/strong> (&lt;code>U+2019 RIGHT SINGLE QUOTATION MARK&lt;/code>).&lt;/li>
&lt;li>Replace &lt;strong>&lt;code>&amp;quot;&lt;/code>&lt;/strong> (&lt;code>U+0022 QUOTATION MARK&lt;/code>) with &lt;strong>&lt;code>“&lt;/code>&lt;/strong> (&lt;code>U+201C LEFT DOUBLE QUOTATION MARK&lt;/code>) and &lt;strong>&lt;code>”&lt;/code>&lt;/strong> (&lt;code>U+201D RIGHT DOUBLE QUOTATION MARK&lt;/code>).&lt;/li>
&lt;li>Replace &lt;strong>&lt;code>/&lt;/code>&lt;/strong> (&lt;code>U+002F SOLIDUS&lt;/code>) with &lt;strong>&lt;code>⧸&lt;/code>&lt;/strong> (&lt;code>U+29F8 BIG SOLIDUS&lt;/code>).&lt;/li>
&lt;/ul>
&lt;p>If, however, you want to ensure the portability of your music library to different operating systems &amp;amp; filesystems, you&amp;rsquo;ll have to consider the restrictions of those filesystems as well; if you want completely generalisable, filesystem-agnostic filename normalisation, then I can concede that perhaps &lt;em>in that particular situation for compatibility with &lt;strong>all&lt;/strong> filesystems&lt;/em> that a general replace of special characters with underscores is justifiable.
For my uses, I also want my music library to be synchronised to my Android smartphone:
since Android uses the FAT32 filesystem, which is rather archaic in the characterset it supports, I am forced to be more aggressive in my substitution of characters that I would otherwise like to be.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>Regardless of my directory-based approach instead of a tags-based approach, I try to ensure that my music is properly tagged in an effort to ensure portability across devices and for future-proofing.
I do this using &lt;a href="https://docs.beets.io/en/latest/index.html">&lt;strong>beets&lt;/strong>&lt;/a>, a music organiser which you can use to import a collection of music files, which it will tag using sources including the &lt;a href="https://musicbrainz.org/">MusicBrainz&lt;/a> database, rename as appropriate in keeping with your naming conventions, and run plug-ins or user-specified hooks on the imported files.
My beets configuration is pretty standard, with it naming and organising files in accordance with the approach I&amp;rsquo;ve outlined above, embedding the album cover art, updating the tags, and so on;
the only non-standard part of my configuration is the following:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln">72&lt;/span>&lt;span class="cl">&lt;span class="nt">hook&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">73&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">hooks&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">74&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- &lt;span class="nt">event&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">album_imported&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">75&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">command&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;karaoke.pl &amp;#34;{album.path}&amp;#34;&amp;#39;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This hook runs once an album has been imported and executes a script I wrote called &lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/karaoke.pl">&lt;strong>&lt;code>karaoke.pl&lt;/code>&lt;/strong>&lt;/a> to
fetch lyrics for music files from the &lt;a href="https://lrclib.net/docs">LRCLIB API&lt;/a> and save them to &lt;code>*.lrc&lt;/code> files.
Surprisingly, I couldn&amp;rsquo;t find a good solution for downloading the synchronised lyrics for a song programmatically, so I had to write my own script to do so.&lt;/p>
&lt;p>Beyond what I&amp;rsquo;ve mentioned already, I find the following two command-line utilities indispensable for making files in a directory match a naming convention:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://man.archlinux.org/man/vidir.1.en">&lt;strong>&lt;code>vidir&lt;/code>&lt;/strong>&lt;/a>: allows you to edit a directory like a normal body of text in a Vi-buffer, the most useful feature of which I find to be using visual block mode to edit, add, or delete a column of characters in a list of filenames.&lt;/li>
&lt;li>&lt;a href="">&lt;strong>&lt;code>perl-rename&lt;/code>&lt;/strong>&lt;/a>: allows you to rename a number of files using regular expressions, without doing any overcomplicated nonsense like &lt;code>echo&lt;/code>ing filenames into &lt;code>sed&lt;/code> and interpolating the output into a &lt;code>mv&lt;/code> command.&lt;/li>
&lt;/ul>
&lt;h2 id="music-player">Music Player&lt;/h2>
&lt;p>I use &lt;a href="https://mpv.io/">&lt;strong>&lt;code>mpv&lt;/code>&lt;/strong>&lt;/a> for playing both video and audio;
it has nice features like the ability to play all the files in a directory in alphabetical order (which is why it&amp;rsquo;s good to number your tracks properly) with a command like &lt;code>mpv &amp;quot;King Crimson/(1969) In The Court Of The Crimson King&amp;quot;&lt;/code>, and it automatically detects &lt;code>*.lrc&lt;/code> files and displays the synchronised lyrics at the appropriate time on-screen.
I use the &lt;a href="https://github.com/mpv-player/mpv/blob/master/TOOLS/umpv">&lt;strong>&lt;code>umpv&lt;/code>&lt;/strong>&lt;/a> script to achieve an enqueueing effect with mpv:
by default, running &lt;code>mpv file1.mp3; mpv file2.mp3&lt;/code> will spawn two separate mpv windows, one playing each file;
running &lt;code>umpv file1.mp3; umpv file2.mp3&lt;/code> will append &lt;code>file2.mp3&lt;/code> to the original umpv window&amp;rsquo;s queue.&lt;/p>
&lt;figure>
 &lt;a href="./images/mpv_lyrics.png">
 &lt;img src="./images/mpv_lyrics.png" alt="Screenshot of mpv displaying lyrics">
 &lt;/a>&lt;figcaption>&lt;code>mpv&lt;/code> displaying lyrics&lt;/figcaption>&lt;/figure>&lt;p>I use the &lt;a href="https://github.com/gokcehan/lf">&lt;strong>&lt;code>lf&lt;/code>&lt;/strong>&lt;/a> terminal file manager to manage and browse all my files, including music files.
Previews for each file are generated using a script I wrote called &lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/file_previewer.sh">&lt;strong>&lt;code>file_previewer.sh&lt;/code>&lt;/strong>&lt;/a>:
if the album cover is available, it&amp;rsquo;s displayed in the terminal using &lt;a href="https://hpjansson.org/chafa/">&lt;code>chafa&lt;/code>&lt;/a>&lt;sup id="fnref:7">&lt;a href="#fn:7" class="footnote-ref" role="doc-noteref">7&lt;/a>&lt;/sup> &amp;mdash; otherwise, a waveform image is generated using &lt;a href="https://ffmpeg.org/">&lt;code>ffmpeg&lt;/code>&lt;/a> and also displayed with &lt;code>chafa&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln">51&lt;/span>&lt;span class="cl"> audio/*&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">52&lt;/span>&lt;span class="cl"> &lt;span class="c1"># attempt to preview the audio file&amp;#39;s embedded image&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">53&lt;/span>&lt;span class="cl"> &lt;span class="c1"># if no embedded image, attempt to preview a file named &amp;#39;cover.*&amp;#39; in the same directory&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">54&lt;/span>&lt;span class="cl"> &lt;span class="c1"># else, generate a waveform image and display it&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">55&lt;/span>&lt;span class="cl"> exiftool -Picture -b &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$file&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> chafa --size &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">$((&lt;/span>&lt;span class="nv">$width&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="m">4&lt;/span>&lt;span class="k">))&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>x&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$height&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="o">||&lt;/span> chafa cover.* --size &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">$((&lt;/span>&lt;span class="nv">$width&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="m">4&lt;/span>&lt;span class="k">))&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>x&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$height&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="o">||&lt;/span> ffmpeg -i &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$file&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> -filter_complex &lt;span class="s2">&amp;#34;showwavespic=s=1280x720:colors=pink&amp;#34;&lt;/span> -frames:v &lt;span class="m">1&lt;/span> -f image2pipe -vcodec png - &lt;span class="p">|&lt;/span> chafa --size &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">$((&lt;/span>&lt;span class="nv">$width&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="m">4&lt;/span>&lt;span class="k">))&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>x&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$height&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">56&lt;/span>&lt;span class="cl"> exiftool &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$file&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="p">|&lt;/span> bat --theme&lt;span class="o">=&lt;/span>&lt;span class="s1">&amp;#39;base16&amp;#39;&lt;/span> --terminal-width &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">$((&lt;/span>&lt;span class="nv">$width&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="m">4&lt;/span>&lt;span class="k">))&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> --force-colorization&lt;span class="p">;;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;figure>
 &lt;a href="./images/lf_album_art.png">
 &lt;img src="./images/lf_album_art.png" alt="Screenshot of lf displaying the cover image for an album">
 &lt;/a>&lt;figcaption>&lt;code>lf&lt;/code> with album art preview&lt;/figcaption>&lt;/figure>&lt;figure>
 &lt;a href="./images/lf_waveform.png">
 &lt;img src="./images/lf_waveform.png" alt="Screenshot of lf displaying the audio waveform in the absence of a cover image">
 &lt;/a>&lt;figcaption>&lt;code>lf&lt;/code> with audio waveform preview in the absence of cover image&lt;/figcaption>&lt;/figure>&lt;p>I&amp;rsquo;ve also written a number of scripts to play music in different ways over the years, although I only really use the last script listed here now:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>&lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/music_dmenu.sh">&lt;code>music_dmenu.sh&lt;/code>&lt;/a>:&lt;/strong> creates a hierarchical &lt;a href="https://tools.suckless.org/dmenu/">&lt;code>dmenu&lt;/code>&lt;/a> prompt based on the files in your music directory which allows you to play a specific artist, album, or track.
Cumbersome to use, as playing a particular track requires you to select &lt;code>Track&lt;/code> in the first prompt, then the artist, then the album, and then the track itself &amp;mdash; probably only really useful for playing a particular artist as you would just select &lt;code>Artist&lt;/code> and then the artist&amp;rsquo;s name.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>&lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/play_music.sh">&lt;code>play_music.sh&lt;/code>&lt;/a>:&lt;/strong> plays an album based off a given artist name and album name.
Usage: &lt;code>play_music.sh &amp;quot;Fontaines D.C.&amp;quot; &amp;quot;(2022) Skinty Fia&amp;quot;&lt;/code>.
This worked well before I started including release years in the filepath, as while it&amp;rsquo;s not especially unreasonable to require the user to know the precise artist name and album name, it&amp;rsquo;s a bit much to expect them to know the album&amp;rsquo;s release year as well, and so I don&amp;rsquo;t really use it anymore.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>&lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/fzfplay.sh">&lt;code>fzfplay.sh&lt;/code>&lt;/a>:&lt;/strong> uses &lt;a href="https://github.com/sharkdp/fd">&lt;code>fd&lt;/code>&lt;/a> (a faster&lt;sup id="fnref:8">&lt;a href="#fn:8" class="footnote-ref" role="doc-noteref">8&lt;/a>&lt;/sup> alternative to the POSIX &lt;a href="https://www.man7.org/linux/man-pages/man1/find.1.html">&lt;code>find&lt;/code>&lt;/a>) to list all the tracks in the music directory&lt;sup id="fnref:9">&lt;a href="#fn:9" class="footnote-ref" role="doc-noteref">9&lt;/a>&lt;/sup>, and allows the user to search them with &lt;a href="https://github.com/junegunn/fzf">&lt;code>fzf&lt;/code>&lt;/a> (a command-line fuzzy finder).
For me, this is not only far superior to the other two scripts mentioned here, it&amp;rsquo;s superior to any other method of enqueueing music I&amp;rsquo;ve encountered in any other application &amp;mdash; its speed and convenience are unparalleled.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="cp">#!/bin/sh
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="c1"># Script that allows a music file or directory to be selected via `fzf` to be enqueued with `umpv`&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="nv">media_directory&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$HOME&lt;/span>&lt;span class="s2">/media/music&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">fd --base-directory &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$media_directory&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="se">&lt;/span> --exclude &lt;span class="s2">&amp;#34;*.jpg&amp;#34;&lt;/span> --exclude &lt;span class="s2">&amp;#34;*.png&amp;#34;&lt;/span> --exclude &lt;span class="s2">&amp;#34;*.lrc&amp;#34;&lt;/span> &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="se">&lt;/span> --absolute-path &lt;span class="p">|&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> fzf &lt;span class="p">|&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> xargs -I&lt;span class="o">{}&lt;/span> sh -c &lt;span class="s1">&amp;#39;umpv &amp;#34;{}&amp;#34; &amp;amp;&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>I bound this script to a hotkey using &lt;a href="https://github.com/baskerville/sxhkd">&lt;code>sxhkd&lt;/code>&lt;/a> (my preferred hotkey manager for X), and use this to play my music 90% of the time &amp;mdash; the remaining 10% being via &lt;code>lf&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="ln">219&lt;/span>&lt;span class="cl">super + control + shift + m
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">220&lt;/span>&lt;span class="cl"> alacritty --class &amp;#34;fzfplay.sh&amp;#34; --command sh -c &amp;#34;nohup fzfplay.sh &amp;gt; /dev/null&amp;#34;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;figure>
 &lt;a href="./images/fzfplay.png">
 &lt;img src="./images/fzfplay.png" alt="Screenshot of fzfplay.sh">
 &lt;/a>&lt;figcaption>&lt;code>fzfplay.sh&lt;/code>&lt;/figcaption>&lt;/figure>&lt;p>I have the idea in my head to (probably at some point in the near future) update this script to allow the user to search by lyrics as well, not unlike the search bar on Spotify.
I&amp;rsquo;m unsure as of yet whether I would want this functionality united in the same script for convenience, or if I would prefer to have it isolated to its own &amp;ldquo;search-by-lyrics&amp;rdquo; script:
at present, I&amp;rsquo;m leaning towards the latter, as it would be both simpler and more UNIXy, but also because it would circumvent the issue of weighting lyrics versus titles in search.
What I mean by this is that a unified search interface would have decide whether to weight a matching title or lyric more heavily (i.e., treat it as more relevant), which would turn this program from a handy little script to an open research question in the field of Information Retrieval.&lt;/p>
&lt;ul>
&lt;li>The most likely approach would be to weight the title more heavily: e.g., if a user were to search &amp;ldquo;i love you&amp;rdquo;, the song &lt;em>I Love You&lt;/em> by Fontaines D.C. would be more likely to be a relevant result than any of the other thousands of songs that contain that string.&lt;/li>
&lt;li>You could probably take it as an axiom that given that song titles are forced to be quite short and are often chosen with some view towards uniqueness or recognisability, whereas song lyrics have no such length constraints and have far less of a focus on uniqueness (though, of course, still have some focus on uniqueness), that the likelihood of a any given sub-string occurring in a song title is far lower than the likelihood of it occurring in a song&amp;rsquo;s lyrics, and that therefore its presence in a song&amp;rsquo;s title is more meaningful for providing relevant search results.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="sharing-music-between-devices">Sharing Music Between Devices&lt;/h2>
&lt;p>I use &lt;a href="https://syncthing.net/">Syncthing&lt;/a> for sharing music between my laptop and my phone;
on my phone, I use the Android Syncthing client &lt;a href="https://github.com/researchxxl/syncthing-android">Syncthing-Fork&lt;/a> (set to open directly to the web view window, as I find that the most intuitive interface).
Because my music collection consists of FLAC files, it&amp;rsquo;s rather large and therefore takes up a lot of storage space:
it was for this reason that one of my primary &amp;ldquo;must-haves&amp;rdquo; for my most recent smartphone purchase was 256Gb of storage.
In the future, I may look into compressing my music before syncing it to other devices, and using the FLAC files primarily for archival purposes, or on my home computer where I can attach additional storage without having to worry about transporting it.
For the time being, my music collection does still fit on my phone, so I can worry about that another day.&lt;/p>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>Besides the following research papers which substantiate this claim, this is intuitively self-evident: someone who listens to the Dead Kennedys is likely to have anarcho-lefist views, a Tom MacDonald listener is likely to have conservative views, a Wolfe Tones listener is likely to have Irish republican views &amp;mdash;&lt;sup id="fnref1:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup> and so on, and doubly so for inferring one&amp;rsquo;s religion if they tend to listen to Orthodox chants or Islamic &lt;em>nasheeds&lt;/em>.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:2">
&lt;p>I swear that this is an organic em-dash, not a GPT-generated one&lt;sup id="fnref:10">&lt;a href="#fn:10" class="footnote-ref" role="doc-noteref">10&lt;/a>&lt;/sup>.&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&amp;#160;&lt;a href="#fnref1:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:3">
&lt;p>&lt;a href="https://support.spotify.com/us/article/audio-quality/">https://support.spotify.com/us/article/audio-quality/&lt;/a>&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:4">
&lt;p>Spotify does give you a way to address its lack of certain tracks with its &lt;a href="https://support.spotify.com/us/article/local-files/">Local Files feature&lt;/a> which allows you to not only listen to files you have stored locally on your devices, but to add them to playlists and to download these playlists onto other devices under the same account, where the tracks will be playable even if you never manually copied the local files to that device.
I made extensive use of this feature when I primarily used Spotify.&amp;#160;&lt;a href="#fnref:4" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:5">
&lt;p>The most notable example of me doing this was removing the repeated lines &lt;em>When Richard one calm summer night / put a bullet through his head&lt;/em> before a breakdown from the &lt;a href="https://www.youtube.com/watch?v=yO3VhLYSWgE">Tiny Little Houses song&lt;/a> based upon the Edwin Arlington Robinson poem &lt;a href="https://www.poetryfoundation.org/poems/44982/richard-cory">&lt;em>Richard Cory&lt;/em>&lt;/a>, the repetition of which I felt destroyed much of the impact of the original poem, as in the original poem the final line very much strikes the reader out of nowhere and then ends abruptly in silence.&amp;#160;&lt;a href="#fnref:5" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:6">
&lt;p>&lt;a href="https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file">https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file&lt;/a>&amp;#160;&lt;a href="#fnref:6" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:7">
&lt;p>I will most likely stop using &lt;code>chafa&lt;/code> in favour of switching to a terminal emulator with graphics support; at present, I use &lt;a href="https://alacritty.org/">Alacritty&lt;/a> for its simplicity, but I would probably be better of using a terminal emulator with graphics/Sixel support such as &lt;a href="https://sw.kovidgoyal.net/kitty/">kitty&lt;/a>.&amp;#160;&lt;a href="#fnref:7" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:8">
&lt;p>&lt;a href="https://github.com/sharkdp/fd#benchmark">https://github.com/sharkdp/fd#benchmark&lt;/a>&amp;#160;&lt;a href="#fnref:8" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:9">
&lt;p>My implementation actually just finds all the files that are not JPEGs, PNGs, or LRC files, which is fine in the case of my music directory, as I know it won&amp;rsquo;t contain any other filetypes. An alternative approach would be to search specifically for MP3 files, FLAC files, etc., but this approach would exclude any tracks in a non-specified file format: I&amp;rsquo;ve prioritised track filetype flexibility over guaranteed exclusion of non-music files.
A more robust approach would be to check the MIME type of each file and to only use the ones which are of the &lt;strong>type&lt;/strong> &lt;code>audio&lt;/code>, but I consider this to be prohibitively computationally expensive for my use-case, as it would require actual reading of the contents of each file rather than just filename-based filtering.&amp;#160;&lt;a href="#fnref:9" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:10">
&lt;p>Have you ever seen someone put a footnote on a punctuation mark like that before? I haven&amp;rsquo;t. Would be even weirder if someone put yet another footnote within that footnote&amp;hellip;&amp;#160;&lt;a href="#fnref:10" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item><item><title>`inutility_scripts`: Scripts That Don't Do Anything Useful</title><link>https://ohaodha.ie/blog/inutility_scripts-scripts-that-dont-do-anything-useful/</link><pubDate>Fri, 01 Aug 2025 15:45:45 +0100</pubDate><guid>https://ohaodha.ie/blog/inutility_scripts-scripts-that-dont-do-anything-useful/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>Roughly two years ago now, I created my &lt;a href="https://github.com/0x4A0D4A/utility_scripts">&lt;code>utility_scripts&lt;/code>&lt;/a> repository on GitHub, which contains all the scripts that I&amp;rsquo;ve written for use on my GNU/Linux laptop.
These scripts are generally short, useful scripts, that make basic tasks on my computer easier, ranging from creating dynamic menus to select &lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/bluetooth_dmenu.pl">Bluetooth devices&lt;/a> or &lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/wifi_dmenu.sh">WiFi networks&lt;/a> to connect to, to listing the &lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/bspwm_window_count.sh">count of open windows on a workspace&lt;/a> or &lt;a href="https://github.com/0x4A0D4A/utility_scripts/blob/main/src/bluetooth_info.pl">connected Bluetooth devices&lt;/a> in the status bar of my desktop, and more.
Originally a copy of my &lt;code>~/scripts/&lt;/code> directory, the repository now lives with all my other GitHub repositories at &lt;code>~/code/github/0x4A0D4A/utility_scripts/&lt;/code> and &lt;code>~/code/scripts/&lt;/code> is symlinked to the &lt;code>utility_scripts/src&lt;/code> directory, and is in my &lt;code>$PATH&lt;/code>, so any of these scripts are accessible as commands on my system.&lt;/p>
&lt;p>However, I eventually found that the &lt;code>utility_scripts&lt;/code> repository was restrictive in content:
it was restricted to containing only &lt;em>useful&lt;/em> scripts, and I had a number of scripts that I wanted to share that I couldn&amp;rsquo;t in good faith describe as &amp;ldquo;useful&amp;rdquo;, and so I created a new repository named &lt;code>inutilty_scripts&lt;/code>: a collection of miscellaneous scripts that don&amp;rsquo;t do anything useful.
These scripts are ones that I consider interesting (often proofs of concepts), but that I have yet to find any practical use for.
Some of these scripts are entirely useless, but others demonstrate some interesting concept that I think &lt;em>could&lt;/em> potentially be useful if the right situation arose.&lt;/p>
&lt;h2 id="inutility_scripts">&lt;code>inutility_scripts&lt;/code>&lt;/h2>
&lt;h3 id="recursivesh">&lt;code>recursive.sh&lt;/code>&lt;/h3>
&lt;p>&lt;a href="https://github.com/0x4A0D4A/inutility_scripts/blob/main/src/recursive.sh">&lt;code>recursive.sh&lt;/code>&lt;/a> is a script that calls itself, recursively.
While I can&amp;rsquo;t think of any uses for this, I don&amp;rsquo;t find it particularly difficult to imagine that a recursive shell script &lt;em>could&lt;/em> be useful for some task or another, as recursion has many useful applications with regards to function calls.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="cp">#!/bin/sh
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="c1"># A script that calls itself&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="nv">recursion_count&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="si">${&lt;/span>&lt;span class="nv">1&lt;/span>&lt;span class="k">:-&lt;/span>&lt;span class="nv">0&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Recursion number &lt;/span>&lt;span class="nv">$recursion_count&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">&lt;span class="nv">$0&lt;/span> &lt;span class="k">$((&lt;/span>recursion_count &lt;span class="o">+&lt;/span> &lt;span class="m">1&lt;/span>&lt;span class="k">))&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;figure>
 &lt;a href="./images/recursive.png">
 &lt;img src="./images/recursive.png" alt="Screenshot of the output of recursive.sh">
 &lt;/a>&lt;figcaption>Output of &lt;code>./recursive.sh&lt;/code>&lt;/figcaption>&lt;/figure>&lt;p>This script also makes use of some interesting POSIX shell features:
to demonstrate that recursion is indeed occurring, the script prints the &lt;code>recursion_count&lt;/code> for each invocation, using parameter expansion &amp;amp; default value substitution to set the value of the &lt;code>recursion_count&lt;/code> variable to the first positional argument supplied to the script invocation (&lt;code>$1&lt;/code>), if present, or &lt;code>0&lt;/code> if &lt;code>$1&lt;/code> is unset.
It also exploits the fact that the &lt;code>$0&lt;/code> variable contains the filename with which the shell was invoked&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup>, i.e., the script file itself, and uses the value of that variable as a command within the script.&lt;/p>
&lt;figure>
 &lt;a href="./images/recursive_limit.png">
 &lt;img src="./images/recursive_limit.png" alt="/bin/sh: warning: shell level (1000) too high, resetting to 1">
 &lt;/a>&lt;figcaption>&lt;code>/bin/sh: warning: shell level (1000) too high, resetting to 1&lt;/code>&lt;/figcaption>&lt;/figure>&lt;p>Something interesting also happens when the recursion count hits 996:
a message is displayed which says &lt;code>/bin/sh: warning: shell level (1000) too high, resetting to 1&lt;/code>.
The variable &lt;code>$SHLVL&lt;/code> is incremented for each new instance of the shell spawned&lt;sup id="fnref:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup>, and thus tells you the current level of shell nesting.
If you run &lt;code>echo $SHLVL&lt;/code> in an open terminal window, you&amp;rsquo;ll probably get either &lt;code>1&lt;/code> or &lt;code>2&lt;/code> as your output:&lt;/p>
&lt;ul>
&lt;li>&lt;code>1&lt;/code> will be your output if this shell session is the first one spawned for your session, that is, a shell wasn&amp;rsquo;t automatically spawned when you logged in;&lt;/li>
&lt;li>&lt;code>2&lt;/code> will be your output if a login shell was automatically started when your user was logged in.&lt;/li>
&lt;/ul>
&lt;p>On my system, I get an output of &lt;code>3&lt;/code>: one of these shells being my login shell, the other being the terminal window that I have open, and the third probably being spawned by the &lt;code>zsh&lt;/code> plugins I source in my configuration, although I&amp;rsquo;ve never bothered to actually track down the what exactly is causing it.
This explains why the recursion count hits &lt;code>996&lt;/code> before the shell level reset warning is displayed:
the shell level is reset to &lt;code>1&lt;/code> once it hits &lt;code>1000&lt;/code>, and 1,000 shells can be accounted for by adding the 3 that we know are extant when a terminal window is opened on my system, and the 997 shells spawned by the &lt;code>recursive.sh&lt;/code> script (recalling that the first call of the script the &lt;code>$recursion_count&lt;/code> is &lt;code>0&lt;/code> so the number of shells is the &lt;code>$recursion_count&lt;/code> plus 1).&lt;/p>
&lt;h3 id="self_destructsh">&lt;code>self_destruct.sh&lt;/code>&lt;/h3>
&lt;p>&lt;a href="https://github.com/0x4A0D4A/inutility_scripts/blob/main/src/self_destruct.sh">&lt;code>self_destruct.sh&lt;/code>&lt;/a> is the simplest of inutility scripts, and simply deletes itself.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="cp">#!/bin/sh
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="c1"># A script that deletes itself&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">rm &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$0&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;figure>
 &lt;a href="./images/self_destruct.png">
 &lt;img src="./images/self_destruct.png" alt="Screenshot of the before &amp;amp; after of self_destruct.sh">
 &lt;/a>&lt;figcaption>Before &amp;amp; after of &lt;code>./self_destruct.sh&lt;/code>&lt;/figcaption>&lt;/figure>&lt;p>Similar to &lt;code>recursive.sh&lt;/code>, it exploits the &lt;code>$0&lt;/code> variable, and simply deletes the file at the filepath contained within that variable.
Since the script is under Git version control, it can easily be brought back after execution (in both senses of the word) by running &lt;code>git restore self_destruct.sh&lt;/code>.&lt;/p>
&lt;p>I can imagine a potential use for this self-destructive functionality being to ensure that a script is ran precisely once and never again, or perhaps to try and cover the tracks of a malware attack by deleting the malicious file as soon as possible to avoid detection.&lt;/p>
&lt;p>Interestingly, it&amp;rsquo;s possible to delete a script while it&amp;rsquo;s executing without interrupting execution at all.
Originally, I had assumed that, since shell scripts are only interpreted line-by-line (with a few exceptions), deleting a script while it&amp;rsquo;s executing would halt execution, so when I discovered that this was not the case, I imagined that this meant that the entire script was read into memory before execution, but this isn&amp;rsquo;t the case either&amp;hellip;&lt;/p>
&lt;p>Most people are probably aware that deleting a file on a UNIX system (or pretty much any computer for that matter) doesn&amp;rsquo;t actually destroy the data contained within that file:
instead, deleting a file removes the &lt;strong>directory entry&lt;/strong>, which is the mapping of the filename to its &lt;strong>inode&lt;/strong>&lt;sup id="fnref:3">&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref">3&lt;/a>&lt;/sup> which is the underlying data structure used by the file system to store the file&amp;rsquo;s metadata and pointers to the data blocks at which the actual file data is stored on disk.
When there are no longer any filenames (or &lt;em>links&lt;/em>) pointing to the inode, the file is considered unreachable and data blocks are marked as &lt;strong>free&lt;/strong>, allowing them to be deallocated &amp;amp; overwritten.&lt;/p>
&lt;p>However, if a process has a file open, it continues to have a reference to that file&amp;rsquo;s inode, regardless of file deletion or renaming.
Thus, the process can continue to read from that file even after it has no extant links in the file system.
The deleted file&amp;rsquo;s data blocks cannot be deallocated until the process has closed the open file descriptor.&lt;/p>
&lt;p>I wrote a short script called &lt;code>test.sh&lt;/code> to demonstrate this:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="cp">#!/bin/sh
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="cp">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 1&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 2&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 3&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 4&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 5&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 6&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 7&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 8&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 9&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Step 10&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>(Obviously, this script would make more sense as a &lt;code>for&lt;/code>-loop if it served some practical purpose, but the point here is to demonstrate that lines that had not yet been reached when the file was deleted are still executed, which would not be demonstrated if a &lt;code>for&lt;/code>-loop was used.)
If the script is deleted mid-execution, we can see that execution is not interrupted:&lt;/p>
&lt;figure>
 &lt;a href="./images/test.png">
 &lt;img src="./images/test.png" alt="Screenshot of test.sh being deleted mid-execution yet continuing to run unaffected">
 &lt;/a>&lt;figcaption>Screenshot of &lt;code>test.sh&lt;/code> being deleted mid-execution yet continuing to run unaffected&lt;/figcaption>&lt;/figure>&lt;p>We can also use &lt;a href="https://www.man7.org/linux/man-pages/man8/lsof.8.html">&lt;code>lsof(8)&lt;/code>&lt;/a> to list the open files on the system to see that the executing process retains a file descriptor for the deleted file:&lt;/p>
&lt;figure>
 &lt;a href="./images/lsof.png">
 &lt;img src="./images/lsof.png" alt="Screenshot of the output of lsof | grep test.sh after deletion of test.sh mid-execution">
 &lt;/a>&lt;figcaption>Output of &lt;code>lsof | grep test.sh&lt;/code> after deletion of &lt;code>test.sh&lt;/code> mid-execution&lt;/figcaption>&lt;/figure>&lt;p>Thus we know that a shell script will continue to execute even if its source file is deleted, because it retains an open file descriptor for that file from which it can read its contents until termination.&lt;/p>
&lt;h3 id="disappearingsh">&lt;code>disappearing.sh&lt;/code>&lt;/h3>
&lt;p>&lt;a href="https://github.com/0x4A0D4A/inutility_scripts/blob/main/src/disappearing.sh">&lt;code>disappearing.sh&lt;/code>&lt;/a> is a script that disappears from the filesystem (or, more precisely, the directory entries) during execution and re-appears once it has finished executing.
It is something of a natural follow-on from &lt;code>self_destruct.sh&lt;/code> in that it is a script that deletes its own source code, and also because it exploits the fact that a script will continue to execute uninterrupted even if its source file is deleted.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="cp">#!/bin/sh
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="c1"># Script that deletes its source code while executing and replaces it when exiting, hence &amp;#34;disappearing&amp;#34; while running and re-appearing once finished&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="nv">file_contents&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="k">$(&lt;/span>cat &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$0&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">rm &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$0&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="k">for&lt;/span> i in &lt;span class="k">$(&lt;/span>seq &lt;span class="m">1&lt;/span> 10&lt;span class="k">)&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Doing something invisible&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$file_contents&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &amp;gt; &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$0&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> chmod +x &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$0&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Like &lt;code>self_destruct.sh&lt;/code>, it exploits &lt;code>$0&lt;/code> to delete its own source code, but it also uses &lt;code>$0&lt;/code> to read its source code into a variable called &lt;code>file_contents&lt;/code>, which it then &lt;code>echo&lt;/code>es into the filepath defined by &lt;code>$0&lt;/code> and makes said file executable before finally exiting.&lt;/p>
&lt;figure>
 &lt;a href="./images/disappearing.png">
 &lt;img src="./images/disappearing.png" alt="Screenshot of disappearing.sh being executed, including the output of ls before, after, and during its execution">
 &lt;/a>&lt;figcaption>Output of &lt;code>disappearing.sh&lt;/code>, including the output of &lt;code>ls&lt;/code> before, after, and during its execution&lt;/figcaption>&lt;/figure>&lt;p>Of course, the script hasn&amp;rsquo;t truly disappeared from the filesystem, only the directory entries, as the remaining open file descriptor can be seen with the use of &lt;code>lsof&lt;/code>:&lt;/p>
&lt;figure>
 &lt;a href="./images/grep_disappearing.png">
 &lt;img src="./images/grep_disappearing.png" alt="Screenshot of the output of lsof | grep disappearing.sh">
 &lt;/a>&lt;figcaption>Screenshot of the output of &lt;code>lsof | grep disappearing.sh&lt;/code>&lt;/figcaption>&lt;/figure>&lt;h3 id="suicidalsh">&lt;code>suicidal.sh&lt;/code>&lt;/h3>
&lt;p>&lt;a href="https://github.com/0x4A0D4A/inutility_scripts/blob/main/src/suicidal.sh">&lt;code>suicidal.sh&lt;/code>&lt;/a> is a script that kills itself, that is, its own process.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="cp">#!/bin/sh
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="c1"># A script that kills the process that&amp;#39;s executing it, i.e. itself&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">pkill &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">$(&lt;/span>basename &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$0&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">)&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Similar in &amp;ldquo;function&amp;rdquo; (I use the word lightly) to &lt;code>self_destruct.sh&lt;/code>, it also uses the &lt;code>$0&lt;/code> variable;
it finds the name of the script by running the &lt;a href="https://www.man7.org/linux/man-pages/man3/basename.3.html">&lt;code>basename&lt;/code>&lt;/a> command with this &lt;code>$0&lt;/code> variable, and uses &lt;a href="https://linux.die.net/man/1/pkill">&lt;code>pkill&lt;/code>&lt;/a> to kill any process executing with that name, i.e., itself.&lt;/p>
&lt;figure>
 &lt;a href="./images/suicidal.png">
 &lt;img src="./images/suicidal.png" alt="Screenshot of the output of ./suicidal.sh">
 &lt;/a>&lt;figcaption>Output of &lt;code>./suicidal.sh&lt;/code>&lt;/figcaption>&lt;/figure>&lt;p>Try as I might, I cannot think of a single situation in which this would be useful;
one might think that this could potentially have some use in killing a script if some error is encountered, but there&amp;rsquo;s nothing that this approach achieves that the &lt;code>exit&lt;/code> command wouldn&amp;rsquo;t.
Perhaps, in a very contrived situation, you might have a script that must only ever have one instance of itself running at a time, and if it is detected that there are multiple instances running, they will all need to be killed to prevent some catastrophic scenario, and in that situation this snippet would be useful;
however, I would suggest that a mutex or semaphore of some kind would likely be a far better solution to prevent this situation from arising.&lt;/p>
&lt;h3 id="trippysh">&lt;code>trippy.sh&lt;/code>&lt;/h3>
&lt;p>&lt;a href="https://github.com/0x4A0D4A/inutility_scripts/blob/main/src/trippy.sh">&lt;code>trippy.sh&lt;/code>&lt;/a> is, in my opinion, by far the least useful script in this repository: it simply writes random data to the user&amp;rsquo;s display in an infinite loop, creating visuals that a hip and cool youngster such as myself&lt;sup id="fnref:4">&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref">4&lt;/a>&lt;/sup> might describe as &amp;ldquo;trippy&amp;rdquo;.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="cp">#!/bin/sh 
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="c1"># Script to display random data to the screen. To be ran from a TTY, as a user who has write privileges to the fb0 device&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="k">while&lt;/span> &lt;span class="o">[&lt;/span> &lt;span class="nb">true&lt;/span> &lt;span class="o">]&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl"> cat /dev/urandom &amp;gt; /dev/fb0
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It makes use of the &lt;code>/dev/urandom&lt;/code> device as a source of random data which, unlike its sister &lt;code>/dev/random&lt;/code>, never blocks, even if entropy is low, which makes it more suitable for our purposes&lt;sup id="fnref:5">&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref">5&lt;/a>&lt;/sup>.
It also makes use of the &lt;strong>frame buffer device&lt;/strong> &lt;code>/dev/fb0&lt;/code>, which provides an abstraction for the graphics hardware, allowing software to write data to be displayed on a graphics device in a hardware-independent manner;
this is the interface used by the X11 graphics server to control the user&amp;rsquo;s display.&lt;/p>
&lt;p>Running the script requires write privileges to the &lt;code>/dev/fb0&lt;/code> device (typically held only by the root user) and that there is no GUI session that is overriding the frame buffer, like an X11 server.
Therefore, the script cannot be executed from an ordinary terminal window within a graphical session and must instead by run from the TTY.
The GIF below is a screen recording of the script being executed on an Alpine Linux virtual machine in QEMU:&lt;/p>
&lt;figure>
 &lt;a href="./images/trippy.gif">
 &lt;img src="./images/trippy.gif" alt="Animated GIF of the output of ./trippy.sh">
 &lt;/a>&lt;figcaption>Output of ./trippy.sh&lt;/figcaption>&lt;/figure>&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>&lt;a href="https://www.man7.org/linux/man-pages//man1/bash.1.html">&lt;code>bash(1)&lt;/code>&lt;/a> § &lt;span style="font-variant: small-caps">arguments&lt;/span>&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:2">
&lt;p>&lt;a href="https://www.man7.org/linux/man-pages//man1/bash.1.html">&lt;code>bash(1)&lt;/code>&lt;/a> § Shell Variables&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:3">
&lt;p>The etymology of the term is forgotten: see &lt;a href="https://en.wikipedia.org/wiki/Inode#Etymology">inode - Wikipedia § Etymology&lt;/a>&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:4">
&lt;p>&amp;ldquo;Hip and cool&amp;rdquo; youngsters are famously known to refer to themselves as such.&amp;#160;&lt;a href="#fnref:4" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:5">
&lt;p>On modern systems, the behaviour of &lt;code>/dev/random&lt;/code> and &lt;code>/dev/urandom&lt;/code> are virtually indistinguishable. Historically, &lt;code>/dev/random&lt;/code> would block if there was not sufficient entropy in the system to generate sufficiently random data, whereas &lt;code>/dev/urandom&lt;/code> would re-use its internal state along with a &lt;strong>cryptographically secure pseudo-random number generator (CSPRNG)&lt;/strong> to avoid blocking. In modern systems, however, &lt;code>/dev/random&lt;/code> usually blocks at startup until sufficient entropy has been gathered, and then unblocks permanently, making its behaviour past boot indistinguishable from &lt;code>/dev/urandom&lt;/code>.&amp;#160;&lt;a href="#fnref:5" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item><item><title>The Perils of `sudo vim`</title><link>https://ohaodha.ie/blog/the-perils-of-sudo-vim/</link><pubDate>Fri, 11 Jul 2025 13:14:53 +0100</pubDate><guid>https://ohaodha.ie/blog/the-perils-of-sudo-vim/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>On a UNIX-like system, one often has the need to edit a write-restricted file and thus to escalate one&amp;rsquo;s privileges to do so;
the obvious approach that might initially occur to you is to use &lt;code>sudo vim&lt;/code> or &lt;code>sudo $EDITOR&lt;/code> to edit the file with superuser privileges, but this is inadvisable for a number of reasons, the majority of which boil down to the &lt;a href="https://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of least privilege&lt;/a>: root privileges are needed only for writing to the file &amp;mdash; there is no need to run the entire editor process as &lt;code>root&lt;/code>.&lt;/p>
&lt;p>Throughout this blog post, I make repeated reference to Vim as it is the most popular terminal text editor, but the points discussed in this blog post are true for any text editor, terminal or otherwise, if being used to edit write-protected files.&lt;/p>
&lt;h2 id="the-risks">The Risks&lt;/h2>
&lt;p>On a single-user system, such as a personal computer, running your text editor as &lt;code>root&lt;/code> isn&amp;rsquo;t a massive problem, and is &lt;em>probably&lt;/em> fine&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> 99.99% of the time;
however, you&amp;rsquo;ll find that running &lt;code>sudo vim&lt;/code> will execute Vim with the default configuration and none of your personalisation specified in your &lt;code>~/&lt;/code> directory, as these configurations are not associated with your &lt;code>root&lt;/code> user.
You can either simply do without these configurations (which I would strongly recommend), or copy your configuration files into your &lt;code>root&lt;/code> user directory (which I would strongly discourage):
if you have any plugins installed that execute in the background as you use your editor, these will be executed as &lt;code>root&lt;/code> also, meaning that you&amp;rsquo;re putting a lot of faith in the authors of these plugins to assume that it&amp;rsquo;s safe to execute their programs as &lt;code>root&lt;/code> on your computer.
The risk here is not just that there may be some malicious code hidden in the plugin:
it&amp;rsquo;s equally possible that a simple bug or mistake in the program, that would ordinarily be of little consequence, could cause catastrophic damage to your system as the error executed with root privileges.&lt;/p>
&lt;p>The real risk, however, associated with &lt;code>sudo vim&lt;/code> is on a multi-user system:
&lt;code>sudo&lt;/code> configuration, in keeping with the principle of least privilege, allows a system administrator to specify certain commands that a user may run with root privileges, while preventing them from running other commands as &lt;code>root&lt;/code>;
therefore, if a user just needs to edit a few write-protected files for configuration purposes, it might be tempting to give them &lt;code>sudo&lt;/code> privileges with which to run the &lt;code>vim&lt;/code> command, but this introduces a massive privilege escalation vulnerability.
Now that you&amp;rsquo;ve given the user the ability to execute &lt;code>vim&lt;/code> as &lt;code>root&lt;/code>, any other process that they spawn from within &lt;code>vim&lt;/code> will also be running as &lt;code>root&lt;/code>:
if they simply enter the Vim command &lt;code>:term&lt;/code> to spawn an interactive shell within Vim, they will then have full, unrestricted access to a &lt;code>root&lt;/code> shell on your system, something which cybersecurity experts describe as &amp;ldquo;very bad&amp;rdquo;.&lt;/p>
&lt;figure>
 &lt;a href="./images/term.png">
 &lt;img src="./images/term.png" alt="The :term command being used to spawn a shell as root within Vim">
 &lt;/a>&lt;figcaption>The &lt;code>:term&lt;/code> command being used to spawn a shell as &lt;code>root&lt;/code> within Vim&lt;/figcaption>&lt;/figure>&lt;h2 id="the-alternative-sudoedit">The Alternative: &lt;code>sudoedit&lt;/code>&lt;/h2>
&lt;p>Fortunately, there is a very simple solution to these problems that I&amp;rsquo;ve raised: &lt;a href="https://www.man7.org/linux/man-pages/man8/sudoedit.8.html">&lt;code>sudoedit&lt;/code>&lt;/a>.
Instead of running your text editor as &lt;code>root&lt;/code>, &lt;code>sudoedit&lt;/code> copies the file that you want to edit to a temporary location and opens that file with your default text editor (usually specified by &lt;code>$EDITOR&lt;/code> or &lt;code>$VISUAL&lt;/code>), with your normal user privileges, and once you have quit and are finished editing, copies the modified file to overwrite the original.
Thus, you can edit write-protected files with all your text editor configurations continuing to work as usual, and you prevent any privilege escalation of the processes running within that editor, and you prevent the users of that editor from executing arbitrary commands as &lt;code>root&lt;/code>.&lt;/p>
&lt;p>Something to be careful of, however, when using &lt;code>sudoedit some_file&lt;/code> (or its alias &lt;code>sudo -e some_file&lt;/code>), is that writing the contents of your buffer (such as entering &lt;code>:w&lt;/code> in Vim) will only write the contents to the temporary file location, and will not apply them to the actual file you wish to edit;
to apply the changes, you must write your changes &lt;em>and&lt;/em> quit the editor (&lt;code>:wq&lt;/code> or &lt;code>ZQ&lt;/code> in Vim);
for example, it has happened to me in the past that I&amp;rsquo;ve been using &lt;code>sudoedit /etc/hosts&lt;/code> and expecting my changes to apply in real-time, but when I&amp;rsquo;ve tested my new configuration, nothing has changed, because I did not close the editor once I was finished making my changes &amp;mdash; my changes had been saved to the temporary file with &lt;code>:w&lt;/code>, but had not been applied to the actual &lt;code>/etc/hosts&lt;/code> file.&lt;/p>
&lt;h2 id="what-if-i-use-doas">What if I use &lt;code>doas&lt;/code>?&lt;/h2>
&lt;p>As a former &lt;a href="https://man.openbsd.org/doas">&lt;code>doas&lt;/code>&lt;/a> contrarian myself (and likely future &lt;code>doas&lt;/code> contrarian when I eventually abandon Arch Linux), I&amp;rsquo;m sympathetic to the plight of the &lt;code>doas&lt;/code> users who, in the true spirit of Free &amp;amp; Open Source Software, make their lives needlessly far more difficult for symbolic purposes, in this case, in the battle against &amp;ldquo;bloat&amp;rdquo;.
&lt;code>doas&lt;/code>, being truly minimalistic, has no such alternative to &lt;code>sudoedit&lt;/code>, but you still shouldn&amp;rsquo;t run &lt;code>doas vim&lt;/code>:
instead, you should perform the same steps that &lt;code>sudoedit&lt;/code> performs yourself, and copy the original file to a temporary location, edit that temporary file, and overwrite the original with the temporary file when you&amp;rsquo;re finished.
There are shell scripts which automate this for you online (&lt;a href="https://github.com/TinfoilSubmarine/doasedit">&lt;code>github.com/TinfoilSubmarine/doasedit&lt;/code>&lt;/a>), but I would recommend against using such options, or making your own:
the reduced attack surface given by the minimalism of &lt;code>doas&lt;/code> is rendered null &amp;amp; void if you start increasing the attack surface with random scripts you find online, and you&amp;rsquo;d be far better off just using &lt;code>sudo&lt;/code> at that point.&lt;/p>
&lt;h2 id="addendum">Addendum&lt;/h2>
&lt;p>After publishing this post, the thought occurred to me that I had neglected to mention what I consider to be the easiest workaround for when you&amp;rsquo;ve opened a file and edited it without root privileges in Vim, and now you cannot save it because it&amp;rsquo;s write-protected.
My advice on the matter may be obvious to some, but I&amp;rsquo;ve found myself taking needlessly complicated workarounds for this issue in the past, and have witnessed others doing similar, so I thought it&amp;rsquo;s best that I share how to quickly solve this problem.&lt;/p>
&lt;figure>
 &lt;a href="./images/readonly.png">
 &lt;img src="./images/readonly.png" alt="Error displayed when a file is readonly in Vim">
 &lt;/a>&lt;figcaption>Error displayed when a file is &lt;code>readonly&lt;/code> in Vim&lt;/figcaption>&lt;/figure>&lt;p>Obvious though it may seem, the best approach to solving this issue, especially if you have just finished making lengthy &amp;amp; complex changes to the file, is to simply enter &lt;code>:w some_temp_file&lt;/code>, where &lt;code>some_temp_file&lt;/code> is just a filepath that you know you have write privileges for, and then quit and run &lt;code>sudo cp some_temp_file original_file&lt;/code> to overwrite the original target file with your new changes.&lt;/p>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>I would not be surprised if the words &amp;ldquo;probably fine&amp;rdquo; have preceded every major cybersecurity incident of the past 20 years.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item><item><title>Tor as a Tool for Testing Fonts</title><link>https://ohaodha.ie/blog/tor-as-a-tool-for-testing-fonts/</link><pubDate>Thu, 13 Feb 2025 00:00:00 +0000</pubDate><guid>https://ohaodha.ie/blog/tor-as-a-tool-for-testing-fonts/</guid><description>&lt;p>A concern that may occur to you when you are designing a website is whether or not the font that you are using is &amp;ldquo;web-safe&amp;rdquo;, meaning that the user will be able to render it on their end when they load your webpage because they already have that font on their device.
Web-safe fonts include the typical well-known fonts, like Times New Roman, Arial, etc.
If you are using non-web-safe fonts on your website, typically you will either define a fallback font in the CSS that is used if the user does not have the font in question and/or supply the user with the font using some kind of web font delivery service like &lt;a href="https://fonts.google.com/">Google Fonts&lt;/a> or &lt;a href="https://www.fontsquirrel.com/">Font Squirrel&lt;/a> or by packaging the font directly with the website.&lt;/p>
&lt;p>I am, at present, using two different non-web-safe fonts for this website that I must supply the user with:&lt;/p>
&lt;ul>
&lt;li>&lt;span style="font-family: EB Garamond">EB Garamond&lt;/span>, which I supply using a font delivery service;&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup>&lt;/li>
&lt;li>&lt;span style="font-family: Gadelica">Gadelica&lt;/span>, which I supply the font files for directly bundled with the website.&lt;/li>
&lt;/ul>
&lt;p>Testing whether or not these fonts are being distributed correctly can be difficult, as I already have these fonts installed on my system, so they will render correctly even if the website is not distributing them correctly to the user.
There are a couple different ways to test this, such as using a different device, but one way that I&amp;rsquo;ve found to be particularly convenient is to use the &lt;a href="https://www.torproject.org/download/">Tor Browser&lt;/a>.&lt;/p>
&lt;p>The Tor Browser is a browser that intends to offer a level of anonymity and security when browsing the Internet.
The main way in which it provides this is by routing the user&amp;rsquo;s traffic through a number of different nodes to obfuscate where the user&amp;rsquo;s traffic is coming from, but it also employs a number of anti-browser-fingerprinting techniques to prevent the user&amp;rsquo;s computer from being recognised, including managing the window dimensions and &lt;em>sandboxing the user&amp;rsquo;s fonts&lt;/em>;
because a computer could be potentially identified by the unique combination of fonts installed on the machine, a malicious website could &lt;em>fingerprint&lt;/em> or identify a user based on the fonts available to them.
Therefore, the Tor Browser comes with a number of generic pre-bundled fonts and does not have access to any of your system fonts, making it harder to fingerprint you &lt;em>and&lt;/em>, incidentally, working as a handy tool for checking how web-safe a font is.&lt;/p>
&lt;p>If you open your website in the Tor Browser, and the fonts render as expected, you know the fonts are being supplied correctly; if the fonts do not render correctly, you not that they are not being supplied correctly.
I personally use this test all the time when setting up a website, as it&amp;rsquo;s one of the handiest and simplest ways I know to test such a thing.&lt;/p>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>This is no longer true as of 2025-04-12: EB Garamond is now also directly bundled with the site, both in an effort to de-Google and as the original EB Garamond has superior font features to that offered on Google Fonts.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item><item><title>Cool qutebrowser Customisations</title><link>https://ohaodha.ie/blog/cool-qutebrowser-customisations/</link><pubDate>Fri, 19 Apr 2024 20:03:27 +0100</pubDate><guid>https://ohaodha.ie/blog/cool-qutebrowser-customisations/</guid><description>&lt;h2 id="what-is-qutebrowser">What is qutebrowser?&lt;/h2>
&lt;p>Qutebrowser&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> is a keyboard-focused, minimal web browser written in Python.
It makes use of Vim-like keybindings, and is surprisingly fast &amp;amp; memory-efficient considering the language in which it is
written: it
generally starts up faster than the &lt;a href="https://brave.com/">Brave&lt;/a> browser on my machine, and consumes less memory than
&lt;a href="https://www.mozilla.org/en-US/firefox/">Firefox&lt;/a> (which I&amp;rsquo;ve had memory leak issues with in the past).&lt;/p>
&lt;p>It has a minimal graphical user interface, and some primitive built-in adblocking which tends to work quite well, with
the exception of YouTube advertisements.
It is configured via plaintext Python files, making it highly &amp;amp; easily configurable.
It also has the built-in capability to run JavaScript userscripts, making its behaviour even more flexible &amp;amp;
customisable.&lt;/p>
&lt;h2 id="configurations-in-configpy">Configurations in &lt;code>config.py&lt;/code>&lt;/h2>
&lt;p>I&amp;rsquo;ll skip over the &lt;del>boring&lt;/del> common configurations that have been covered to death by others before me here, such as
turning on dark mode or launching the current URL in &lt;code>mpv&lt;/code> and skip to configurations that I think have something interesting to them.&lt;/p>
&lt;h3 id="video-speed-controls">Video Speed Controls&lt;/h3>
&lt;p>Most video platforms only allow you to speed up a video to $2\times$ speed, which I often find to be not as fast as
I would like.
I rarely watch a video on lower than $2\times$ speed, unless the timing of the video is important, such as music or
comedy, and often want to watch videos on $3\times$ or $4\times$ speed.&lt;/p>
&lt;p>The following lines of code, when added to the qutebrowser &lt;code>config.py&lt;/code> configuration file, make the up &amp;amp; down arrow keys
into video speed controllers which increase the speed of videos on the page by quarter-increments at a time, and allows
speeds in excess of the typical limit $2\times$ to be achieved:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># control video speed&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="n">config&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">bind&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;&amp;lt;Up&amp;gt;&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;jseval document.querySelector(&amp;#39;video&amp;#39;).playbackRate += 0.25;&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="n">config&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">bind&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;&amp;lt;Down&amp;gt;&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;jseval document.querySelector(&amp;#39;video&amp;#39;).playbackRate -= 0.25;&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> 
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="clear-notifications-messages">Clear Notifications (Messages)&lt;/h3>
&lt;p>The above speed controls generate messages at the bottom of the screen that show what speed has been set, which I find
useful, but if I hit the button several times they can build up and take a while to disappear, so the following
keybinding clears them:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="n">config&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">bind&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;cm&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;clear-messages&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> 
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="find-missing-webpages">Find Missing Webpages&lt;/h3>
&lt;p>If a webpage I visit is missing or has been changed, I hit the key combination &lt;code>,w&lt;/code> to find it on the Wayback Machine:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># search for the current URL in archive.org&amp;#39;s wayback machine&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="n">config&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">bind&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;,w&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;open http://web.archive.org/&lt;/span>&lt;span class="si">{url}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="load-the-current-page-in-firefox">Load the Current Page in Firefox&lt;/h3>
&lt;p>If a webpage I visit doesn&amp;rsquo;t work properly in qutebrowser (as is sometimes, but rarely, the case) I can open the tab in
Firefox:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># keybind to open current url in firefox &lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="n">config&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">bind&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;&amp;lt;Ctrl+Alt+t&amp;gt;&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;spawn -d firefox &lt;/span>&lt;span class="si">{url}&lt;/span>&lt;span class="s2"> ;; tab-close&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> 
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="userscripts">Userscripts&lt;/h2>
&lt;h3 id="redirectors">Redirectors&lt;/h3>
&lt;p>I use a number of alternative frontends for various social media sites to avoid having to make an account, and have a
number of scripts that redirect each social media page to the alternative frontend.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="c1">// @name reddit to libreddit redirector
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="c1">// @include /^https?:\/\/(www\.)?reddit\.com\/.*$/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="c1">// @run-at document-start
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==/UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">alt&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;libreddit.kavin.rocks&amp;#34;&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">replace&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/(www.)?reddit.com/gi&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">alt&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;redirected reddit.com to &amp;#34;&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="nx">alt&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="c1">// @name tiktok to proxitok redirector
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="c1">// @include /^https?:\/\/(www\.)?tiktok\.com\/.*$/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1">// @run-at document-start
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==/UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">replace&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/(www\.)?tiktok.com/gi&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;proxitok.pabloferreiro.es&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;redirected tiktok.com to proxitok.pabloferreiro.es&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="c1">// @name twitter to nitter redirector
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="c1">// @include /^https?:\/\/(www\.)?twitter\.com\/.*$/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1">// @run-at document-start
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==/UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">replace&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/(www.)?twitter.com/gi&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;nitter.poast.org&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;redirected twitter.com to nitter.poast.org&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="c1">// @name youtube to invidious redirector
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="c1">// @include /^https?:\/\/(www\.)?youtube\.com\/.*$/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1">// @run-at document-start
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==/UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nx">location&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">href&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">replace&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sr">/(www\.)?youtube.com/gi&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;yewtu.be&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;redirected youtube.com to yewtu.be&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="smart-video-speed-reset">Smart Video Speed Reset&lt;/h3>
&lt;p>As I mentioned above, I typically have my video speeds set to $2\times$ or above except for videos in which the
timing is important, such as music or comedy.
The following userscript detects the category of a YouTube video and resets the speed to normal speed.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="c1">// @name normal speed music
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="c1">// @description make music &amp;amp; comedy youtube videos normal speed (for timing purposes)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="c1">// @include /^https?:\/\/(www\.)?y\.com\.sb\/watch\?v=.+$/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="c1">// @include /^https?:\/\/(www\.)?yewtu\.be\/watch\?v=.+$/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="c1">// @run-at document-idle
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="c1">// ==/UserScript==
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="nb">document&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">getElementById&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;genre&amp;#34;&lt;/span>&lt;span class="p">).&lt;/span>&lt;span class="nx">innerHTML&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">includes&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;Music&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">||&lt;/span> &lt;span class="nb">document&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">getElementById&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;genre&amp;#34;&lt;/span>&lt;span class="p">).&lt;/span>&lt;span class="nx">innerHTML&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">includes&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;Comedy&amp;#34;&lt;/span>&lt;span class="p">))&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="nb">document&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">querySelector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;video&amp;#34;&lt;/span>&lt;span class="p">).&lt;/span>&lt;span class="nx">playbackRate&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Note that the above script is written to work on Invidious YouTube frontends rather than YouTube itself, although it could easily
be adapted for YouTube by just adding it to the &lt;code>@include&lt;/code> section.&lt;/p>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>&lt;a href="https://qutebrowser.org/">https://qutebrowser.org/&lt;/a>&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item><item><title>How to Download Videos with No “Download” Button from Canvas</title><link>https://ohaodha.ie/blog/how-to-download-videos-with-no-download-button-from-canvas/</link><pubDate>Mon, 26 Feb 2024 16:58:00 +0000</pubDate><guid>https://ohaodha.ie/blog/how-to-download-videos-with-no-download-button-from-canvas/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>Normally, when videos such as lecture recordings are posted to Canvas, there is a &amp;ldquo;Download &lt;code>file.mp4&lt;/code>&amp;rdquo; or similar
link available on the page, or at the very least you can right-click on the playing video and select &amp;ldquo;Save media&amp;rdquo;.
Sometimes, however, there is no such link available, which is quite annoying for people like me who want to hoard
lecture content.
Below is the workaround that I&amp;rsquo;ve found to work for downloading such videos from Canvas.
It&amp;rsquo;s not quite as streamlined as I would like it to be, so suggestions and/or feedback would be appreciated, but it
works for me.&lt;/p>
&lt;p>The instructions should be browser and operating system independent, but note that I have only tested them on Arch Linux
using qutebrowser &amp;amp; Firefox.
The instructions also assume that the file on Canvas is an &lt;code>mp4&lt;/code> file, an assumption that I have not been able to verify
the reliability of: I have never encountered a non-&lt;code>mp4&lt;/code> video on Canvas so it&amp;rsquo;s possible that it stores videos
exclusively as &lt;code>mp4&lt;/code> files, but it&amp;rsquo;s also equally possible that I&amp;rsquo;ve simply coincidentally only ever encountered &lt;code>mp4&lt;/code>
files and that other video formats are supported.&lt;/p>
&lt;h2 id="steps">Steps&lt;/h2>
&lt;ol>
&lt;li>
&lt;p>Open Canvas in your web browser and navigate to the video file that you want to download.
Before going any further, I suggest you double-check that there is no download button and that you
can&amp;rsquo;t right-click on the vide and select &amp;ldquo;Save media&amp;rdquo; to save yourself the trouble of the following steps.&lt;/p>
&lt;figure>
 &lt;img src="images/canvas_video_with_download_options.png" alt="Canvas Video with Download Options">
 &lt;figcaption>Canvas Video with Download Options&lt;/figcaption>
&lt;/figure>
&lt;figure>
 &lt;img src="images/canvas_video_without_download_options.png" alt="Canvas Video without Download Options">
 &lt;figcaption>Canvas Video without Download Options&lt;/figcaption>
&lt;/figure>
&lt;/li>
&lt;li>
&lt;p>Open your browser&amp;rsquo;s Inspector window by right-clicking anywhere on the page and selecting &amp;ldquo;Inspect&amp;rdquo; and navigate to
the &amp;ldquo;Network&amp;rdquo; tab of the Inspector window.&lt;/p>
&lt;figure>
 &lt;img src="images/network_tab_of_inspector_window.png" alt="Network Tab of the Inspector Window">
 &lt;figcaption>Network Tab of the Inspector Window&lt;/figcaption>
&lt;/figure>
&lt;/li>
&lt;li>
&lt;p>Enter the term &lt;code>mp4&lt;/code> into the &amp;ldquo;Filter&amp;rdquo; box (called &amp;ldquo;Filter URLs&amp;rdquo; on Firefox) to only display network results
relating to the video itself.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Click on the video to start playing it.
A number of network requests will be displayed in the Inspector window; we are interested in the last one.&lt;/p>
&lt;figure>
 &lt;img src="images/filter_mp4_network_tab_of_inspector_window.png" alt="Network Requests Containing the String 'mp4'">
 &lt;figcaption>Network Requests Containing the String &lt;code>mp4&lt;/code>&lt;/figcaption>
&lt;/figure>
&lt;/li>
&lt;li>
&lt;p>Right-click on the last result displayed in the Inspector window, and hover the mouse over the &amp;ldquo;Copy&amp;rdquo; option in the
drop-down menu (labelled &amp;ldquo;Copy Value&amp;rdquo; on Firefox).
This is where the steps will diverge depending on your operating system.
If you are on a UNIX-like system such as GNU/Linux or MacOS, click the &amp;ldquo;Copy as cURL&amp;rdquo; option in the sub-menu.
If you are on Windows, click the &amp;ldquo;Copy as PowerShell&amp;rdquo; option.&lt;/p>
&lt;figure>
 &lt;img src="images/copy_network_request_as_curl.png" alt="Copy Network Request as cURL">
 &lt;figcaption>Copy Network Request as cURL&lt;/figcaption>
&lt;/figure>
&lt;/li>
&lt;li>
&lt;p>Open a terminal emulator (or PowerShell on a Windows machine) and paste the command you copied into the command
prompt.
If you are using &lt;code>cURL&lt;/code>, append &lt;code>-o file_name.mp4&lt;/code> (or &lt;code>--output file_name.mp4&lt;/code>) to the command before hitting
return, substituting &lt;code>file_name.mp4&lt;/code> for the name that you wish to give the file.
The download directory can be specified by prepending the path to that directory to the file name, otherwise it
will default to your home directory.&lt;/p>
&lt;figure>
 &lt;img src="images/run_curl_command_canvas.png" alt="Run the cURL Command">
 &lt;figcaption>Run the cURL Command&lt;/figcaption>
&lt;/figure>
&lt;/li>
&lt;/ol></description></item></channel></rss>