Miscellaneous Notes
The notes captured in the Linux notes tool = n.
7zip
7z a -p -mhe=on archive.7z <files_or_folders> | -mhe=on - encrypt file contents and filenames/headers. |
ES
@esql 'SELECT id.value FROM "mdm-example-entity-store-business-all-data"' | Elasticsearch SQL |
Excel
| Open Drop-down in Cell - ALT-DOWN_ARROW |
Windows
| WIN-H - turn on dictation |
apt
sudo apt autoclean | tidy the package cache |
sudo apt autoremove | remove not required dependencies |
sudo apt install ./download/apt.deb | install deb files |
awk
awk '($1 ~ /re/ \|\| $1 ~/2/) && $2 == "one" && $3 >= 7 {print $5}' | Various matches (!~ /x/ is not match) |
awk '/^#/ {next} {print}' file # skip lines starting | ('NF == 0 {next} - skip blank lines) |
awk '/^aa/,/^bb/ {print $0}' file | output from first line starting 'aa' to the last line starting 'bb' |
awk '/error/' file | A regex match anywhere in file |
awk 'BEGIN { "date" \| getline nw; close("date"); print "time now: " tolower(nw)}' | run unix data and capture output |
awk 'BEGIN { i=1; while (("ls" \| getline f) > 0) { print "file " i++ ": " f } close("ls") }' | loop though command output |
awk 'BEGIN {OFS="->"}{ORS="\|"}{print $1, $2}' file | Output Field Separator, O' Record S' (replacing newline) |
awk 'BEGIN {OFS="->"}{ORS="\|"}{print $1, $2}' file | Output Field Separator, Output Record Separator (replacing newline) |
awk 'BEGIN {RS=""} {print $0}' file | Treat blank-line-separated paragraphs as records |
awk 'BEGIN {print "head"} {print $0} END {print "---\n" NR}' file | BEGIN/END |
awk 'NR == 1 {mn=mx=$1} $1<mn {mn=$1} $1>=mx {mx=$1} END {print mn, mx}' numfile | Can't use BEGIN as $1 not set for BEGIN. |
awk 'function sz(v) {if (v < 5) return "S" else if (v >= 9) return "L" else return "M"} {print $1, sz($1)}' file | functions |
awk '{count[$1]++} END {for (x in count) print x, count[x]}' file | count is array indexed by $1 value containing count |
awk '{for (i=1; i<=NF; i++) {print i, $i}}' file | for loop through all fields, every line |
awk '{i=1}{while (i<=5) {print i; i++ }}' file | while loop, every line |
awk '{if ($1 >= 80) {print $1, "Y"} else {print $1, "N"}}' | if, else. No elif - use 'else if' |
awk '{n=split($1,a,","); print n, a[1]}' file | split $1 on comma, n = number of elements, a is array of elements |
awk '{print $1, $3, $NF, NF}' file | print 1st, 3rd, last field and number of fields ($0 is all fields) |
awk '{print $1, $3, $NF, NF}' file | print 1st, 3rd, last field and number of fields ($0 is all fields) |
awk '{print FILENAME, NR, FNR, $0}' file1 file2 | NR - record number continues, FNR - resets each file |
awk '{printf "%-12s %5d %.2f\n", $1, $2, $3}' file | Formatted output: %12s - left pad, %-12s - right pad |
awk '{t += ($1 + $2)} END {print "tot:" t, "avg:" t/NR}' file | Arithmetic |
awk -F, '{print $1, $7}' file | use , as field separator (awk 'BEGIN {FS=","} {print ...) also works |
awk -F, '{print $1, $7}' file | use , as field separator (awk 'BEGIN {FS=","} {print ...) also works |
awk -v x="$envvar" '$1 == x {print $0}' file | Using environment variables |
length($1), tolower($1), toupper($1), index($1, "err:") | index is find |
substr($1, 1, 3), sub(/foo.*bar/, "fb"), gsub(/foo/, "bar") | gsub - replace all occurrences |
bash
!! !<abc> !!:p !<abc>:p | Run last command (:p - print command) |
!^ !$ !:2 !:2-4 | First, Last, 2nd, 2nd to 4th arguments from prev command (!:0 is last command) |
#!/usr/bin/env bash | First line bash script |
${var^^} ${var,,} | var to UPPERCASE, lowercase |
COLUMNS=1; PS3="Choose: "; select v in *; do [[ -n $v ]] && break; done | Generate a numbered list of options |
CTRL-X CTRL-E | Edit current command line |
cp file.txt{,.bak} | cp file.txt file.txt.bak |
echo {0..10..2} | 0 -> 10 in steps of 2: 0 2 4 6 8 10 |
echo {1,2}{a..e} | 1a 1b 1c 1d 1e 2a 2b 2c 2d 2e |
o="$(!!)" | Rerun last command and capture output eg. `which f.txt`, `o="$(!!)"`, `vi "$o"` |
claude
| Quick/simple → Haiku; Everything else → Sonnet; Hard problems → Opus |
code
| ALT+z - Enable word wrap |
| CTRL+, - Open Settings |
| CTRL+. - Fix problem |
| CTRL+; - Find next problem (I added this shortcut) |
| CTRL+k CTRL+s - Open Keyboard Shortcuts |
| CTRL-k v - View markdown and markdown preview side by side |
column
column -J -s"," -N col1,col2 | format csv as JSON with attribute names col1, col2 |
column -t -s"," -N col1,col2 | format csv as a table with headings col1, col2 |
date
date -d "1 month ago" +%y-%m-%d | Outputs date 1 month ago in YY-MM-DD format. |
find
find . -name "*.jar" -exec du -h {} \; | Find the size of jar files |
find . -type d -name ".git" | Find directories |
find . -type d -name .git -prune -o -type f -name *.py -print | exclude .git directories |
find . -type f -mtime +90 | Find files last modified at least 90 days ago |
gcp
| gcloud auth login --no-launch-browser |
git
git checkout tags/<tag name> | checkout a git tag. |
git push --set-upstream origin main | set main as default origin branch |
grep
grep -v ... | to invert the match |
if
if [[ -d "<directory>" ]] | Check directory exists |
if [[ -e "<path>" ]] | Check any path (file, directory, device, etc.) exists |
if [[ -f "<file>" ]] | Check file exists |
if [[ -n "$var" ]] | Non-zero length / not empty check |
if [[ -z "$var" ]] | Zero length / empty check |
jq
jq '. \\| length' | Count |
jq '.[0]' | Array item |
jq '.[] \\| select(x == "y")' | Filter |
jq '.[] \\| {a, b}' | Pick fields |
jq '.[]' | Loop array |
jq '.a.b.c' | Nested field |
jq '.field' | Get a field |
jq -r '.field' | Raw output |
ln
ln -s <target_file_or_dir> [<link_name>] | -s symbolic link (ie a pointer), hard link is same item two names |
postgres
ALTER ROLE <username> SET search_path TO <schema name>; | Permanently alter search_path. |
SET search_path TO <schema name>; | Alter the schema searched. |
SHOW search_path; | show the currently search schema. |
export PGPASSWORD=postgres | Set password |
export PGPASSWORD=postgres; cat my.sql \\| psql -h localhost -U postgres -d postgres | Run my.sql |
ps
ps auxww | list linux processes, ww - show full command, do not truncate |
psql
SET search_path=<schema>[,<schema>]; | set current schema(s) |
SHOW search_path; | show current schema(s) |
\c <db> | connect to database |
\d <table> | describe table |
\dn | list schemas |
\dt | list tables |
\l | list databases |
sed
sed -E | Extended - can use 's/a+/b/' rather than 's/a\+/b/' |
sed -E 's/a+/b/' | one or more a's. |
sed -E 's/a{2,5}/b/' | between 2 and 5 a's |
sort
sort -h | Sort human readable numbers eg 1.2GB, 9KB |
sqlite
REPLACE(PRINTF('%100c', ' '), ' ', 'X') | Generate a string of 100 X's |
tar
tar -cvzf filename sourcedir | Create tarball. |
tar -xvzf <filename> | Extract zipped tarball. |
tr
tr A-Z a-z | lowercase |
tr a-z A-Z | uppercase |
vim
/lala\C | Make search case sensitive (..\c - insensitive) [sed, regex] |
:%!sort -R | Random order - calling external app to order [sed, regex] |
\a (\A) | alphabetic character [A-Za-z]. Uppercase: non ... [sed, regex] |
\d \x (\D \X) | digit [0-9], hex digit [0-9A-Fa-f]. Uppercase: non... [sed, regex] |
\l \u | lowercase letter [a-z], uppercase letter [sed, regex] |
\w \s (\W \S) | word character [0-9A-Za-z_], whitespace char (space, tab). Uppercase: non... [sed, regex] |
wsl
/c/Users/[name]/.wslconfig | WSL Config |