CodingNic

Environment, Process and Finding

Intermediate Terminal Exercises

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

  1. Create an environment variable called FIRST_NAME set to your first name (it doesn’t need to be permanent).
  2. Print the FIRST_NAME variable.
  3. Print the $PATH variable.
  4. What is the $PATH variable, and what does your shell use it for?
  5. Why would you want to create your own environment variable?
  6. How do you make an environment variable permanent?
  7. What is a process?
  8. How do you list every process running on your machine?
  9. What is a PID?
  10. How do you stop a process?
  11. What’s the difference between kill and kill -9?
  12. What grep flag enables case-insensitive search?
  13. What grep flag shows a certain number of lines before a match?
  14. What grep flag shows a certain number of lines around a match?
  15. What grep flag shows a certain number of lines after a match?
  16. What grep flag restricts a match to a whole word?
  17. What grep flag 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):

  1. Find all files inside the Desktop folder named exactly learn.
  2. Find all files inside the Desktop folder that start with P.
  3. Find all files inside the Desktop folder that end in .txt.
  4. Find all files inside Desktop/views that have data somewhere in the filename.
  5. In instructors.txt, output how many times the word “Erin” appears.
  6. In instructors.txt, list every whole word that starts with a capital P.
  7. In instructors.txt, list the line numbers of every whole word starting with z, matching regardless of case.

Part III — Challenges

These combine skills from this module with piping from Module 2.

  1. Chained variables. Create an environment variable BASE_DIR pointing at a folder of your choice, then define LOGS_DIR using $BASE_DIR plus a subfolder name, without hardcoding the path twice.
  2. Where does it live? Run which on three commands you use often, and note which folder each one comes from.
  3. Hunt and kill, in one line. Start a less session on any file. In a second window, pipe ps aux into grep to find that process without scrolling manually, then kill it using the PID you found.
  4. Find, then search. Create three or four small .txt files with different words inside each. Use find with a wildcard to list them all, then grep for a specific word across whichever one you think contains it.
  5. Write your own pattern. Using the names.txt file from this module’s lesson (or a list of your own), write a grep regular expression that counts how many entries start with a vowel.
  6. 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.