How to do various things

General *nix

Run sed on every Rust source file in a directory (GNU):

find . -type f -name '*.rs' -exec sed -i 's/\(DataflowDesc::new(\)None/\1/' {} \;

Above for BSD:

find . -type f -name '*.rs' -exec sed -i '' 's/\(DataflowDesc::new(\)None/\1/' {} \;

Convert a Unix timestamp to a local date/time (GNU):

date -d @1571862547

Above for BSD:

date -r 1571862547

Convert times to other locations (GNU) (h/t Jesse Luehrs):

TZ=America/Los_Angeles date -d 'TZ="Africa/Harare" 2pm'

Get number of CPUs (BSD):

sysctl hw.ncpu

Get all the nodes watched in an inotify instance (GNU/Linux):

for f in $(cat fdinfo/6 | perl -ne 'if (/ino:([0-9a-f]*)/) { print hex $1 ; print "\n" }'); do
sudo debugfs -R "ncheck $f" /dev/nvme0n1p1 2>/dev/null | awk 'BEGIN {d=1} {d=!d; if (d) {print $0}}'
done

Print a number with digit separator:

LC_NUMERIC=en_US.utf8 printf "%'.f\n" # On FreeBSD: LC_NUMERIC=en_US.UTF-8

Find what process is listening on a port (Linux):

sudo lsof -i :8080

Find all processes listening on ports:

sudo ss -ptnulw

Find executable files under current directory (GNU):

find . -type f -executable

Above for FreeBSD:

find . -type f -perm -u+x

Diff the output of two commands (bash/zsh):

diff <(ls) <(ls -a)

Arch

Check what package owns a file:

pacman -Qo `which tail`

Checkout the source for a a package:

asp checkout coreutils

Ubuntu/Debian

Find files associated with a given package:

dpkg-query -L <package>

Check what package owns a file:

dpkg -S /bin/ls

Get source code for a package:

apt-get source xscreensaver

Change the local timezone:

sudo timedatectl set-timezone America/New_York

FreeBSD

Get reverse dependencies of a package:

pkg info -r wayland

Print a package's install message:

pkg query %M sshguard

OpenBSD

Search for a package:

pkg_info -Q gtk

Get installed reverse dependencies of a package:

pkg_info -R rust

FreeBSD server admin

Renew LetsEncrypt certificate:

certbot-3.6 renew
service dovecot onerestart
service nginx onerestart
# And whatever else you might need to restart...

Install patches:

freebsd-update fetch
freebsd-update install

Docker

Kill all running containers (h/t Brandon Maister):

docker kill $(docker ps -q)

Blow away a bunch of stuff (caches, unused volumes, etc):

docker system prune -a && docker volume prune

Rust

Patch a dependency from crates.io:

[patch.crates-io]
bar = { path = "/path/to/bar" }

Patch a dependency from GitHub:

# Note that the crate name (here `timely`) might differ from
# the repo name (here `timely-dataflow`).

[patch."https://github.com/TimelyDataflow/timely-dataflow"]
timely = { path = "/path/to/timely" }

Cross-compile:

sudo dnf install gcc-x86_64-linux-gnu
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-gnu-gcc RUSTFLAGS="" cargo build --release --target x86_64-unknown-linux-musl

Git

Delete a remote branch:

git push <remote> --delete <branch>

Check whether a commit is on a given branch:

git merge-base --is-ancestor <commit> <branch> && echo "it's there" || echo "nope"

Print the root directory of the repository:

git rev-parse --show-toplevel

Change the commit hash (e.g., to kick off a new CI run):

git commit --amend --no-edit

Default Ports

Service Port
Confluent Schema Registry 8081
Zookeeper 2181
Kafka 9092
SMTP 25
SMTPS 465
SMTP submission 587
IMAP 143
IMAPS 993
Confluent Control Center 9021
Grafana 3000
Hugo 1313
SQL Server 1433
Kafka Connect 8083
PostgreSQL 5432
Materialize 6875
OpenVPN 1194 (UDP)

Vim

Find out why startup is slow:

vim --startuptime out_file

Reload .vimrc:

:so $MYVIMRC

Trick to write a file as root:

:w !sudo tee %

Confluent

Start with a different data directory:

CONFLUENT_CURRENT=/some/where confluent local start

eBPF

Various tricks:

// sometimes clang is smarter than the verifier, and
// optimizes out a bounds check that the verifier needed.
//
// This function returns true if val1 < val2, and can't
// be optimized out.
static __always_inline bool force_lt(u64 val1, u64 val2) {
    bool out;
    asm volatile(
        "%[out] = 1\n"
        "if %[val1] < %[val2] goto 1f\n"
        "%[out] = 0\n"
        "1:\n"
        : [out] "=&r"(out)
        : [val1] "r"(val1), [val2] "r"(val2)
    );
    return out;
}

// Force logical shift right.
//
// clang sometimes compiles val / 2^k
// as val & -2^k, which the verifier can't cope with.
//
// Force it to compile as val >> k instead.
static __always_inline u64 force_lsr(u64 val, u64 shift) {
    asm volatile("%0 >>= %1\n" : "+&r"(val) : "r"(shift));
    return val;
}

Miscellaneous

Render Graphviz graph:

dot -T pdf < graph.dot > graph.pdf

Render this file:

rst2html < howtodo.rst > howtodo.html

Detach all other tmux clients:

tmux detach-client -a

OpenSSL equivalent of netcat:

openssl s_client -quiet -connect example.com:443

Decode a pprof file:

zcat ~/btv.pb.gz | protoc --decode=perftools.profiles.Profile ~/code/materialize/src/prof/src/pprof_types.proto -I ~/code/materialize