Difference between revisions of "Hacks"

From Jon's Wiki
 
(9 intermediate revisions by the same user not shown)
Line 17: Line 17:
 
=== Fix the daily commit bug ===
 
=== Fix the daily commit bug ===
  
Unfortunately since about 18.04 there is a bug where a systemd timer and a cron.daily task compete to commit a daily commit if needed; unfortunately one of them doesn't bother to check the {{path|etckeeper.conf}} file to see whether you've disabled it. If you have {{path|DISABLE_DAILY_AUTOCOMMITS=1}} in your config file, then you probably don't want daily auto commits. If this is so, run this to disable the systemd timer:
+
Unfortunately since about 18.04 there is a bug where a systemd timer and a cron.daily task compete to commit a daily commit if needed; unfortunately one of them doesn't bother to check the {{path|etckeeper.conf}} file to see whether you've disabled it. If you have <code>DISABLE_DAILY_AUTOCOMMITS=1</code> in your config file, then you probably don't want daily auto commits. If this is so, run this to disable the systemd timer:
  
 
  systemctl disable --now etckeeper.timer
 
  systemctl disable --now etckeeper.timer
Line 49: Line 49:
 
Spring-clean your gpg database, make it go faster. Just go:
 
Spring-clean your gpg database, make it go faster. Just go:
  
  gpg --delete-keys `gpg --list-keys|grep -P 'revoked|expired'|awk -F/ '{print $2}'|awk '{print $1}'`
+
  gpg --delete-keys $(gpg --list-public-keys|grep -PB1 'expired|revoked'|grep '^\s')
  
 
Obviously. If you can't be arsed confirming each key, add <tt>--batch --yes</tt> to the first gpg call.
 
Obviously. If you can't be arsed confirming each key, add <tt>--batch --yes</tt> to the first gpg call.
Line 84: Line 84:
 
== Turn off the shitty default "visual" mouse mode in vim ==
 
== Turn off the shitty default "visual" mouse mode in vim ==
  
