These final exercises will cover some commands useful for development, operational purposes and the elegance of piping.
Before concluding we'll demystify command history and get a taste of some advanced shortcuts.
- Simple file readers:
lessismore- Slightly more versatile versions of
catthat contain simple text searches less hello_grep.txt- Type
/to enter search mode & thenhelloas your search string and hitenter - the up/down arrow keys and
space(page down) will aid in page navigation - sometimes
lessis not available on containers butmoreis more hello_grep.txt
- Slightly more versatile versions of
- Display parts of a file:
headandtail- Both commands give you a sample of a large flat file and
tailis the workhorse of log watching head hello_grep.txtprints the first 10 lines (default) of a filehead -3 hello_grep.txtprints the first 3 lines of a filetail -5 hello_grep.txtprints the last 5 lines of a file (default is also 10)
- Both commands give you a sample of a large flat file and
- The original computer-search noun that became a verb (no, not google)
grep:-
Simple & flexible command to locate text in a flat file
-
grep hello hello_grep.txt -
Aside from simple searches grep also fully supports regular expressions
-
grep can also be used to ignore certain text
grep -v hello hello_grep.txt -
By default, grep is case-sensitive but ignoring case is also common
grep -i hello hello_grep.txt -
fun fact, grep stands for "Globally search for a Regular Expression and Print matching lines"
-
- Simple web request tool:
curlcurlcomes in handy when testing out APIs or doing an investigation for front end work- It's light weight but also capable enough all varieties of HTTP requests
- Get your ip address
curl ifconfig.me - Check the headers for debugging
curl -v ifconfig.me - Sample POST request
curl 'https://jsonplaceholder.typicode.com/posts' --data-raw '{"title":"foo","body":"bar","userId":1}' -H 'content-type: application/json; charset=UTF-8' - Your favorite web browser can export curl commands from the network tab of the developer console
- Putting it all together with pipe
|-
One of the most elegant features of shell programming is ability to easily tie multiple commands together using the pipe
|operator. -
Developers who created the early shell commands weren't focused on solving every possible use case for their tools but rather kept them simple for the purpose of efficiency & speed.
-
To allow for flexibility the
|utility allowed users to stitch together various commands to accomplish their unique use case. -
Specifically, a command on the left side of
|feeds its output directly to the input of the command on the right side -
tail hello_grep.txt | grep hello -
grep hello hello_grep.txt | grep twist -
Execute the given script to create a simulated log file:
./log_generator.sh &(note: the&sends the script execution to the background) -
tail -f grep.log -
press
ctrl+cto stop following the file -
tail -f grep.log | grep hello -
press
ctrl+cto stop following the file -
Check the content type of a HTTP response
curl -s -I ifconfig.me | grep content-type
-
- Command
historyhistorywill display a list of your previous commands in descending order- Using the up/down arrow keys at shell prompt traverses this history
- It can be combined w/ pipe to find that nifty command you used yesterday
history | grep curl - There's an even better way!
- Basic shell keyboard shortcuts:
- The best shortcut has already been covered
tabfor completion. - shortcut
ctrl + ris used to recursively look up your previous history. Once prompted type characters and simple regex will be used to locate the previous command that matches it. Typectrl + ragain to locate the next oldest match ctrl + cwill kill whatever program you are running and is also handy to delete your current promptctrl + lwill clear your shell (mac also supportscmd + k)ctrl + dis a shortcut to exit the current session (note: this will close your current tab)
- The best shortcut has already been covered
- If you forget use the manual:
man- Anytime you forget how to use a command know that there are manuals for them all
man curl- It can be meta!
man man
References:

