Index

Unix/Bash bible

Inspired by pure-bash-bible, so I wanted to compile a list of notes and tips for myself on how to “properly” use Unix/Bash. If this list is missing something, then it’s probably because I’ve never had a use for it.

xargs

Generally when you’re piping information, each program takes in the input, does something to it, then spits out something afterwards. This doesn’t work though when your command expects its input to be parameter rather than some file. For example, mkdir expects an argument of the folder name to make, so simply streaming in a name won’t work. echo "test" | mkdir will fail where echo "test" | xargs mkdir will work.

useful flags

  • -I: use as -I [REPLACEMENT STRING] (commonly {}). This let’s you run a command over each and every line. Example: If you have a list of file paths and want to run basename over all of them.
    • ex. cat files.txt | xargs -I {} basename {}

tac

Note to self that this is in coreutils and is like cat but outputs the lines in reverse.

grep

useful flags

  • -H: print’s out filename
  • -e: extended regex (can do counting)
  • -o: only prints out line that it matches

sort

useful flags

  • -M: sorts by month
  • -R: sorts randomly
  • -k: partition out sections of lines as key to sort by
    • you can specify multiple keys that will go in order
  • -n: sorts by number
  • -r: sorts in reverse

uniq

Does what the name says. Given a bunch of lines, spits it back out with no duplicates.

cut

It seems that I generally use it with -f and -d. -d sets the character you want to split on, and -f is the index of which split piece you want to pull out.

An example:

>> echo "date: July 4, 2024" | cut -d: -f1
 May 20, 2024

sed

Takes in stream of input and modifies according to the rules you set. The way it works is that there are a lot of commands you can run on the input, and you specify which ones you want to use.

find and replace

the s function is basically Unix’s find and replace tool. sed 's|^\./||' is an example use where we’re finding ^\./ and replacing it with nothing.

piping to files

  • >> will redirect stdout to some file
  • 2> will redirect stderr to some file

rg

This is basically grep but faster? Not sure why people still use grep!? (will look into this later)

useful flags

  • -I: hides filenames
  • -N: hides line numbers

general bash magic

${parameter#word}
${parameter##word}

You can expand out a parameter than use regex to take off patterns from the front of it. #word removes shortest prefix, and ##word removes the longest.