Basic regular expressions
Special characters
Metacharacter | Description |
---|---|
. | Match any single character |
[] | Match any single character within the square brackets, for example grep 'aljandr[oa]' /etc/passwd, will return either alejandra or alejandro |
? | Match the preceding character zero or one times, example : grep -E 'paulas?' /etc/passwd, will match paula or paulas |
+ | Match the preceding element one or more times, for example : grep -E 'j[a-z]+n' /etc/passwd, will return for example - joan, john, jason, jonathan |
* | Match the preceding element zero or more times, for example : grep -E 'jo[a-z]*n' /etc/passwd, will return for example - joan, john, jonathan |
^ | Match the beginning of a line, for example grep '^bin' /etc/passwd, will return all lines that start with bin |
$ | Match the ending of a line, for example grep '/bin/[kz]sh$' /etc/passwd, will return all line that end like /bin/ksh, /bin/zsh |
Sort
This command will sort the the output of a given file for example
$ sort /etc/passwd
Grep
This command will look for term within files, it can be customized in order to display the information is a given way
$ grep -rin --color "some term" *
This command will search for "some term" within the current directory and all subdirectory files
Head
This command displays the first 10 lines of a file, it can be alse customized to display more initial lines
$ head /etc/passwd
Tail
This command displays the last 19 lines of a file, it can be also customized to display more lines or to display the last part of a file dinamically
$ tail /etc/passwd $ tail -f /var/log/messages
Less
This command navigates in a read only way a given file, used most commonly to analyze big files such like error files
$ less /var/log/messages
More
This command also navigates in a read only way a given file, it has fewert capabilities compared to less since it can scroll down but not up and others functionalities
$ more /var/log/messages
Cat
This command displays the content of a given file, it does not allow navigation
$ cat /var/log/messages