Linux Operating Systems Fundamentals · Lesson 1

Exploring Linux Command-Line Tools

Exploring Linux Command-Line Tools: Build reliable command lines from shell basics, text filters, regular expressions, and data streams.

  • linux
  • command-line
  • shell
  • lpic-1

Lesson purpose

A strong Linux workflow treats commands as composable tools, not isolated facts. Build reliable command lines from shell basics, text filters, regular expressions, and data streams.

Learning objectives

  • Navigate and control an interactive Linux shell.
  • Edit, view, transform, and summarize text safely.
  • Combine regular expressions, streams, redirection, and pipelines.

LPIC-1 exam focus

  • 103.1 Work on the command line
  • 103.2 Process text streams using filters
  • 103.4 Use streams, pipes, and redirects
  • 103.7 Search text with regular expressions
  • 103.8 Basic file editing

Teaching sequence

1. A shell connects users to the Linux toolset

The central idea is a shell connects users to the linux toolset. Use these points to explain the topic and connect it to the next command or decision.

  • Distribution context. Commands are broadly portable, while package names, defaults, and terminal launchers can differ.
  • TTY or terminal emulator. A text console and a graphical terminal both provide an interactive shell session.
  • Prompt. The prompt shows that the shell is ready; it may encode user, host, path, and privilege level.
  • Command program. Most entries invoke a program or shell builtin with arguments and options.

2. Shell choices balance compatibility and features

This comparison prevents students from treating related tools or layers as interchangeable.

Side Teaching point
Bash and Dash Bash is the common interactive default; Dash is small and fast for POSIX-style scripts.
KornShell, tcsh, and Z shell Alternatives emphasize programming features, C-like syntax, completion, history, or customization.

Decision rule: Check the assigned login shell and the interpreter named by a script before assuming Bash behavior.

3. Command syntax separates action from control

Use this example to show how command syntax separates action from control works in a controlled environment.

  • Command. Names the builtin or executable to run.
  • Options. Modify behavior; short and long forms depend on the utility.
  • Arguments. Identify files, directories, patterns, or other operands.
$ command [options] [arguments]
$ uname -a
$ echo $SHELL
$ type cd

4. Paths determine where a command operates

Use this example to show how paths determine where a command operates works in a controlled environment.

  • Absolute path. Begins at / and identifies one location regardless of the current directory.
  • Relative path. Begins from the present working directory and may use . or …
  • Home shortcuts. cd, cd ~, and cd $HOME return to the user’s home directory.
$ pwd
/home/student
$ cd /var/log
$ cd ../tmp
$ cd ~

5. The shell resolves names before execution

The central idea is the shell resolves names before execution. Use these points to explain the topic and connect it to the next command or decision.

  • Builtins. Commands such as cd execute inside the shell because they must change shell state.
  • External commands. Executable files are found by searching directories in PATH.
  • type, which, whereis. Use these tools to learn how a name will be interpreted and where files are located.
  • Aliases and functions. Shell-defined shortcuts may replace or wrap the command name you typed.

6. Quoting and expansion change the command line

The central idea is quoting and expansion change the command line. Use these points to explain the topic and connect it to the next command or decision.

  • Globbing. *, ?, and bracket expressions expand matching filenames before the command runs.
  • Single quotes. Preserve literal characters and suppress variable and command expansion.
  • Double quotes. Preserve spaces while allowing selected expansions such as $variable.
  • Escaping. A backslash protects the next special character from shell interpretation.

7. History turns prior commands into reusable work

Use this example to show how history turns prior commands into reusable work works in a controlled environment.

  • history. Displays the current history list and supports file synchronization options.
  • Recall. Arrow keys and history expansion retrieve earlier command lines.
  • Safety. Review recalled commands before running them, especially as root.
$ history
$ history -a
$ history -n
$ history -r

8. Environment variables configure the session

Use this example to show how environment variables configure the session works in a controlled environment.

  • Local shell variable. Exists in the current shell until exported or the shell exits.
  • Exported variable. Becomes part of the environment inherited by child processes.
  • Configuration files. Login and non-login shells read different system and user startup files.
$ PS1='lab> '
$ export EDITOR=vim
$ echo "$PATH"
$ env | sort

9. Linux help tools answer different questions

