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.