Classroom Public page

Week 8: Linux Shell I

975 words

The command line. Every software professional navigates the filesystem from the terminal. This week you learn the 15 commands that cover 90% of day-to-day shell work: navigation, file operations, text search, and pipes.


Theme

The graphical desktop you normally use is a layer on top of the operating system. Under it, the OS thinks about files, directories, processes, and users. The shell is the program that lets you talk to the OS directly, in text. It is faster for many tasks than a graphical interface, it is scriptable (automatable), and it is the primary interface for servers, embedded systems, and remote administration.

This week you learn the navigation and text-manipulation commands. Week 9 adds scripting, environment variables, and SSH.

Reading list (~1 hour)

  1. The Linux command line tutorial at https://linuxcommand.org/lc3_learning_the_shell.php, lessons 1-6 (navigation, looking around, a guided tour, manipulating files, working with commands)
  2. Optional: William Shotts, The Linux Command Line (free PDF at https://linuxcommand.org/tlcl.php), Chapters 1-8

If you are on macOS, your shell is also bash (or zsh in modern macOS); all commands in this week work on macOS. Windows users need WSL2 installed (see SETUP.md).

Lecture outline (~2 hours)

Section 1: The filesystem tree

  • Unix filesystems are a single tree rooted at / (the root directory)
  • Every file and directory has a path from root: /home/student/lab8/notes.txt
  • Home directory: ~ is a shorthand for your home directory (/home/username on Linux, /Users/username on macOS)
  • Absolute path: starts with /; unambiguous from any location
  • Relative path: relative to the current directory; lab8/notes.txt from your home directory, or ../other-dir/file to go up one level

Section 2: Core navigation commands

Command What it does
pwd Print working directory (show where you are)
ls List contents of current directory
ls -la List all files (including hidden) in long format with permissions
cd path Change directory to path
cd .. Go up one directory
cd ~ Go to home directory
mkdir name Create a directory named name
rmdir name Remove an empty directory

Practice: navigate from your home directory to /tmp, list its contents, then navigate back.

Section 3: File operations

Command What it does
cat file Print the contents of file to the terminal
cp src dst Copy src to dst
mv src dst Move/rename src to dst
rm file Remove a file (CAUTION: no trash; deletion is permanent)
rm -r dir Remove a directory and all its contents (CAUTION: permanent)
touch file Create an empty file (or update its modification time)
less file Page through a file; press q to quit, / to search
head -n 20 file Print the first 20 lines
tail -n 20 file Print the last 20 lines

Warning on rm: unlike the graphical trash can, rm permanently deletes. There is no undo. Always double-check the filename.

Section 4: Text search with grep

  • grep pattern file: print all lines in file that contain pattern
  • grep -i pattern file: case-insensitive search
  • grep -r pattern dir: recursive search in all files under dir
  • grep -n pattern file: print line numbers alongside matches
  • grep -v pattern file: print lines that do NOT contain the pattern (invert)
  • grep -c pattern file: count the number of matching lines

Pattern can be a plain string or a regular expression. For FND-101, plain strings are enough.

Section 5: Pipes and redirection

  • The shell connects programs together with pipes (|): the output of one program becomes the input of the next
  • cmd1 | cmd2: pipe stdout of cmd1 into stdin of cmd2
  • cmd > file: redirect stdout to file (overwrites)
  • cmd >> file: redirect stdout to file (appends)
  • cmd < file: read stdin from file
  • cmd 2> file: redirect stderr to file

Example pipeline: cat /var/log/syslog | grep "error" | head -20

  • cat reads the log file; grep filters to lines containing "error"; head shows the first 20 such lines

Section 6: find

  • find /path -name "*.txt": recursively find files whose name matches *.txt under /path
  • find . -type f -newer reference.txt: find files modified more recently than reference.txt
  • find . -type f -size +1M: find files larger than 1 MB
  • find . -name "*.log" -mtime -7: files named *.log modified in the last 7 days

find is often paired with -exec to run a command on each result, but that is an advanced use; stick to finding and listing for this week.

Labs (~90 minutes)

Lab 8.1: Shell Navigation Scavenger Hunt (labs/lab-8-1-shell-navigation.md)

  • A guided tour: navigate a provided sample directory tree, locate specific files using find and ls -la, and print their contents with cat
  • Build a pipeline: filter a sample log file for entries containing "ERROR" and count them
  • Artifact: a shell transcript (copy-paste from your terminal) committed to Git

Lab 8.2: Text Search (labs/lab-8-2-text-search.md)

  • Use grep, cut, sort, and uniq on a provided sample log file
  • Find the top 5 most frequent IP addresses in an access log using a pipeline
  • Artifact: the pipeline you built, saved as lab-8-2.sh, committed to Git

Independent practice (~4 hours)

  1. Use find to locate all .txt files in your home directory tree and count them with wc -l
  2. Explore man grep (the manual page). Find two grep flags you did not use in the lab and test them on a file
  3. Write a pipeline that: reads /etc/passwd, filters lines containing your username, and prints only the home directory field (cut -d: -f6)
  4. Run ls -la /usr/bin | wc -l and note the result in your Toolchain Diary. What is the number and what does it represent?
  5. Make a mistake: run rm on a file you care about (from a backup or a test file). Observe that it is gone. This is a real-world lesson; make it controlled.

Reflection prompts (~30 minutes)

  1. The shell is text-only. The graphical desktop is point-and-click. Which felt more efficient for the scavenger-hunt task in Lab 8.1? For what tasks do you imagine the shell is faster?
  2. The pipe operator connects programs together. This is the "Unix philosophy": small tools that each do one thing, connected in combinations. Give one example from the labs of a combination that would have been harder to do with a single program.
  3. rm has no undo. How does this shape your attitude toward automation? (Hint: think about what happens when you write a script that calls rm.)
  4. You can find any file on the system if you know what you are looking for, using find. From a security perspective, is this concerning? Why or why not?
  5. The shell is the primary interface for remote servers. Why does a server not have a graphical desktop?

What comes next

Week 9 builds on the shell: you write a bash script, learn about environment variables, and use SSH to reach a remote system. The lab in week 9 produces a script that does something useful, not just a sequence of manual commands.