Delete Badly Named Files

SYNOPSYS

sometimes, for whatever reason, you end up with bad files with terrible names like ‘–’.
this is how you do to erase them.

PROCEDURE

/etc/config/file.conf
1
2
3
$ ls -li
655464 -rw-r--r--  1 user user  889 Apr 28 09:25 --
$ find . -maxdepth 1 -inum 655464 -delete

Trash My Trash

SYNOPSYS

I don’t want no trash, it sucks my free space down.

PROCEDURE

1
2
rm -rf ~/.local/share/Trash/*
sudo chattr +i ~/.local/share/Trash

Mdp - Markdown Presentation Tool

SYNOPSYS

I love it soooo much mdp.

SCHEMATICS

markdow + binary => lovely ncurses based presentation

PROCEDURE

Systemd-networkd

SYNOPSYS

I have 2 network interfaces and very simple routing needs.
Let’s try systemd-networkd.

SCHEMATICS

systemd-networkd must be running,
links can be configured through *.link files.
network is configured through *.network files.

PROCEDURE

First replace dhcpcd.service with systemd-networkd network configuration.

/etc/systemd/network/en.network
1
2
3
4
5
[Match]
Name=en*

[Network]
DHCP=v4

Then build a unit to mess with routing tables.

/etc/systemd/system/route.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=Route tunning
Wants=network-online.target
After=network-online.target
 
[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/route.conf
ExecStart=/usr/bin/ip route del default via ${gateway}
ExecStart=/usr/bin/ip route add ${network} dev ${etx}
ExecStop=/usr/bin/ip route add default via ${gateway}
ExecStop=/usr/bin/ip route del ${network} dev ${eth}

[Install]
WantedBy=multi-user.target

Finally, the big switch.

switch from dhcp to systemd-networkd
1
2
3
4
systemctl disable dhcpcd.service
systemctl enable systemd-networkd
systemctl enable systemd-networkd-wait-online.service
systemctl enable route.service

Mutt Archives Search

SYNOPSYS

This is how I file my old mails, and what I use to search in.

PROCEDURE

First I archive the mail of the previous year.

1
2
3
4
5
Y=$(date +%Y)
PY=$(($Y - 1))
for I in maildirs; do
    archivemail -D $Y-01-01 --preserve-unread --no-compress --suffix=-$PY -o ~/mail_path/archives/ ~/mail_path/$I
done

Then I can index them using mairix

~/.mairixrc
1
2
3
4
5
base=~/mail_path/archives
maildir=*
mbox=*
mfolder=~/mail_path/search
database=~/mail_path/.mairix_database

I hav the following in my muttrc to search the archives using mairix.

~/.muttrc
1
macro index \eb 'c=search'<enter><shell-escape>'mairix ' 'Enter Mairix search term'

Alsa Pulseaudio Setup

SYNOPSYS

Here are the basis of alsa + pulseaudio configuration.

PROCEDURE

First I try to control the kernel module loading and initilasation order. alsa.opensrc.org

/etc/modprobe.d/alsa.conf
1
2
options snd_usb_audio index=-2
options snd-hda-intel id=PCH,HDMI index=0,1

but this produces weird results

aplay -l
1
2
3
4
5
6
card 0: PCH [HDA Intel HDMI], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: HDMI [HDA Intel PCH], device 0: ALC3220 Analog [ALC3220 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

what I can do in a reliable way is to disable the HDMI.

/etc/modprobe.d/alsa.conf
1
2
3
4
# no usb as first card
options snd_usb_audio index=-2
# disable HDMI that shows up first, enable PCH
options snd_hda_intel enable=0,1
aplay -l
1
2
3
card 0: PCH [HDA Intel PCH], device 0: ALC3220 Analog [ALC3220 Analog]
  Subdevices: 1/1
    Subdevice #0: subdevice #0

Whatever, to correctly setup pulseaudio defaults.
pulseaudio perfect setup
man pulse-cli-syntax.

Identify your preferred card and profile.

pacmd list-cards (shortened)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2 card(s) available.
index: 0
  name: <alsa_card.pci-0000_00_03.0>
  driver: <module-alsa-card.c>
  profiles:
      output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400, available: unknown)
      output:hdmi-surround: Digital Surround 5.1 (HDMI) Output (priority 300, available: unknown)
      off: Off (priority 0, available: unknown)
  active profile: <output:hdmi-stereo>
index: 1
  name: <alsa_card.pci-0000_00_1b.0>
  driver: <module-alsa-card.c>
  profiles:
      input:analog-stereo: Analog Stereo Input (priority 60, available: unknown)
      output:analog-stereo: Analog Stereo Output (priority 6000, available: unknown)
      output:analog-stereo+input:analog-stereo: Analog Stereo Duplex (priority 6060, available: unknown)
      off: Off (priority 0, available: unknown)
  active profile: <output:analog-stereo+input:analog-stereo>

Then your preferred port.

pacmd list-sinks (shortened)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2 sink(s) available.
index: 0
  name: <alsa_output.pci-0000_00_03.0.hdmi-stereo>
  driver: <module-alsa-card.c>
  card: 0 <alsa_card.pci-0000_00_03.0>
  ports:
      hdmi-output-0: HDMI / DisplayPort (priority 5900, latency offset 0 usec, available: no)
  active port: <hdmi-output-0>
index: 1
  name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
  driver: <module-alsa-card.c>
  card: 1 <alsa_card.pci-0000_00_1b.0>
  ports:
      analog-output: Analog Output (priority 9900, latency offset 0 usec, available: unknown)
      analog-output-lineout: Line Out (priority 9900, latency offset 0 usec, available: yes)
      analog-output-speaker: Speakers (priority 10000, latency offset 0 usec, available: unknown)
      analog-output-headphones: Headphones (priority 9000, latency offset 0 usec, available: no)
  active port: <analog-output>

try pulse config using pacmd

1
2
echo "set-card-profile alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo" | pacmd
echo "set-sink-port alsa_output.pci-0000_00_1b.0.analog-stereo analog-output-headphones" | pacmd

set pulse config permanently

/etc/pulse/default.conf
1
2
3
...
set-card-profile alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo
set-sink-port alsa_output.pci-0000_00_1b.0.analog-stereo analog-output-headphones

Systemd Autologin && Noclear

SYNOPSYS

I want autologin on one of my media purpose archlinux box.

PROCEDURE

TTYVTDisallocate=

/etc/systemd/system/getty@tty1.service.d/noclear.conf
1
2
[Service]
TTYVTDisallocate=no

ExecStart=
Type=

/etc/systemd/system/getty@tty1.service.d/autologin.conf
1
2
3
4
[Service]
ExecStart=
ExecStart=-/urs/bin/agetty --autologin jeyzu --noclear %I 38400 linux
Type=idle

Postgresql: Row Update Trigger

SYNOPSYS

This post needs a trigger to update a timestamp at each row modification.

PROCEDURE

add usr_modified field to org_users table and set the trigger
1
2
3
4
5
6
7
8
9
10
ALTER TABLE org_users ADD COLUMN usr_modified timestamp with time zone NOT NULL DEFAULT current_timestamp;

CREATE OR REPLACE FUNCTION org_users_update() RETURNS trigger AS $BODY$
BEGIN
  NEW.usr_modified := current_timestamp;
  RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;

CREATE TRIGGER org_users_update_call BEFORE UPDATE ON org_users FOR EACH ROW EXECUTE PROCEDURE org_users_update();

Postgresql: Table Replication Through Databases

SYNOPSYS

I already wrote on the subject.
Obvious issues where easily discovered.
I must add a timestamp to the records and do some INSERT or UPDATE.

PROCEDURE

update cur_data with new or updated date from origin_db/tbl_data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
CREATE OR REPLACE FUNCTION sync_cur_data(usr text, passwd text, days integer)
RETURNS integer AS
$BODY$
DECLARE
    count integer;
    r cur_data%rowtype;
BEGIN
    count = 0;
    FOR r IN SELECT *
        FROM dblink(format('dbname=origin_db user=%s password=%s', usr, passwd),
                    format('select * from tbl_data where ts > (current_timestamp - interval '%d days')))
        AS t1 (
            f1  integer,
            f2  boolean,
            f3  text,
            ts timestamp
        )
    LOOP
        count = count + 1;
        PERFORM f1 FROM cur_data WHERE f1=r.f1;
        IF NOT FOUND THEN
            INSERT INTO cur_data SELECT r.*;
        ELSE
            UPDATE cur_data SET f2=r.f2, f3=r.f3, ts=r.ts WHERE f1=r.f1;
        END IF;
    END LOOP;
    RETURN count;
    END
$BODY$
LANGUAGE plpgsql VOLATILE

ISSUES

  • need to maintain an INSERT/UPDATE timestamp via a trigger.

Postgresql: Psql Flags

SYNOPSYS

How annoying !

NOTICE:  table "new_table" does not exist, skipping
CONTEXT:  SQL statement "DROP TABLE IF EXISTS new_table"
PL/pgSQL function "adm_update_table_from" line 9 at SQL statement

PROCEDURE

Luckily I found this on Peter Eisentraut’s Blog (worth the reading).

Long story short :

PGOPTIONS='--client-min-messages=warning' psql -X -q -a -1 -v ON_ERROR_STOP=1 --pset pager=off -d mydb -f dump.sql