Metacharacters
| Metacharacter | Purpose | Examples |
| | (pipe) | Pipes the output of one command to another command | echo “string of text” | grep |
| > | Empties a file and writes the output of a command it | echo “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 file | echo “string of text” >> textfile.txt |
| < | Read a file and pipe the contents to a command | sort < file.txt grep “Password” < file.txt |
| & | Start the command before & and move it to the background, and run the command after & if there is one | rm -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 variable | PATH, $var |
| * (wildcard) | Matches any word or filename | ls /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”. |