← 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

tac

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

grep

useful flags

sort

useful flags

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

rg

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

useful flags

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.