Christ. Add <tt>set mouse-=a</tt> to ~/.vimrc
+
Add this to <code>/etc/vim/vimrc.local</code> file:<ref>Böhnke, H (16 March 2019). [https://unix.stackexchange.com/a/506723 Answer 506723] on "Disabling mouse support in vim in a gnome-terminal environment", Unix & Linux Stack Exchange.</ref>
 +
 
 +
" Use skip_defaults_vim from the master /etc/vim/vimrc file
 +
if ! exists('skip_defaults_vim')
 +
  " Source the defaults file manually from here
 +
  source $VIMRUNTIME/defaults.vim
 +
endif
 +
 +
" Avoid loading the defaults twice
 +
let g:skip_defaults_vim = 1
 +
 +
" Revert any unwanted changes the defaults file introduced
 +
set mouse=
  
 
== Too many leftover git branches merged into master? Nukem! ==
 
== Too many leftover git branches merged into master? Nukem! ==
Line 114: Line 126:
 
  sudo apt install cuetools
 
  sudo apt install cuetools
 
  cuetag cdimage.cue *.flac
 
  cuetag cdimage.cue *.flac
 +
 +
== Ditch Snap ==
 +
 +
=== 1. Install Firefox properly ===
 +
 +
Ubuntu 22.04 replaces the Firefox Debian package with a pseudo-package that installs it using Snap. This is no longer optional, and it is also stupid, annoying, slow, and potentially insecure for many reasons.<ref>Brandon Hopkins. [https://techhut.tv/flatpak-vs-snap-vs-appimage/ "Flatpak vs. Snap vs. AppImage - Linux Packaging Benchmarks"] TechHut, April 2022.</ref> Several Firefox extensions that need to talk to other applications don't currently work in Snap installed Firefox, including things like Zotero and Keepass.
 +
 +
It also completely fails to find and import any existing Firefox profile. Snap apps for whatever stupid reason keep settings in a hard-coded ~/snap folder. Who thought that was a good idea? What's wrong with ~/.config and/or ~/.local, or couldn't they even use ~/.snap? Good grief.
 +
 +
Luckily, we can use the Mozilla Team PPA instead.<ref>Ji M. [https://ubuntuhandbook.org/index.php/2022/04/install-firefox-deb-ubuntu-22-04/ "How to Install Latest Firefox as classic Deb in Ubuntu 22.04"] Ubuntu Handbook, April 2022.</ref> In summary:
 +
<syntaxhighlight lang="bash">
 +
# Remove Firefox entirely:
 +
snap remove firefox
 +
apt remove firefox
 +
 +
# Add the Mozilla Team PPA and install:
 +
add-apt-repository ppa:mozillateam/ppa
 +
apt update
 +
apt install -t 'o=LP-PPA-mozillateam' firefox
 +
 +
# Pin to the Mozilla Team PPA so we never get the Snap pseudo-package
 +
echo 'Package: firefox*
 +
Pin: release o=LP-PPA-mozillateam
 +
Pin-Priority: 501' > /etc/apt/preferences.d/mozillateam-ppa.pref
 +
</syntaxhighlight>
 +
 +
=== 2. Remove snap ===
 +
 +
You can also remove snap entirely as follows:
 +
 +
<syntaxhighlight lang="bash">
 +
# get a list of things installed with snap:
 +
snap list
 +
 +
# remove them one by one:
 +
snap uninstall ''program1''
 +
snap uninstall ''program2''
 +
 +
# once there's nothing left, remove snap:
 +
apt purge snapd
 +
 +
# then prevent it ever being re-installed with a pin preference:
 +
echo 'Package: snapd
 +
Pin: release a=*
 +
Pin-Priority: -10' > /etc/apt/preferences.d/no-snap.pref
 +
</syntaxhighlight>
 +
 +
==Fancy PostgreSQL prompts==
 +
 +
For a coloured prompt including what user, connection, and host you're on, stick this in ~/.psqlrc or /etc/postgresql-common/psqlrc:
 +
 +
\set PROMPT1 '%[%033[1m%](%n@%[%033[1;35m%]%M%[%033[m%]%[%033[1m%] on %[%033[1;31m%]%`hostname`%[%033[m%]%[%033[1m%]) \n%/%R%#%[%033[m%] '
 +
\set PROMPT2 '%[%033[1m%]%/%R%#%[%033[m%] '
 +
\set PROMPT3 '%[%033[1m%]>>%[%033[m%] '
 +
 +
Escape codes use '%' character, documented here: [https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PROMPTING Psql Prompts].
 +
 +
==Using rsync with FAT volumes (USB sticks and SD cards)==
 +
 +
You might have noticed that all your files have today's timestamp on them when using rsync thus:
 +
rsync -av /path/to/my/stuff /mnt/usb-stick/
 +
 +
The -a option means -rlDtpog, which is recursive, include links and devices, preserving timestamps, permissions, owner and group. Which is what you want most of the time. Trouble is, FAT partitions have no concept of permissions, owners and groups, so you need to use:
 +
rsync -rtlDv /path/to/my/stuff /mnt/usb-stick/
 +
 +
But that's probably not all, because rsync will probably complain that it can't set the timestamp. You'll need to mount your USB stick so it ignores a user check that FAT can't enforce:
 +
mount -o remount,allow_utime=2 /mnt/usb-stick
 +
 +
==Notes==
 +
 +
<references/>

Latest revision as of 21:10, 16 December 2024

Image renaming by create date

When you have a bunch of files from your old Android phone with a timestamp filename (e.g. 1388042328550.jpg), you can rename them thus:

for f in 1*.jpg; do
  mv $f $(date +IMG_%Y%m%d_%H%M%S.jpg -d @$(echo $f|cut -c 1-10))
done

Otherwise, we can fish out the Create Date from the EXIF data, using exiftool:

exiftool '-FileName<CreateDate' -d %Y%m%d_%H%M%S%%-c.%%e .

Use etckeeper

It saves your bacon. Store changes to the /etc directory in git, uses hooks to commit changes when packages are updated, puppet runs, etc. Stash your own changes with useful commit messages. Look up stuff in history when something goes wrong, figure out what changes happened when packages are added or updated.

Fix the daily commit bug

Unfortunately since about 18.04 there is a bug where a systemd timer and a cron.daily task compete to commit a daily commit if needed; unfortunately one of them doesn't bother to check the etckeeper.conf file to see whether you've disabled it. If you have DISABLE_DAILY_AUTOCOMMITS=1 in your config file, then you probably don't want daily auto commits. If this is so, run this to disable the systemd timer:

systemctl disable --now etckeeper.timer

Repair broken or rotated phone videos

You can fix broken video .tmp files from your Android phone using untrunc. This will create a fixed.mp4 file in cwd:

untrunc working_video_from_the_same_camera.mp4 broken.mk4.tmp

Did your phone unhelpfully rotate your video? Fix the metadata:

ffmpeg -i original.mp4 -acodec copy -vcodec copy -metadata:s:v:0 rotate=0 unrotated.mp4

Or, did you actually film your shit the wrong way up? You'll need to re-encode it:

ffmpeg -i original.mp4 -acodec copy -vcodec libx264 -crf 20 -metadata:s:v:0 rotate=0 unrotated.mp4

Export your photos

Before uploading your photos, remove all the EXIF tags, and reduce the resolution; nobody needs 5000x4000 pixels, much less corporations hoovering up your stuff to put in adverts.

mkdir tmp
for f in $.jpg; do
  convert -resize 1600x1200 $f tmp/$f
  exiftool -all= -P -overwrite_original tmp/$f
done

Clean out your revoked and expired PGP keys

Spring-clean your gpg database, make it go faster. Just go:

gpg --delete-keys $(gpg --list-public-keys|grep -PB1 'expired|revoked'|grep '^\s')

Obviously. If you can't be arsed confirming each key, add --batch --yes to the first gpg call.

Firefox opening your shit in gedit

I don't know, who thought that was a good idea? Jesus.

sed -i 's#.*octet-stream.*#application/octet-stream=xdg-open#' ~/.local/share/applications/mimeapps.list ~/.config/mimeapps.list

GNOME not sorting your folders first

It's not in a dialog anywhere, and a pretty lame default behaviour.

dconf write /org/gnome/nautilus/preferences/sort-directories-first true

GNOME not dealing with WebP images

I mean, it's 2021 and WebP is only what, ten years old? Jesus. See solution on AskUbuntu.

Restoring huge databases to PostgreSQL

See also: PostgreSQL

Skip some huge tables by dumping the TOC, removing culprit large tables, and restore with the edited TOC, e.g.

pg_restore -l huge.dump > TOC
# edit the TOC file...
pg_restore -L TOC -d dbname huge.dump

Output PostgreSQL queries to CSV

COPY (SELECT <query> ...) TO STDOUT CSV HEADER;

Turn off the shitty default "visual" mouse mode in vim

Add this to /etc/vim/vimrc.local file:[1]

" Use skip_defaults_vim from the master /etc/vim/vimrc file
if ! exists('skip_defaults_vim')
  " Source the defaults file manually from here
  source $VIMRUNTIME/defaults.vim
endif

" Avoid loading the defaults twice
let g:skip_defaults_vim = 1

" Revert any unwanted changes the defaults file introduced
set mouse=

Too many leftover git branches merged into master? Nukem!

This will remove any branches on your local git repo that have been merged to master.

for i in $(for b in $(git branch|grep -v '\*'); do git branch $b --merged master; done); \
    do git branch -D $i; \
done

Move your MySQL databases

Used to be you could move your /var/lib/mysql directory somewhere else and symlink it. You can, but in newer distros with Apparmor you'll need to declare it:

# In /etc/apparmor.d/tunables/alias
alias /var/lib/mysql/ -> /mnt/wherever/mysql/,

Note the trailing comma.

Split audio CD rips using the cue file

Single-file FLAC or Ape files of a CD can be split up using the .cue file for both the timestamps and the tagging data. Also, install flacon (from a PPA).

sudo apt install shntool
shnsplit -o flac cdimage.flac -f cdimage.cue -t '%n.%t'

You may need to do tags again, if they didn't stick:

sudo apt install cuetools
cuetag cdimage.cue *.flac

Ditch Snap

1. Install Firefox properly

Ubuntu 22.04 replaces the Firefox Debian package with a pseudo-package that installs it using Snap. This is no longer optional, and it is also stupid, annoying, slow, and potentially insecure for many reasons.[2] Several Firefox extensions that need to talk to other applications don't currently work in Snap installed Firefox, including things like Zotero and Keepass.

It also completely fails to find and import any existing Firefox profile. Snap apps for whatever stupid reason keep settings in a hard-coded ~/snap folder. Who thought that was a good idea? What's wrong with ~/.config and/or ~/.local, or couldn't they even use ~/.snap? Good grief.

Luckily, we can use the Mozilla Team PPA instead.[3] In summary:

# Remove Firefox entirely:
snap remove firefox
apt remove firefox

# Add the Mozilla Team PPA and install:
add-apt-repository ppa:mozillateam/ppa
apt update
apt install -t 'o=LP-PPA-mozillateam' firefox

# Pin to the Mozilla Team PPA so we never get the Snap pseudo-package
echo 'Package: firefox*
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 501' > /etc/apt/preferences.d/mozillateam-ppa.pref

2. Remove snap

You can also remove snap entirely as follows:

# get a list of things installed with snap:
snap list

# remove them one by one:
snap uninstall ''program1''
snap uninstall ''program2''

# once there's nothing left, remove snap:
apt purge snapd

# then prevent it ever being re-installed with a pin preference:
echo 'Package: snapd
 Pin: release a=*
 Pin-Priority: -10' > /etc/apt/preferences.d/no-snap.pref

Fancy PostgreSQL prompts

For a coloured prompt including what user, connection, and host you're on, stick this in ~/.psqlrc or /etc/postgresql-common/psqlrc:

\set PROMPT1 '%[%033[1m%](%n@%[%033[1;35m%]%M%[%033[m%]%[%033[1m%] on %[%033[1;31m%]%`hostname`%[%033[m%]%[%033[1m%]) \n%/%R%#%[%033[m%] '
\set PROMPT2 '%[%033[1m%]%/%R%#%[%033[m%] '
\set PROMPT3 '%[%033[1m%]>>%[%033[m%] '

Escape codes use '%' character, documented here: Psql Prompts.

Using rsync with FAT volumes (USB sticks and SD cards)

You might have noticed that all your files have today's timestamp on them when using rsync thus:

rsync -av /path/to/my/stuff /mnt/usb-stick/

The -a option means -rlDtpog, which is recursive, include links and devices, preserving timestamps, permissions, owner and group. Which is what you want most of the time. Trouble is, FAT partitions have no concept of permissions, owners and groups, so you need to use:

rsync -rtlDv /path/to/my/stuff /mnt/usb-stick/

But that's probably not all, because rsync will probably complain that it can't set the timestamp. You'll need to mount your USB stick so it ignores a user check that FAT can't enforce:

mount -o remount,allow_utime=2 /mnt/usb-stick

Notes

  1. Böhnke, H (16 March 2019). Answer 506723 on "Disabling mouse support in vim in a gnome-terminal environment", Unix & Linux Stack Exchange.
  2. Brandon Hopkins. "Flatpak vs. Snap vs. AppImage - Linux Packaging Benchmarks" TechHut, April 2022.
  3. Ji M. "How to Install Latest Firefox as classic Deb in Ubuntu 22.04" Ubuntu Handbook, April 2022.