Skip to content

Instantly share code, notes, and snippets.

@pestophagous
Last active July 23, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pestophagous/182f7ea8301c5b912ee480515e874312 to your computer and use it in GitHub Desktop.
Save pestophagous/182f7ea8301c5b912ee480515e874312 to your computer and use it in GitHub Desktop.
Learning And Using Bash

A Book For Learning And Using Bash

Several weeks ago, I was at an in-person client meeting where I was getting a walkthrough of how to use a device.

I noticed a book and asked if I could borrow it.

The book:

bookcover

I consider myself "very experienced" with the Linux command line already, but I wanted to see if I would recommend this book to other people who are just getting started.

I have read 7 chapters (jumping around a bit), and I am very impressed!

My conclusion: I recommend this book without hesitation.

It has good, compelling examples. It is very readable. It is also well organized, so you can focus on whichever topics are most relevant to you. The author did a great job of calling out related Linux system concepts that are pertinent to various commands.

"One Weird Tool" For Learning And Using Bash

Since 2016, I have been using a bash history tool that saves my bash history forever. (That sounds like an exaggeration but it's really not.)

The original tool:

My "fork" of it:

Using My "One Weird Tool" to Measure the Book's Utility

Since I have kept my bash history since 2016, I realized that I could do an analysis.

I can quantify my own "100 most used bash commands", and then check whether this book teaches about each of them.

The results indeed validate my already strong recommendation of the book.

The "census" of my bash history is also interesting in its own right.

My 100 Most Used Bash Commands (2016-2021)

If you do not recognize a command, click the link to learn about it.

Commands are listed from most-used to least-used.

The number after each command shows how many times the command appears in my bash history, between 2016 and 2021.

Four commands are custom, and are explained at the bottom.

  1. git, 162542
  2. cd, 43973
  3. ls, 43133
  4. grep, 32317
  5. ec (see bottom section), 15779
  6. glof (see bottom section), 12307
  7. cat, 11352
  8. rm, 9963
  9. find, 8569
  10. sudo, 7907
  11. cp, 6072
  12. cdg (see bottom section), 4302
  13. bazel, 3735
  14. mv, 3687
  15. echo, 3302
  16. less, 3269
  17. eog, 2865
  18. mkdir, 2844
  19. diffmerge, 2789
  20. tree, 2735
  21. xargs, 2721
  22. export, 2693
  23. lsfg (see bottom section), 2490
  24. source, 2306
  25. touch, 2030
  26. man, 1592
  27. make, 1539
  28. awk, 1538
  29. ps, 1408
  30. which, 1268
  31. sort, 1244
  32. evince, 1112
  33. docker, 1048
  34. file, 1041
  35. df, 1033
  36. cut, 1024
  37. tail, 979
  38. convert, 936
  39. pushd, 913
  40. killall, 846
  41. pwd, 777
  42. chmod, 767
  43. popd, 763
  44. diff, 750
  45. ssh, 740
  46. ping, 703
  47. head, 690
  48. du, 636
  49. gitk, 615
  50. curl, 608
  51. wc, 543
  52. lsof, 509
  53. On MacOS open, 502
  54. ln, 487
  55. vagrant, 470
  56. ffmpeg, 447
  57. kill, 419
  58. qmake, 414
  59. ifconfig, 410
  60. qmlscene, 399
  61. dmesg, 390
  62. sed, 389
  63. ldd, 381
  64. python3, 376
  65. htop, 346
  66. strings, 313
  67. dot, 301
  68. date, 299
  69. scp, 287
  70. umount, 277
  71. dpkg, 277
  72. clear, 272
  73. psql, 268
  74. unzip, 262
  75. wmctrl, 239
  76. jq, 239
  77. rmdir, 234
  78. fg, 223
  79. type, 222
  80. unset, 220
  81. cmake, 218
  82. mount, 214
  83. tar, 205
  84. pgrep, 187
  85. history, 177
  86. nm, 169
  87. pkill, 166
  88. perl, 159
  89. ipython, 158
  90. wget, 151
  91. env, 147
  92. apt, 145
  93. nmap, 144
  94. lsmod, 144
  95. recordmydesktop, 140
  96. mediainfo, 132
  97. gs, 132
  98. tee, 132
  99. watch, 126
  100. gdb, 123

The Four Custom Commands In My Top 100

ec

A script as follows. It takes any number of filenames and opens them all in an already-running instance of emacs:

#!/bin/bash

# Should now work on linux AND macos:
# https://stackoverflow.com/a/28806991/10278
echo "$@"  | tr ' ' '\0' |  tr '\n' '\0' | xargs -0 -n1   emacsclient -n

glof

An alias that shows a compact, columnar, colored git log:

git log --color=always -20 --pretty=format:'%C(yellow)%h|%Cred%cd|%Cblue%an|%Cgreen%d %Creset%s' --date=short | awk '{printf("%s\n",$0)}' | column -ts'|' | less -R -X

cdg

An alias that moves the current working directory to the root of whichever git repository you are navigating in:

cd $(git rev-parse --show-toplevel)

lsfg

A script as follows. It takes grep arguments and does a recursive grep over only version-controlled files in the git repository you are navigating in. In particular, if there are untracked files (such as build artifacts or local config files ignored by git), then those untracked files will not be grepped through:

#!/bin/bash

MYCLRCMD="--color=always"

if [ -t 1 ] ; then
    true;
    #echo "running in terminal rather than piped to a thing";
else
    MYCLRCMD="--color=auto";
fi

# the `find` invocation (for `-type f`) will stop any version-controlled SYMLINK
# files from moving forward in the pipeline

# this tactic excludes submodules
git grep --cached -l '' | xargs -I {} find "{}" -type f | xargs -I {} grep -Hn $MYCLRCMD "$@" "{}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment