diff --git a/knowledge base/cmp.md b/knowledge base/cmp.md new file mode 100644 index 0000000..2b1e0b3 --- /dev/null +++ b/knowledge base/cmp.md @@ -0,0 +1,44 @@ +# `cmp` + +Compares two files byte by byte. + +Prints the line and the character number where the two files diverge. + +## Table of contents + +1. [TL;DR](#tldr) +1. [Further readings](#further-readings) +1. [Sources](#sources) + +## TL;DR + +```sh +# Print only lines present in both file1 and file2. +cmp 'path/to/file1' 'path/to/file2' +``` + +## Further readings + +- [`comm`][comm] +- [man page] + +## Sources + +All the references in the [further readings] section, plus the following: + +- [6 more terminal commands you should know] + + + + + +[further readings]: #further-readings + + +[comm]: comm.md + + +[6 more terminal commands you should know]: https://betterprogramming.pub/6-more-terminal-commands-you-should-know-3606cecdf8b6 +[man page]: https://linux.die.net/man/1/cmp diff --git a/knowledge base/comm.md b/knowledge base/comm.md index abdd613..d2fbf7c 100644 --- a/knowledge base/comm.md +++ b/knowledge base/comm.md @@ -1,19 +1,60 @@ -# comm +# `comm` -`comm` requires the files it is working with to be pre-sorted. +Compares two files line by line. + +With no options, produces 3 columns in output: + +- column 1 contains lines **unique to file 1**; +- column 2 contains lines **unique to file 2**; +- column 3 contains lines **common** to both files. + +Comparisons honor the rules specified by 'LC_COLLATE'. + +## Table of contents + +1. [TL;DR](#tldr) +1. [Further readings](#further-readings) +1. [Sources](#sources) ## TL;DR ```sh +# Print only lines present in both file1 and file2. +comm -12 'path/to/pre-sorted/file1' 'path/to/pre-sorted/file2' + # Print unique lines of file1 which are not present in file2. -comm -23 <(sort -u 'file1') <(sort -u 'file2') +comm -23 'path/to/pre-sorted/file1' 'path/to/pre-sorted/file2' +comm -23 <(sort -u 'path/to/file1') <(sort -u 'path/to/file2') # Check the whole content of file1 is present in file2. -[[ $(comm -23 <(sort -u 'file1') <(sort -u 'file2') | wc -l) -eq 0 ]] +test $(comm -23 'path/to/pre-sorted/file1' <(sort -u 'path/to/file2') | wc -l) -eq 0 +[[ $(comm -23 <(sort -u 'path/to/file1') 'path/to/pre-sorted/file2' | wc -l) -eq 0 ]] ``` +## Further readings + +- [`cmp`][cmp] +- [man page] + ## Sources -- [Check whether all lines of file occur in different file] +All the references in the [further readings] section, plus the following: +- [Check whether all lines of file occur in different file] +- [6 more terminal commands you should know] + + + + + +[further readings]: #further-readings + + +[cmp]: cmp.md + + +[6 more terminal commands you should know]: https://betterprogramming.pub/6-more-terminal-commands-you-should-know-3606cecdf8b6 [check whether all lines of file occur in different file]: https://unix.stackexchange.com/questions/397747/check-whether-all-lines-of-file-occur-in-different-file#397749 +[man page]: https://linux.die.net/man/1/comm diff --git a/knowledge base/iptab.md b/knowledge base/iptab.md new file mode 100644 index 0000000..0eacc56 --- /dev/null +++ b/knowledge base/iptab.md @@ -0,0 +1,37 @@ +# `iptab` + +Displays a reference table of subnet masks and other IP address space details. + +It has no arguments. That's it. + +## Table of contents + +1. [TL;DR](#tldr) +1. [Further readings](#further-readings) +1. [Sources](#sources) + +## TL;DR + +```sh +iptab +``` + +## Further readings + +## Sources + +All the references in the [further readings] section, plus the following: + +- [6 more terminal commands you should know] + + + + + +[further readings]: #further-readings + + + +[6 more terminal commands you should know]: https://betterprogramming.pub/6-more-terminal-commands-you-should-know-3606cecdf8b6 diff --git a/knowledge base/jot.md b/knowledge base/jot.md new file mode 100644 index 0000000..c3638a7 --- /dev/null +++ b/knowledge base/jot.md @@ -0,0 +1,56 @@ +# `jot` + +Generates sequential or random data. + +## Table of contents + +1. [TL;DR](#tldr) +1. [Further readings](#further-readings) +1. [Sources](#sources) + +## TL;DR + +```sh +# Print 21 evenly spaced increasing numbers from -1 to 1. +jot '21' '-1' '1.00' +jot -p '2' '21' '-1' '1' + +# Print all ASCII characters. +jot -c '128' '0' + +# Print all strings from 'xaa' to 'xaz'. +jot -w 'xa%c' '26' 'a' + +# Print 20 random 8-letter strings. +jot -r -c '160' 'a' 'z' | rs -g '0' '8' + +# Create files containing a bunch of 'x' characters for exactly 1024 bytes of +# data. +jot -b 'x' '512' > 'file.txt' + +# Print all lines of 80 characters or longer. +grep $(jot -s "" -b '.' '80') 'file.txt' +``` + +## Further readings + +- [`man` page][man page] + +## Sources + +All the references in the [further readings] section, plus the following: + +- [6 more terminal commands you should know] + + + + + +[further readings]: #further-readings + + + +[6 more terminal commands you should know]: https://betterprogramming.pub/6-more-terminal-commands-you-should-know-3606cecdf8b6 +[man page]: https://manned.org/jot diff --git a/knowledge base/log a terminal session.md b/knowledge base/log a terminal session.md new file mode 100644 index 0000000..d8bf6d2 --- /dev/null +++ b/knowledge base/log a terminal session.md @@ -0,0 +1,3 @@ +# Log (record) a terminal session + +See [`script`](script.md). diff --git a/knowledge base/script.md b/knowledge base/script.md new file mode 100644 index 0000000..44eb9f8 --- /dev/null +++ b/knowledge base/script.md @@ -0,0 +1,56 @@ +# `script` + +Make a typescript file (a.k.a. log a.k.a. recording) of a terminal session. + +## Table of contents + +1. [TL;DR](#tldr) +1. [Further readings](#further-readings) +1. [Sources](#sources) + +## TL;DR + +```sh +# Start recording. +# Defaults to a file named "typescript". +script +script 'file.log' + +# Stop recording. +exit + +# Append to an existing file. +script -a 'file.log' + +# Execute quietly. +# Avoids 'start' and 'done' messages. +script -q 'file.log' + +# Flush output after each write. +script -f +``` + +## Further readings + +- [6 more terminal commands you should know] +- [`man`][man] + +## Sources + +All the references in the [further readings] section, plus the following: + +- [cheat.sh] + + + + + +[further readings]: #further-readings + + + +[6 more terminal commands you should know]: https://betterprogramming.pub/6-more-terminal-commands-you-should-know-3606cecdf8b6 +[cheat.sh]: https://cheat.sh/script +[man]: https://manned.org/script diff --git a/knowledge base/ssh.md b/knowledge base/ssh.md index b7d5c01..09d2ffd 100644 --- a/knowledge base/ssh.md +++ b/knowledge base/ssh.md @@ -55,6 +55,9 @@ ssh-add -L ssh-copy-id 'host.fqdn' ssh-copy-id -i "${HOME}/.ssh/id_rsa.pub" 'user@host.fqdn' +# Preload trusted keys. +ssh-keyscan 'host.fqdn' >> "${HOME}/.ssh/known_hosts" + # Connect to an unreachable host tunnelling the session through a bastion. ssh -t 'bastion-host' ssh 'unreachable-host'