The Command Line, Explained

UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 1.7 Free with a free account

The Command Line

Introduction to Operating Systems · Chapter 1: Operating System FoundationsSection 1.7 · lesson 7 of 838 min · free with a free account

← Mobile Operating SystemsNext lesson: Batch Files and Scripts →

A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

In this lesson

Typing shell commands to move around, write files, and join tools.

  • Explain the roles of the shell, the terminal, and the prompt
  • Navigate a file tree with absolute and relative paths using cd, pwd, and ls
  • Use one and two greater-than signs to replace or append file contents
  • Combine grep, wc, sort, and uniq with pipes and read exit codes

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,075 words). Read along, or search it.

Opening Question

A Hundred Machines Checked by Typing

Ivy looks after the computers at a school, and she often works without touching the mouse at all. She types a few words, presses Enter, and a hundred lab machines are checked, in the time it takes to open one folder window. To a beginner, it looks like a movie hacker's screen. To Ivy, it's just a very polite, very literal way of asking the operating system to do things. By the end of this lesson, you'll be doing it too.

Key Terms

CLI, Shell, Terminal, and Prompt

A graphical interface shows you pictures to point at. A command line interface, or CLI, lets you type instructions instead. Same OS, same work: only the way you ask changes. The program that reads what you type, and carries it out, is called a shell, since it wraps around the kernel like a shell around a nut. The window that holds the typing is the terminal. And the prompt, often a dollar sign, is the shell saying: I'm ready. Most Linux systems use a shell called bash. A Mac starts new accounts in zsh, but bash is installed too, and every command in this lesson is real bash, run on a Mac.

Why Type

Repeat, Save, Share, and Combine

Why bother, when a GUI exists? Typed commands can be repeated, saved, shared, and joined together. A server in a closet has no screen at all. And a single typed line can do to a thousand files what would take a thousand clicks.

File Tree

Root, Working Directory, and Paths

Files live in a file tree: folders, also called directories, inside folders, all growing from one starting point, called the root, written as a single slash. On a Mac, people's own files live under slash Users. Sam, who runs a small online bakery, might keep his in slash Users, slash sam, slash bakery. At any moment, the shell is standing in one folder: the working directory. A path that starts with a slash is absolute: it starts at the root. Any other path is relative: it starts where you're standing. Two shortcuts help. A single dot means here, and two dots mean the folder above.

Moving Around

Changing Folders and Checking Location

Let's try it. cd, short for change directory, moves the shell. Go to slash usr slash share: an absolute path. pwd, print working directory, says where you are: slash usr slash share. Now cd dot dot climbs one folder up. pwd again: slash usr. ls lists what's in this folder. These are folders the system keeps in slash usr, and share is one of them. And cd share uses a relative path. Share is right here, so the shell steps down into it, and pwd confirms it.

Making Files

Writing Files With Echo and Cat

cd on its own goes home, to your own folder. Make a new folder with mkdir bakery, and cd into it. echo prints words. With one greater-than sign, the words go into a file instead of the screen, creating it. Two greater-than signs add a line to the end of a file. So now orders dot t x t holds two orders. ls lists the folder, and cat shows a file's contents: both orders, in order.

Common Mistake

One Greater-Than Sign Erases a File

The classic mistake is one greater-than sign when you meant two. Watch: echo three cupcakes, with a single sign. Now cat the file again. The muffins and the cake are gone. A single sign replaced everything that was in the file.

Quick Check

Scones, Pies, and Redirection

Quick check. The file now holds just the cupcakes. Sam types two more lines: echo four scones, with two signs, then echo five pies, with one sign. What does cat orders dot t x t print now? Pause and predict. Only five pies. The double sign added the scones, but then the single sign replaced the whole file, scones and cupcakes included. When you mean to add, use two signs. They never throw anything away.

Small Tools

Small Tools Joined by Pipes

Next, small tools. Sam keeps a file of what sold today, one item per line. grep keeps only the lines that contain a word. wc dash l counts lines. sort puts lines in order, and uniq dash c counts repeats. Their power comes from the pipe, the vertical bar. It feeds the output of one command straight into the next, like a conveyor belt between kitchen stations.

Worked Example

Counting Muffins and Top Sellers

First, cd dot dot, back up to the home folder, where the sales file is. How many muffins went out today? grep muffin sales dot t x t keeps the three muffin lines. Pipe them into wc dash l, and it counts them: three. And which item sells most? Sort the file, and pipe it into uniq dash c: one bagel, two cakes, three muffins.

Quick Check

Counting Repeats Without Sorting

Quick check. Sam drops sort, and runs uniq dash c straight on the file. What does it print? Pause and predict. Six lines, each counted once. uniq only notices repeats that sit right next to each other. In this file, no two equal lines are neighbors, so every line is a run of one. That's why sort comes first: it brings the equal lines together.

Exit Codes

Zero Means Success

Last idea: what happens when a command fails? Ask for a folder that doesn't exist: ls cupcakes. The shell doesn't crash. ls prints a clear message, no such file or directory, and carries on. Every command also leaves an exit code, and dollar question mark holds the last one. Here, it's one. Try one that works: ls sales dot t x t. Now the exit code is zero. Zero means success, and anything else means a problem. On Linux, ls reports two for that missing folder. The number differs, but the meaning is the same: not zero, so something went wrong.

Real Example

Counting Overnight Errors

Back to Ivy. One morning she needed to know how many lab machines had reported an error overnight. She ran the night's log through grep error and wc dash l, just like Sam's muffin count, and had her answer in a second. I didn't write a program, she said. I snapped two small tools together.

Key Takeaways

Shell, Paths, Redirection, and Pipes

To recap. The shell reads your commands, the terminal holds them, and the prompt says it's ready. pwd, cd, ls and mkdir move you around the tree, with absolute or relative paths. One greater-than sign replaces a file, and two add to it. Pipes join small tools, and an exit code of zero means success.

Try It

A First Session in Terminal

Here's something to try. Open Terminal on a Mac, or PowerShell on Windows, where some names differ. Make a folder, move into it, write two lines with echo, and read them back with cat. Press Tab to finish names, and the up arrow to repeat a command.

About Introduction to Operating Systems at UPI Study

Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About The Command Line

What is the difference between a shell and a terminal?
A shell is the program that reads the commands you type and carries them out, while the terminal is the window that holds the typing. Bash is the usual shell on most Linux systems, and new Mac accounts start in zsh. The prompt, often a dollar sign, is the shell signaling that it is ready for the next command.
What is the difference between an absolute path and a relative path?
An absolute path starts at the root of the file tree, written as a single slash, so /usr/share means the same place from anywhere. A relative path starts from the shell's current working directory, so cd share works only if share sits inside the current folder. A single dot means the current folder, and two dots mean the folder above.
What is the difference between > and >> in the shell?
One greater-than sign sends output into a file and replaces everything already in it, while two greater-than signs add the output to the end of the file. Running echo five pies > orders.txt leaves only that line, even if the file held other orders. When the goal is to add, use two signs, since appending never throws anything away.
What does the pipe symbol do on the command line?
The pipe, a vertical bar, feeds the output of one command straight into the next. For example, grep muffin sales.txt | wc -l counts the lines that mention muffins, and sort sales.txt | uniq -c counts how often each item appears. The sort step matters because uniq only notices repeated lines that sit right next to each other.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Introduction to Operating Systems.

83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Introduction to Operating Systems
3 credits · NCCRS
Enroll →