Investigating with commands

NUnix · L3 MIAGE · CM2 · 90 minutes

Louis Ledoux

ISTIC · University of Rennes

2026–2027

Investigating with commands

A computer is slow. Would adding RAM help?
Check which resource is under pressure first.

We will use commands to inspect the system, find useful information, and turn it into a summary we can save.

CM2 outline

  1. Processes and resources
  2. Finding files and text
  3. Inputs, outputs and redirections
  4. Combining commands with pipes

Observe the system → find information → redirect streams → combine commands.

1. Processes and resources

Which programs are running, and where are the resources going?

CPU, memory and disk space

Resource Question First observation
CPU Is computation keeping a processor busy? top, ps
RAM Is memory pressure affecting the system? free -h
Disk Can the filesystem store more data? df -h .

Adding RAM does not create free space on a full disk.

Running in the foreground

A process is a running instance of a program.

sleep 10

The shell waits for this foreground command to finish before showing a new prompt.

Ctrl+C requests an interrupt of the foreground job.

Inspecting processes

ps
ps -ef
Field Meaning
PID Process identifier
TTY Associated terminal, if any
TIME Accumulated CPU time
CMD Command

Checking memory usage

free -h
ps -eo pid,comm,%cpu,%mem --sort=-%mem
  • Read available memory; cache use is normal.
  • Identify the application’s process and follow it over time.
  • Check logs before attributing every delay to memory.

Checking disk usage

Tool Scope Question
df A filesystem How much space is available?
du A directory tree Which directories use space?
find Individual entries Which files fit my criteria?

Start broad, then inspect a specific file.

Find which directories use space

df -h "$HOME"
du -h --max-depth=1 "$HOME"

df shows usage of the filesystem containing your home.
du shows how much space your directories use.

From resources to files

  1. Processes and resources
  2. Finding files and text
  3. Inputs, outputs and redirections
  4. Combining commands with pipes

We can inspect CPU, memory and disk usage. Now we need to locate files and find the relevant lines in their contents.

2. Finding files and text

Where is the file, and which lines contain the information we need?

Finding files

find . -type f -name '*.txt'
  • . is the starting directory.
  • find also visits its subdirectories.
  • -type f selects regular files.
  • -name '*.txt' tests each entry's name.

The quotes let find receive the pattern unchanged.

Search criteria

Criterion Matches
-iname '*.txt' Name, ignoring case
-user alice Entries owned by Alice
-type d / -type l Directories / symbolic links
-size +100M Size greater than 100 MiB
-mtime -2 Modified less than 48 hours ago
find . -type f -size +100M

Checkpoint: find

  1. Find regular .c files below the current directory.
  2. Why quote '*.c'?
  1. find . -type f -name '*.c'
  2. So the shell passes the pattern to find without expanding it first.

A small log file

We have located a log file. Next, we will search its contents with grep. Save these four lines as app.log:

10:00 INFO STARTED
10:01 ERROR DISK_FULL
10:02 ERROR TIMEOUT
10:03 ERROR DISK_FULL
Field Meaning
1 Time
2 Severity
3 Event code

Finding text with grep

grep ' ERROR ' app.log
grep -r ' ERROR ' .
grep -n 'DISK_FULL' app.log
  • Print matching lines from a file.
  • -r searches directories recursively.
  • -n includes line numbers.

Exit status: 0 = match, 1 = no match, 2 = error in GNU grep.

Where the name grep comes from

The editor ed had the command pattern:

g/re/p
  • g: apply globally to matching lines.
  • re: a regular expression.
  • p: print those lines.

grep makes that text search available as a separate command.

Patterns in grep

Regular expression Meaning
. Any one character
m* Zero or more repetitions of m
[1-3] A digit from 1 to 3
grep 'm.*t' names.txt
grep 'toto[1-3]' names.txt

In a regular expression, * repeats the preceding item.

Who interprets this pattern?

printf '%s\n' *.log
grep 'DISK.*' app.log
find . -name '*.log'
  1. The shell expands a filename pattern.
  2. grep interprets a regular expression.
  3. find interprets a quoted filename pattern.

From matching lines to saved results

  1. Processes and resources
  2. Finding files and text
  3. Inputs, outputs and redirections
  4. Combining commands with pipes

We can find files and select useful lines. How can a command read from a file and save its output or errors?

3. Inputs, outputs and redirections

Where does a command read, where does it write, and did it succeed?

Anatomy of a command

A command receives arguments from above and stdin on the left. Stdout and stderr carry text out. A separate exit status returns to the shell when the command finishes.

Arguments configure the command. Streams carry text. The exit status reports success or failure when the command finishes.

Exit status

true
echo "$?"
false
echo "$?"

0 means success / true. Nonzero means failure / false.
$? is the status of the most recent command.

Keyboard input and screen output

cat

Keyboard text passes through cat unchanged and appears at the terminal. Type Linus, Ada and Grace on separate lines, then Ctrl+D on an empty line.

With no file argument, cat copies stdin to stdout.

Reading input from a file

sort < names.txt

names.txt contains Linus, Ada and Grace. Input redirection sends these lines to sort, which displays Ada, Grace and Linus at the terminal.

< names.txt makes the shell connect the file to stdin. The sorted lines still go to the terminal through stdout.

Arguments and input

head -n 2 < names.txt

head receives the arguments -n and 2 from above. It reads Linus, Ada and Grace from names.txt through stdin and prints the first two lines, Linus and Ada.

