← Operating Systems from Zero: Linux, Windows and macOS

Video coming soon

The written lesson below is ready now. The video is being published on our YouTube channel.

Follow ChangeCV on YouTube →

Lesson 4 of 6

The command line: pipes, redirects, PATH, which and history

Join small commands with pipes, save output to files with redirects, see how the system finds programs with PATH and which, and reuse commands with history. With a conveyor-belt analogy.

The idea in plain words

Think of a factory conveyor belt. Each program is a station that does one small job, and the pipe passes the output of one station to the next.

A redirect drops the result into a box, and that box is a file. Chain a few simple stations and you get a powerful machine.

Commands in this lesson

ls | wc -l
Send the output of one command into the next. Here it counts the files. The pipe works in PowerShell too, but it passes objects instead of plain text.
echo "build started" > build.log
Save output to a file. > replaces the file and >> adds to the end. Windows PowerShell 5 writes UTF-16, so use Set-Content for plain text.
echo $PATH
Show the folders where the system looks for programs. In PowerShell it is $env:PATH, separated by semicolons.
which python3
Show which file runs when you type a command. In PowerShell use Get-Command.
history
List the commands you typed before. On macOS use history 1 to see all of them. In PowerShell use Get-History.

Written notes from the video

Why the command line is powerful

Small commands become powerful when you connect them. This is Operating Systems from zero, episode four: the command line. Five tools to join commands, save results, and find programs.

The conveyor belt analogy

Think of a factory conveyor belt. Each program is a station that does one small job. The pipe passes the output of one station to the next. A redirect drops the result into a box. That box is a file. Chain a few simple stations, and you get a powerful machine.

Command 1: the pipe

Command one: the pipe, written as a vertical bar. It sends the output of one command into the next one. Here, ls lists the files, and wc -l counts the lines. So we have two files. We can also count the lines that contain ERROR in the log. The answer is two. On Windows PowerShell, the pipe works too, but it passes objects instead of plain text.

Command 2: redirects

Command two: the redirect. The greater than sign sends output into a file, instead of the screen. It replaces anything that was in the file before. Two greater than signs append to the end of the file. Then cat build.log shows both lines. On Windows PowerShell, the same signs work. Version 5 saves text as UTF-16, so use Set-Content for plain text.

Command 3: echo $PATH

Command three: echo $PATH. The path is a list of folders where the system looks for programs. On Linux and macOS, the folders are separated by colons. When you type a command, the system checks these folders in order. On Windows PowerShell, the same list is $env:PATH, separated by semicolons.

Command 4: which

Command four: which. It tells you which file runs when you type a command. which python3 answers with /usr/bin/python3. That is how you find out where a program lives. On Windows PowerShell, use Get-Command.

Command 5: history

Command five: history. It lists the commands you typed before, each with a number. Press Ctrl and R to search the history. Press Tab to complete names. On macOS, history one shows everything, because plain history shows only the last few. On Windows PowerShell, use Get-History.

On Windows and macOS

On Windows PowerShell, Get-ChildItem piped to Measure-Object counts the files. Get-Content shows a file, and $env:PATH shows the folders for programs. Get-Command finds a program, and Get-History lists your commands. Each system has a startup file. On Linux it is .bashrc, on macOS .zshrc, and on Windows the profile.

Recap

Recap. The pipe joins commands. A redirect saves the output in a file. echo $PATH lists program folders, which finds a program, and history lists what you typed.

Next episode

Next episode: processes. You will learn to see what is running, and stop it. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

What is the difference between > and >>?

> replaces everything in the file with the new output. >> adds the new output to the end and keeps what was there.

Why does PowerShell 5 write odd characters with > ?

Windows PowerShell 5 saves redirected text as UTF-16, which some tools show badly. Set-Content writes plain text. PowerShell 7 uses UTF-8 by default.

What is PATH?

PATH is a list of folders. When you type a command name, the system looks through these folders in order and runs the first match. Linux and macOS separate them with colons, and Windows with semicolons.

Why does history on macOS show only a few commands?

In zsh, plain history lists only the most recent commands. history 1 starts from the first one and shows everything that is saved.

How do I search my history?

Press Ctrl and R and start typing. The shell finds the most recent matching command. Press Tab to complete names of files and commands.