- It is remarkably helpful sometimes that you can do set intersection, union, and difference of text files via `sort`/`uniq`. Suppose `a` and `b` are text files that are already uniqued. This is fast, and works on files of arbitrary size, up to many gigabytes. (Sort is not limited by memory, though you may need to use the `-T` option if `/tmp` is on a small root partition.) See also the note about `LC_ALL` above and `sort`'s `-u` option (left out for clarity below).
```sh
cat a b | sort | uniq > c # c is a union b
cat a b | sort | uniq -d > c # c is a intersect b
cat a b b | sort | uniq -u > c # c is set difference a - b
```
- Use `grep . *` to visually examine all contents of all files in a directory, e.g. for directories filled with config settings, like `/sys`, `/proc`, `/etc`.
- Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python):
```sh
awk '{ x += $3 } END { print x }' myfile
```
- If want to see sizes/dates on a tree of files, this is like a recursive `ls -l` but is easier to read than `ls -lR`:
```sh
find . -type f -ls
```
- Use `xargs` or `parallel` whenever you can. Note you can control how many items execute per line (`-L`) as well as parallelism (`-P`). If you're not sure if it'll do the right thing, use xargs echo first. Also, `-I{}` is handy. Examples:
```sh
find . -name '*.py' | xargs grep some_function
cat hosts | xargs -I{} ssh root@{} hostname
```
- Say you have a text file, like a web server log, and a certain value that appears on some lines, such as an `acct_id` parameter that is present in the URL. If you want a tally of how many requests for each `acct_id`:
xmlstarlet sel -t -v "(html/body/ul/li[count(p)>0])[$RANDOM mod last()+1]" |
xmlstarlet unesc | fmt -80
}
```
## Obscure but useful
-`expr`: perform arithmetic or boolean operations or evaluate regular expressions
-`m4`: simple macro processor
-`yes`: print a string a lot
-`cal`: nice calendar
-`env`: run a command (useful in scripts)
-`look`: find English words (or lines in a file) beginning with a string
-`cut `and `paste` and `join`: data manipulation
-`fmt`: format text paragraphs
-`pr`: format text into pages/columns
-`fold`: wrap lines of text
-`column`: format text into columns or tables
-`expand` and `unexpand`: convert between tabs and spaces
-`nl`: add line numbers
-`seq`: print numbers
-`bc`: calculator
-`factor`: factor integers
-`gpg`: encrypt and sign files
-`toe`: table of terminfo entries
-`nc`: network debugging and data transfer
-`socat`: socket relay and tcp port forwarder (similar to `netcat`)
-`slurm`: network trafic visualization
-`dd`: moving data between files or devices
-`file`: identify type of a file
-`tree`: display directories and subdirectories as a nesting tree; like `ls` but recursive
-`stat`: file info
-`tac`: print files in reverse
-`shuf`: random selection of lines from a file
-`comm`: compare sorted files line by line
-`hd` and `bvi`: dump or edit binary files
-`strings`: extract text from binary files
-`tr`: character translation or manipulation
-`iconv` or `uconv`: conversion for text encodings
-`split `and `csplit`: splitting files
-`7z`: high-ratio file compression
-`ldd`: dynamic library info
-`nm`: symbols from object files
-`ab`: benchmarking web servers
-`strace`: system call debugging
-`mtr`: better traceroute for network debugging
-`cssh`: visual concurrent shell
-`rsync`: sync files and folders over SSH
-`wireshark` and `tshark`: packet capture and network debugging
-`ngrep`: grep for the network layer
-`host` and `dig`: DNS lookups
-`lsof`: process file descriptor and socket info
-`dstat`: useful system stats
- [`glances`](https://github.com/nicolargo/glances): high level, multi-subsystem overview
-`iostat`: CPU and disk usage stats
-`htop`: improved version of top
-`last`: login history
-`w`: who's logged on
-`id`: user/group identity info
-`sar`: historic system stats
-`iftop` or `nethogs`: network utilization by socket or process
-`ss`: socket statistics
-`dmesg`: boot and system error messages
-`hdparm`: SATA/ATA disk manipulation/performance
-`lsb_release`: Linux distribution info
-`lshw`: hardware information
-`fortune`, `ddate`, and `sl`: um, well, it depends on whether you consider steam locomotives and Zippy quotations "useful"
## More resources
- [awesome-shell](https://github.com/alebcay/awesome-shell): A curated list of shell tools and resources.
- [Strict mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/) for writing better shell scripts.
## Disclaimer
With the exception of very small tasks, code is written so others can read it. With power comes responsibility. The fact you *can* do something in Bash doesn't necessarily mean you should! ;)