-n 2 tells head how many lines to keep. < names.txt supplies the text to read.

Saving output

sort < names.txt > sorted.txt

The shell connects names.txt to sort stdin and sort stdout to sorted.txt. The output file contains Ada, Grace and Linus.

> sorted.txt connects stdout to a file. The shell creates the file or replaces its contents.

Appending to a file

echo 'Zoé' >> sorted.txt
Operator Effect on the destination file
> Create it, or replace its contents
>> Create it, or append to its contents

Zoé is added after the existing lines in sorted.txt.

Redirecting errors

find . -name '*.txt' > matches.txt 2> errors.txt

find receives its search arguments from above. Stdout writes matching paths to matches.txt. Stderr writes diagnostic messages to errors.txt.

> sends normal output to matches.txt. 2> sends error messages to errors.txt.

Redirections are applied from left to right

ls notes.txt missing > all.txt 2>&1
ls notes.txt missing 2>&1 > out.txt
Command stdout ends up… stderr ends up…
First In all.txt In all.txt
Second In out.txt At the terminal

2>&1 copies stdout’s destination at that moment.
It does not make stderr follow later changes to stdout.

Why did the file become empty?

sort names.txt > names.txt

The shell opens and truncates names.txt before starting sort. The input is already gone.

Write to a different file, then inspect the result.

From one command to a pipeline

  1. Processes and resources
  2. Finding files and text
  3. Inputs, outputs and redirections
  4. Combining commands with pipes

We can choose the input and output of one command. Now we will connect commands to count and rank the errors in our log.

4. Combining commands with pipes

How can one command use another command’s output?

A pipe between two commands

grep ' ERROR ' < app.log | wc -l

app.log supplies four log lines to grep. Its three matching lines flow through a pipe into wc -l, which prints 3 at the terminal. Each command has separate arguments above it.

| connects the first command’s stdout to the next command’s stdin. wc -l counts the matching lines: 3 for our sample log.

Connecting programs like garden hoses

Doug McIlroy described connecting programs so that one program’s output could become another’s input.

grep ' ERROR ' app.log | wc -l

Each tool has a small job.
The shared text stream lets us combine them.

Pipeline step 1: extract error codes

grep ' ERROR ' app.log |
    cut -d ' ' -f 3
DISK_FULL
TIMEOUT
DISK_FULL

Pipeline step 2: count each code

grep ' ERROR ' app.log |
    cut -d ' ' -f 3 |
    sort | uniq -c | sort -nr
      2 DISK_FULL
      1 TIMEOUT

uniq -c counts adjacent equal lines. Sort first.

Save and display output with tee

sort names.txt | tee sorted.txt

sort takes names.txt as a file argument and sends sorted lines through a pipe to tee. tee takes sorted.txt as its argument and copies the same text to that file and to stdout at the terminal.

Like a T-shaped pipe fitting, tee copies the text to a file and to stdout.

Chaining commands with arguments

sort < names.txt | head -n 2 > first-two.txt

names.txt feeds sort. The pipe carries Ada, Grace and Linus to head, whose arguments -n and 2 enter from above. The shell saves Ada and Grace in first-two.txt.

< chooses the input file. | joins the commands. > chooses the output file. -n 2 remains an argument to head.

Checkpoint: redirections

  1. Which operator appends to a file?
  2. What does a | b connect?
  3. Does a > out.txt also redirect standard error?
  1. >>
  2. a's stdout to b's stdin.
  3. No. Use 2> to redirect stderr.

The exit status of a pipeline

false | sort
echo "$?"
set -o pipefail
false | sort
echo "$?"

Output: 0, then 1.
Bash normally reports the last pipeline command’s status.

CM2 summary: commands and pipelines

  1. Processes and resources: inspect CPU, memory and disk usage.
  2. Finding files and text: locate files with find, select lines with grep.
  3. Inputs, outputs and redirections: read files, save results, separate errors.
  4. Combining commands with pipes: extract, count and rank log entries.

Optional reference

More search expressions and interactive process control

Combining criteria

find /usr -name '*.c'
find . -user alice -name '*.c'
find . -not \( -name '*.obj' -o -name '*.map' \)
  • Adjacent tests use AND. -a makes it explicit.
  • -o means OR. -not negates a test.
  • Escaped parentheses group tests for find.

Running a command on matches

find . -type f -name '*.txt' -exec wc -l {} \;
  • -exec starts the command to run.
  • {} stands for the current matching path.
  • \; ends the command without ending the shell's command line.

Running in the background

sleep 60 &
jobs
fg %1
  • & lets the shell return to the prompt immediately.
  • jobs lists jobs managed by this shell.
  • fg %1 brings job 1 to the foreground.

Suspending and resuming a job

Action Effect
Ctrl+Z Suspend the foreground job
bg %1 Resume job 1 in the background
fg %1 Resume job 1 in the foreground

A job number belongs to a shell.
A PID identifies a process in the system.

Sequential and asynchronous execution

who | sort; date
sleep 10 &
date
  • ; runs the next command after the previous one finishes.
  • & starts work asynchronously.
  • The final date can run while sleep is still active.

Signals and end of input

Action Meaning
kill PID Send SIGTERM to a process
Ctrl+C Send SIGINT to the foreground process group
Ctrl+\ Send SIGQUIT to the foreground process group
Ctrl+Z Send SIGTSTP to suspend the foreground group
Ctrl+D Terminal end-of-input action
Université de Rennes Louis Ledoux