
Using Grep
Another great utility that is a great one to learn about is the use of grep. The grep command allows us to search the contents of files for specific values that we are looking for.
Take for example, the access log of a web server. In this case, the access.log of a web server has 244 entries.
Using “wc” to count the number of entries in “access.log”
tryhackme@linux1:~$ wc -l access.log
244 access.log
tryhackme@linux1:~$
Using a command like cat isn’t going to cut it too well here. Let’s say for example if we wanted to search this log file to see the things that a certain user/IP address visited? Looking through 244 entries isn’t all that efficient considering we want to find a specific value.
We can use grep to search the entire contents of this file for any entries of the value that we are searching for. Going with the example of a web server’s access log, we want to see everything that the IP address “81.143.211.90” has visited (note that this is fictional)
Using “grep” to find any entries with the IP address of “81.143.211.90” in “access.log”
tryhackme@linux1:~$ grep “81.143.211.90” access.log
81.143.211.90 – – [25/Mar/2021:11:17 + 0000] “GET / HTTP/1.1” 200 417 “-” “Mozilla/5.0 (Linux; Android 7.0; Moto G(4))”
tryhackme@linux1:~$
“Grep” has searched through this file and has shown us any entries of what we’ve provided and that is contained within this log file for the ip.
Searching Recursively with grep
Sometimes, the information we are looking for is spread across multiple files inside a directory. Instead of checking each file individually, we can tell grep to search recursively through all files and subdirectories.
To do this, we use the -R (recursive) option.
For example, to search for a variable across all files in the current directory and its subfolders, we can run:
grep -R “PRETTY_NAME” /etc/
This will:
Search every file in the current directory
Search all subdirectories
Show where the PRETTY_NAME appears
Example output:
grep -R “PRETTY_NAME” /etc/
grep: /etc/sudoers: Permission denied
/etc/os-release:PRETTY_NAME=”Ubuntu”
An Introduction to Shell Operators
Linux operators are a fantastic way to power up your knowledge of working with Linux. There are a few important operators that are worth noting. We’ll cover the basics and break them down accordingly to bite-sized chunks.
At an overview, I’m going to be showcasing the following operators:
Symbol / Operator Description
& This operator allows you to run commands in the background of your terminal.
&& This operator allows you to combine multiple commands together in one line of your terminal.
> This operator is a redirector – meaning that we can take the output from a command (such as using cat to output a file) and direct it elsewhere.
>>
This operator does the same function of the > operator but appends the output rather than replacing (meaning nothing is overwritten).
Let’s cover these in a bit more detail.
Operator “&”
This operator allows us to execute commands in the background. For example, let’s say we want to copy a large file. This will obviously take quite a long time and will leave us unable to do anything else until the file successfully copies.
The “&” shell operator allows us to execute a command and have it run in the background (such as this file copy) allowing us to do other things!
Operator “&&”
This shell operator is a bit misleading in the sense of how familiar is to its partner “&”. Unlike the “&” operator, we can use “&&” to make a list of commands to run for example command1 && command2. However, it’s worth noting that command2 will only run if command1 was successful.
Operator “>”
This operator is what’s known as an output redirector. What this essentially means is that we take the output from a command we run and send that output to somewhere else.
A great example of this is redirecting the output of the echo command that we learned in Task 4. Of course, running something such as echo howdy will return “howdy” back to our terminal — that isn’t super useful. What we can do instead, is redirect “howdy” to something such as a new file!
Let’s say we wanted to create a file named “welcome” with the message “hey”. We can run echo hey > welcome where we want the file created with the contents “hey” like so:
Using the > operator
tryhackme@linux1:~$ echo hey > welcome
Using cat to output the “welcome” file
tryhackme@linux1:~$ cat welcome
hey
Note: If the file i.e. “welcome” already exists, the contents will be overwritten!
Operator “>>”
This operator is also an output redirector like in the previous operator (>) we discussed. However, what makes this operator different is that rather than overwriting any contents within a file, for example, it instead just puts the output at the end.
Following on with our previous example where we have the file “welcome” that has the contents of “hey”. If were to use echo to add “hello” to the file using the > operator, the file will now only have “hello” and not “hey”.
The >> operator allows to append the output to the bottom of the file — rather than replacing the contents like so:
Using the >> operator
tryhackme@linux1:~$ echo hello >> welcome
Using cat to output the “welcome” file
tryhackme@linux1:~$ cat welcome