Use the matrix to contrast the named choices before students select a command or configuration.

Item Meaning
command –help Quick syntax and option reminder
man command Reference manual organized into sections
info command Hypertext-style GNU documentation
help builtin Documentation for shell builtins
man -k keyword Search manual descriptions by keyword

Teaching point: Start with the narrowest source that answers the question, then verify examples in a safe environment.

10. Choose an editor that matches the task

This comparison prevents students from treating related tools or layers as interchangeable.

Side Teaching point
nano and emacs Nano favors direct editing; Emacs offers a large command environment and GUI counterpart.
vim Vim separates movement, insertion, and colon commands for fast keyboard-driven editing.

Decision rule: The best editor is the one you can use confidently without losing or corrupting a configuration file.

11. vim modes separate navigation from changes

Use this example to show how vim modes separate navigation from changes works in a controlled environment.

  • Command mode. Default mode for movement and editing commands.
  • Insert mode. Entered with i; Esc returns to command mode.
  • Ex mode. Colon commands save, quit, search, and configure the editor.
i        # enter Insert mode
Esc      # return to Command mode
:w       # save
:q       # quit
:wq      # save and quit
:q!      # abandon changes

12. Viewing commands control how much text you see

Use the matrix to contrast the named choices before students select a command or configuration.

Item Meaning
cat / bat Read a small file from beginning to end
head / tail Select the first or last records; tail -f follows growth
less / more Page through output interactively
grep Return records that match a pattern

Teaching point: Match the viewing tool to file size, whether content is changing, and whether you need pattern selection.

The central idea is combining commands align related input. Use these points to explain the topic and connect it to the next command or decision.

  • cat. Concatenates files or standard input in sequence.
  • join. Combines records from sorted files using a shared field.
  • paste. Merges corresponding lines side by side.
  • Input order. The selected delimiter and record order determine whether combined output is meaningful.

14. Transformers change representation or order

Use the matrix to contrast the named choices before students select a command or configuration.

Item Meaning
expand / unexpand Convert tabs to spaces or spaces to tabs
tr Translate, squeeze, or delete characters
sort / uniq Order records and report adjacent duplicates
split Divide a large file into smaller pieces
od Display bytes in octal or other numeric formats

Teaching point: Transformations are most predictable when locale, delimiters, and sort order are explicit.

15. Formatting commands prepare text for people

The central idea is formatting commands prepare text for people. Use these points to explain the topic and connect it to the next command or decision.

  • fmt. Reflows paragraphs to a target width.
  • nl. Adds configurable line numbering.
  • pr. Paginates text with headers, columns, and margins.
  • Purpose. Formatting changes presentation; it should not be confused with filtering records.

16. cut selects fields without changing the file

Use this example to show how cut selects fields without changing the file works in a controlled environment.

  • Records. Text is normally processed one newline-terminated record at a time.
  • Delimiter. A character such as : separates fields within a record.
  • Selection. -c selects characters; -f selects fields; -d declares the delimiter.
$ cut -d: -f1 /etc/passwd
$ cut -c1-8 names.txt
$ cat -E records.txt

17. Summaries measure content and integrity

The central idea is summaries measure content and integrity. Use these points to explain the topic and connect it to the next command or decision.

  • wc. Counts lines, words, and bytes or characters.
  • md5sum. Produces a legacy digest useful for non-security integrity comparisons.
  • sha256sum. Produces a stronger SHA-256 digest.
  • sha512sum. Produces a SHA-512 digest when that algorithm is required.

18. Basic regex patterns select matching records

Use this example to show how basic regex patterns select matching records works in a controlled environment.

  • Anchors. ^ matches the start and $ matches the end of a record.
  • Character sets. . matches one character; brackets select from a set or range.
  • Repetition. * repeats the preceding expression zero or more times.
$ grep '^root:' /etc/passwd
$ grep 'colou*r' notes.txt
$ grep '[0-9]$' data.txt

19. Extended regex adds concise alternatives

Use this example to show how extended regex adds concise alternatives works in a controlled environment.

  • grep -E. Enables extended regular-expression syntax; egrep is the historical name.
  • Alternation. | expresses one pattern or another.
  • Grouping and counts. Parentheses, +, ?, and braces express grouped or counted repetition.
