Environment, Process and Finding
20 min read
Intermediate Terminal Exercises
Objectives
This chapter introduces no new commands — it’s a chance to put Module 3 together: environment variables, PATH, processes, find, and grep.
Part I — Check Your Understanding
- Create an environment variable called
FIRST_NAMEset to your first name (it doesn’t need to be permanent). - Print the
FIRST_NAMEvariable. - Print the
$PATHvariable. - What is the
$PATHvariable, and what does your shell use it for? - Why would you want to create your own environment variable?
- How do you make an environment variable permanent?
- What is a process?
- How do you list every process running on your machine?
- What is a
PID? - How do you stop a process?
- What’s the difference between
killandkill -9? - What
grepflag enables case-insensitive search? - What
grepflag shows a certain number of lines before a match? - What
grepflag shows a certain number of lines around a match? - What
grepflag shows a certain number of lines after a match? - What
grepflag restricts a match to a whole word? - What
grepflag shows the line number of a match?
Part II — Hands-On
Write the terminal commands to do the following (assume instructors.txt is a file that already exists):
- Find all files inside the Desktop folder named exactly
learn. - Find all files inside the Desktop folder that start with
P. - Find all files inside the Desktop folder that end in
.txt. - Find all files inside
Desktop/viewsthat havedatasomewhere in the filename. - In
instructors.txt, output how many times the word “Erin” appears. - In
instructors.txt, list every whole word that starts with a capitalP. - In
instructors.txt, list the line numbers of every whole word starting withz, matching regardless of case.
Part III — Challenges
These combine skills from this module with piping from Module 2.
- Chained variables. Create an environment variable
BASE_DIRpointing at a folder of your choice, then defineLOGS_DIRusing$BASE_DIRplus a subfolder name, without hardcoding the path twice. - Where does it live? Run
whichon three commands you use often, and note which folder each one comes from. - Hunt and kill, in one line. Start a
lesssession on any file. In a second window, pipeps auxintogrepto find that process without scrolling manually, then kill it using thePIDyou found. - Find, then search. Create three or four small
.txtfiles with different words inside each. Usefindwith a wildcard to list them all, thengrepfor a specific word across whichever one you think contains it. - Write your own pattern. Using the
names.txtfile from this module’s lesson (or a list of your own), write agrepregular expression that counts how many entries start with a vowel. - Clean sweep. Remove every file and folder you created for this section.
Recap
You can now set and read environment variables, understand and troubleshoot PATH, inspect and stop processes, and locate files and text precisely with find and grep. That’s the toolkit Module 3 set out to build.
Next module: connecting to remote machines with SSH, and more advanced text processing with cut, sed, awk, and xargs.