find tricks

Tasks I tend to forget how to perform with find(1) . Delete matched files The most trivial one: find <path> [..] -delete (e.g. find . -name foo -delete). However, depending on the OS -delete might not be available. In that case, see the next one. Execute command with matched files find <path> [..] -exec <cmd> '{}' \; is executed once for each matched file and the relative path to it is replaces '{}' and I always forget that \;, which might be omitted depending on a bunch of weird shell-isms (e.g. find . -name foo -exec rm -f '{}' \;). ...

219 words

Generate password

pwgen [length] [count] By default it generates a bunch of passwords in columns. However with args it can be ustructed to generate one fairly long secret (64 characters): pwgen 64 1 It has fairly good number of ways to configure what and how exactly to generate. The downside of this approach is that pwgen(1) is probably not installed by default on your machine. /dev/urandom Reading directly from /dev/urandom is the way to go on pretty much any UNIX these days (or fallback to /dev/random, although both are the same except on Linux) without having to install anything. The only question is how to make it output plain-text. ...

218 words

nmap

Find hosts in a range nmap -sn 1.2.3.0/24 HOST DISCOVERY: -sL: List Scan - simply list targets to scan -sn: Ping Scan - disable port scan -Pn: Treat all hosts as online -- skip host discovery -PS/PA/PU/PY[portlist]: TCP SYN, TCP ACK, UDP or SCTP discovery to given ports -PE/PP/PM: ICMP echo, timestamp, and netmask request discovery probes -PO[protocol list]: IP Protocol Ping -n/-R: Never do DNS resolution/Always resolve [default: sometimes] --dns-servers <serv1[,serv2],...>: Specify custom DNS servers --system-dns: Use OS's DNS resolver --traceroute: Trace hop path to each host Port Scanning Quick TCP SYN scanning (-sS: requires root), skipping host discovery (-Pn), of most popular ports: ...

259 words

rsync

rsync -arvzh [host:]/source/path/ [host:]/destination/path Note the trailing slash in source path: this avoids creating [host:]/destination/path/path. -a: archive (i.e. ensures that symlinks, attributes, permissions, owner, etc. are preserved) -r: recursive -v: verbose (second v gives what files are being skipped and some stats at the end) -z: compress -h: human-readable format of numbers --del | --delete: delete files on the destination that are not sent from the source --stats: file-transfer statistics at the end -m: prune empty directories from file-list -t: preserve modification times ...

99 words

Watch for file changes

TL;DR: inotifywait -m -e close_write -e delete -e move $(pwd) Example output under different conditions: touch foo && echo ... > foo /path/to/watched/dir/ CLOSE_WRITE,CLOSE foo mv foo bar /path/to/watched/dir/ MOVED_FROM foo /path/to/watched/dir/ MOVED_TO bar rm bar /path/to/watched/dir/ DELETE bar Text editors This is where things get interesting vim /path/to/watched/dir/ CLOSE_WRITE,CLOSE foo Boring, right? However, let’s do :set backup and try again: /path/to/watched/dir/ CLOSE_WRITE,CLOSE 4913 /path/to/watched/dir/ DELETE 4913 /path/to/watched/dir/ MOVED_FROM foo /path/to/watched/dir/ CLOSE_WRITE,CLOSE foo wut? emacs /path/to/watched/dir/ CLOSE_WRITE,CLOSE foo /path/to/watched/dir/ DELETE .#foo .#foo is never created, yet deleted. ...

101 words

What is my IP?

I’m using a page setup by Jason A. Donenfeld, which he uses to illustrate setting up WireGuard : https://zx2c4.com/ip Even have it as an alias in my dotfiles : alias myip='curl -s https://zx2c4.com/ip | head -n1'

36 words