$ grep -E 'error|warning' app.log
$ grep -E '^[A-Z][a-z]+$' names.txt
$ grep -E 'ab{2,4}c' data.txt

20. File descriptors separate three standard streams

The central idea is file descriptors separate three standard streams. Use these points to explain the topic and connect it to the next command or decision.

  • STDIN — 0. The default input stream, usually the keyboard or upstream pipeline.
  • STDOUT — 1. Normal program results, usually displayed in the terminal.
  • STDERR — 2. Diagnostics and error messages, independently redirectable from output.
  • Descriptor choice. Correct redirection depends on which stream a program actually uses.

21. Redirection changes where streams begin or end

Use this example to show how redirection changes where streams begin or end works in a controlled environment.

  • > and >>. Create/overwrite or append standard output.
  • <. Read standard input from a file.
  • 2> and 2>&1. Redirect standard error alone or combine it with standard output.
$ sort < names.txt > sorted.txt
$ command 2> errors.log
$ command > all.log 2>&1

22. Pipelines turn small tools into workflows

Use this example to show how pipelines turn small tools into workflows works in a controlled environment.

  • Pipe. | connects one command’s standard output to the next command’s standard input.
  • sed. Applies scripted edits to each input record and writes transformed output.
  • xargs. Builds command arguments from standard input when a command does not consume a stream directly.
$ ps aux | grep '[s]sh'
$ sed 's/error/ERROR/g' app.log
$ find . -name '*.log' -print0 | xargs -0 wc -l

23. Apply the lesson to four scenarios

Ask these questions before revealing the answer key. Require students to name the evidence or command that supports each choice.

  1. A command works interactively but fails in a script that uses /bin/sh. What should you inspect first?
  2. You need usernames from the colon-delimited /etc/passwd file. Which filtering strategy fits?
  3. A report must include normal output and errors in one file. Which descriptors must be handled?
  4. You need lines beginning with ERROR or WARNING. Which grep mode and pattern are appropriate?

24. Connect each scenario to the governing clue

Use these answers to debrief the knowledge check. The explanation matters as much as the label.

  1. Interpreter and shell features. Check the shebang and avoid assuming Bash-only behavior in a /bin/sh script.
  2. cut -d: -f1. Declare the colon delimiter and select the first field without modifying the source.
  3. STDOUT and STDERR. Redirect descriptor 1 and combine descriptor 2 with it, preserving the intended order.
  4. grep -E with an anchored alternation. Use an extended expression such as ^(ERROR|WARNING).

25. Three takeaways resolve the lesson

Close the lesson by asking students to restate the decision rule behind each takeaway.

  • Shell context matters. Interpreter, expansion, variables, and path resolution determine what a command means.
  • Text tools are composable. View, combine, transform, format, filter, and summarize with purpose-built utilities.
  • Streams connect the workflow. Descriptors, redirection, regex, sed, and pipelines make repeatable processing possible.

Next lesson connection: Managing Software and Processes.

Classroom application

Use a disposable VM or lab account for commands that can modify packages, processes, partitions, filesystems, ownership, or permissions. Require students to state the target and expected effect before they run a command.

  • Open a terminal and identify the shell, working directory, PATH lookup, and relevant help page without changing system files.
  • Build a pipeline that extracts a field from a text file, sorts it, removes adjacent duplicates, and writes normal output and diagnostics to separate files.
  • Use vim or nano to make a controlled configuration edit, then verify the result with grep, diff, and a checksum.

Common misconceptions

  • The prompt is not the shell. A prompt is output produced by the shell, while the shell parses input and starts builtins or external programs.
  • A pipeline connects standard output to standard input. It does not automatically merge standard error, so redirect descriptor 2 explicitly when needed.
  • Basic and extended regular expressions are different dialects. Use grep -E when alternation, grouping, +, ?, or counted repetition is required.
  • A text editor changes content; a filter transforms a stream. Select the tool according to whether the task is interactive editing or repeatable processing.

Lesson summary

  • Shell context matters. Interpreter, expansion, variables, and path resolution determine what a command means.
  • Text tools are composable. View, combine, transform, format, filter, and summarize with purpose-built utilities.
  • Streams connect the workflow. Descriptors, redirection, regex, sed, and pipelines make repeatable processing possible.

The next lesson is Managing Software and Processes.