Metacharacters

MetacharacterPurposeExamples
| (pipe)Pipes the output of one command to another commandecho “string of text” | grep
>Empties a file and writes the output of a command itecho “string of text” > textfile.txt

echo > textfile.txt
2>Redirects only STDERR to a file.cat file.txt 2> grep “Password”
>>Appends the output of a command to the end of a fileecho “string of text” >> textfile.txt
<Read a file and pipe the contents to a commandsort < file.txt

grep “Password” < file.txt
&Start the command before & and move it to the background, and run the command after & if there is onerm -rf /tmp & nano ~/.bashrc
;Run the command before ; and when that command finishes, run the command after ;cp /etc/hosts /etc/sample | echo “Could not copy /etc/hosts”
&&Run the second command only if the first one ran successfully
|Run the second command only if the first one did not run successfully
$Marks a variablePATH, $var
* (wildcard)Matches any word or filenamels /var/log/.log

ls /home/


cat *.log
.Represents the current folder. Files and folders beginning with . are considered hidden and won’t appear unless you use ls -a.ls .

cp /etc/ssh/ssh_config .
..Represents the parent folder of the current folder.cd ..

Regex metacharacters:

[]Matches the characters listed in the brackets. For example [acd] means either a, c, or d. [a-d] means a, b, c, and d. [0-9] means every digit from 0 to 9.
$Means “end of the line”. For example, 77$ will only match 77 if it’s the last bit of text at the end of a line.
^Means “beginning of the line”. ^Hello will only match Hello at the beginning of a line and will ignore Hello if it’s in the middle of a line or at the end.
\Means “don’t interpret the character that comes after this”. If you want to search for an asterisk in a text file, you would use \* as your search to tell grep “treat this like any other ASCII character, don’t treat it as a wildcard”.
|Means “match either of these”. For example, (win|lose) will match the word “win” or the word “lose”.