Cli and scripting

I use Linux on a daily basis at work and at home. This section is to remind me of how to carry out certain tasks. It may vary slightly as I use bash at home, but my work environment is set to tcsh.

To see your environment variables
printenv

To add an existing user to an existing group
sudo usermod -a -G group user

Regular Expressions

Meta character matching

Match any character with the following metas
\d match any number 0-9
\d\d match any two numbers 0-9
\w A-Z a-z 0-9 Any alphanumeric character
\s any whitespace, for example space or tab
. dot matches any character
\W not a word character
\S not whitespace

Anchors

^ Will look for a selection at the beginning of a string, e.g. ‘^To’ will find a line starting with the word To

$ Will look for a selection at the end of a string

Tip: Check for an empty line with grep ‘^$’

Ranges

Ranges are denoted by []

[A-Za-z] Any upper case of lower case letter

[0-9] Any digit

Boundaries

\s whitespace

\b word boundary

Quantifiers

Rather than using the \d\d example shown above, it is possible to use quantifiers to describe how many of a type of character we are looking for.
* 0 or more
+ 1 or more (at least one)
? 0 or 1 (optional character)
{min,max} Minimum or maximum number or characters
{n} Specific number of characters