C/CPS 393
Introduction to UNIX, C and C++
Prof. Alex Ufkes
Topic 1: Course intro, Linux intro, Linux basics
Notice!
Obligatory copyright notice in the age of digital
delivery and online classrooms:
The copyright to this original work is held by Alex Ufkes. Students
registered in course C/CPS 393 can use this material for the purposes
of this course but no other use is permitted, and there can be no sale
or transfer or use of the work for any other purpose without explicit
permission of Alex Ufkes.
© Alex Ufkes, 2022 2
Instructor
Alex Ufkes
aufkes@ryerson.ca
Class times (Sec 5-7):
Tue 11-1, TRS 1149
Fri 12-1, DSQ 14
Class times (Sec 8-11):
Mon 11-12, TRS 3176
Wed 8-10, TRS 3176
© Alex Ufkes, 2022 3
E-mail I check it often (aufkes@ryerson.ca)
Please put CPS393 in the subject line
Include your full name
Ideally, use your Ryerson account
© Alex Ufkes, 2022 4
You
Passed C/CPS 109, 209
© Alex Ufkes, 2022 5
Course Administration
Announcements and grades will be posted on D2L.
I (Prof. Ufkes) will post my slides on D2L.
Dr. Woit is the course coordinator, all other course
resources can be found on her website:
https://www.cs.ryerson.ca/dwoit/courses/cps393/cmf393.html
© Alex Ufkes, 2022 6
Course Notes/Slides/Content
I (Prof. Ufkes) will post my slides on D2L.
The content in my slides is based on Dr. course notes.
Instructions and conditions for accessing these notes can be
found here:
https://www.cs.ryerson.ca/dwoit/courses/cps393/courseNotesREADME
© Alex Ufkes, 2022 7
Course Synopsis
From the course catalogue:
The course introduces the UNIX operating system, and the C and C++
languages. UNIX topics include: I/O, redirection, processes, and shell
scripts. C and C++ are introduced with an emphasis on differences from
previously studied languages. C topics include pointers, structures,
memory allocation, and paradigm differences. C++ topics include class
formalisms, static and dynamic instantiation, inheritance, constructors
and destructors, polymorphism with virtual functions, operator
overloading, and time permitting, friends. Stream I/O may be introduced.
© Alex Ufkes, 2022 8
Course Topics
From Dr. website:
https://www.cs.ryerson.ca/~dwoit/courses/cps393/topics393.html
Linux Basics: Introduction, file system, commands, options, security,
permissions, devices, I/O, glob constructs, quoting, filters, pipes,
processes, shell shortcuts
Advanced Linux: Command substitution, shell options, variables,
quoting considerations, shell programming: create, test and debug
shell programs, shell decisions and repetitions, file descriptors
Additional Linux Topics (time permitting): Shell functions, process
communication, environment variables
© Alex Ufkes, 2022 9
Course Topics
From Dr. website:
https://www.cs.ryerson.ca/~dwoit/courses/cps393/topics393.html
C Basics: Variables, arithmetic expressions, functions & return values,
compiling, makefiles
Program control statements: If, else, for, while, do, break, continue, switch
Arrays & Strings: One-dimensional arrays, strings, arrays of strings
Using Pointers: pointer basics, pointers with arrays, pointers as parameters
File I/O & Console I/O: Streams, binary files, gets( ), puts( )
Structures & Unions: Structure basics, pointers to structures
© Alex Ufkes, 2022 10
Course Topics
From Dr. website:
https://www.cs.ryerson.ca/~dwoit/courses/cps393/topics393.html
C++ Basics: Differences from C, program layout,
non-OO basics, classes, object instantiation
OO-Derived Basics: Constructors: definitions,
overloading, defaults, copy, destructors,
polymorphism, virtual functions, operator
overloading, stream IO
© Alex Ufkes, 2022 11
Evaluation
From Dr. website:
https://www.cs.ryerson.ca/~dwoit/courses/cps393/cmf393.html
Labs: 0% For practice only
Midterm #1: 20% In Lab Sep 28/29
Midterm #2: 30% In Lab Nov 02/03
Final Exam: 50% During exam period
Midterms and exam take place in a lab environment, in Linux
© Alex Ufkes, 2022 12
Labs
Paraphrased from Dr. website:
https://www.cs.ryerson.ca/~dwoit/courses/cps393/cmf393.html
Labs are not graded. Provided questions are for practice only.
TAs will be available to answer questions and check your work.
All lab related inquiries should be sent to your lab TA
Our lead TA is Jorge Lopez: jlopez@ryerson.ca
© Alex Ufkes, 2022 13
Lab and Test Environment
Labs and tests will take place in Linux (obviously)
Working in Linux on CS moons
Text-based bash shell
vim* text editor
gcc and g++ compilers for C/C++
All of these can be accessed on CS moons.
In Ubuntu on a CS lab computer, or via SSH from anywhere else.
*Other text editors may or may not be permitted. TBA.
© Alex Ufkes, 2022 14
Questions So Far?
© Alex Ufkes, 2022 15
This Week:
Intro to Linux and Bash
Linux: what/who/when/why
Accessing and navigating the Bash shell
Basic shell commands, command options
Linux security and permissions
Input/Output (IO) streams, redirection
© Alex Ufkes, 2022 16
What: Linux is an Operating System
iOS/OS X Windows Android Linux
23% 28% 45% 1%
Worldwide market share across all devices - http://gs.statcounter.com/os-market-share
© Alex Ufkes, 2020, 2021 17
© Alex Ufkes, 2020, 2021 18
What: Linux is an Operating System
Many different flavors:
Ubuntu, Fedora, Debian, Solaris, FreeBSD,
dozens (if not hundreds) more.
Other operating systems are based on Linux:
ChromeOS, Android, iOS, OS X
Much of what you learn in this course
in Linux will work in a Mac or Android
terminal as well!
© Alex Ufkes, 2020, 2021 19
Linux Origins: Who and When
Started out as a Unix variant for the IBM PC.
Unix: OS from Bell Labs, ~1965
Precursor to most other operating systems
Linux is derived from Unix
Linus Torvalds, a Finnish student of computer
science, wrote the initial Linux version.
Linux was first posted on the internet in 1991.
Key to success has been the availability of free
and open-source software packages.
Highly modular and easily configured.
© Alex Ufkes, 2022 20
Why Use/Study Linux?
Widespread web usage:
77% of websites hosted on Linux/Unix servers
92% of top 25 websites in world use Linux
96% of top million servers in world run Linux
used Linux whether you knew it or not.
Linux dominates Supercomputing:
All
run Linux
https://www.networkworld.com/article/3568
616/linux-dominates-supercomputing.html
© Alex Ufkes, 2020, 2021 21
Why Use/Study Linux?
Free, open source
Lightweight, depending on distribution.
Good for embedded systems and single board
computers (ie. Raspberry PI)
Anecdotally, the vast majority of CS research is
done in Linux.
a Linux version, not always a Windows version.
© Alex Ufkes, 2020, 2021 22
Operating Systems
Linux is an Operating System
What is an operating system, exactly?
Broadly Speaking:
The OS communicates with the hardware resources of the
computer system to provide a set of services to system users.
E.g., Program execution on CPU, controlled access to files on
storage devices, access to data in main memory, etc.
© Alex Ufkes, 2022 23
Operating Systems
What this boils down to:
A computer is a set of hardware resources for the
movement, storage, and processing of data.
The Operating System is responsible for managing
these resources on behalf of the user or users
Linux is one such operating system
© Alex Ufkes, 2022 24
Operating System VS Kernel
These are not the same thing!
The Kernel is the heart of the OS that operates closest to the hardware layer.
Communication with IO devices, device drivers
Memory management, controlled access to filesystem
Process management and scheduling, program execution
Internet Explorer is (was?) part of Windows, but
run in Kernel mode or have privileged access to hardware.
© Alex Ufkes, 2022 25
Interacting with the OS/Kernel
On your phones, you do this using touchscreen input.
In the Windows desktop, you can use a mouse to click
icons, navigate start menu, Windows explorer, etc.
In Linux, in this course, we will use the keyboard to type
commands into a shell window.
Many Linux shells:
Bourne Shell (sh)
Korn Shell (ksh)
C Shell (csh)
Bourne Again Shell (bash)
© Alex Ufkes, 2022 26
Windows has its own shells (cmd, PowerShell), and many
Linux distributions have graphical desktops (Ubuntu).
© Alex Ufkes, 2022 27
CS Dept. Linux Servers
metis, elara, europa, thebe
anywhere using SSH.
Personal computer, lab computer, Windows computer,
Windows 10 and later has SSH out of the box
When you SSH into one of the moons, commands
entered in that shell are executed on the Linux server
© Alex Ufkes, 2022 28
Logging in Using SSH
Note: For complete info on logging into CS lab
computers in general, see this link:
http://www.scs.ryerson.ca/user_guide.php
For Windows users wanting to use SSH on their
personal machines, you may have to follow these
steps to enable SSH in Windows:
https://interworks.com/blog/2021/09/08/how-to-enable-ssh-commands-in-windows/
© Alex Ufkes, 2022 29
Logging in Using SSH: Lab Ubuntu Desktop
Use keyboard shortcut Ctrl-Alt-T to open a local
Can also right-
Important!
To log into moon, type:
ssh moon.cs.ryerson.ca
Enter your usual userid/password (if prompted)
© Alex Ufkes, 2022 30
Logging in Using SSH: Everywhere Else
Open whichever basic terminal program you prefer.
Use only a basic terminal, no fancy IDEs.
Such IDEs do things for you that you should be doing
yourself. E.g., graphical file tree navigation, etc.
Use SSH similarly from this terminal, but this time include CS userID:
ssh aufkes@moon.cs.ryerson.ca
© Alex Ufkes, 2022 31
Going forward, the slides/notes assume you
are logged into moon, in a shell window
happen (hopefully)
Almost everything we do in the next
couple weeks will be from this prompt!
© Alex Ufkes, 2022 32
© Alex Ufkes, 2022 33
Changing Password, Logging Out
by typing passwd at the prompt.
Do not forget your password. I cannot help you. The TAs
the system admins, and they will taunt you mercilessly.
To log off moon at any time, type exit or logout in
the prompt.
© Alex Ufkes, 2022 34
Files & Filesystems
© Alex Ufkes, 2022 35
Files & Filesystems:
-based Linux shell, not a graphical file tree.
© Alex Ufkes, 2022 36
Files & Filesystems
File organization in Linux is like Windows and Mac, we
In Linux, we have both files and directories.
Both are treated the same, however. Therefore:
The term file can refer to file or directory
The term entry can also refer to file or directory
Rules:
File names are case sensitive MyFile and myfile are different.
Max length of 255 characters, though this can vary by platform.
Suffix or file extension not required myfile and myfile.txt are both OK
© Alex Ufkes, 2022 37
File Organization
Organized like a tree, where the top of the tree is called root (or root directory)
Working directory:
in
Root directory
When you log in, you
start in /home/<userid>
/home/aufkes
/home/dwoit
Etc.
home/chara adool:I
© Alex Ufkes, 2022 38
Filesystem Navigation
We can change the working directory using the cd command:
Home directory
abbreviated to ~
/usr/courses
The pattern is cd, followed by a path.
The path in this example starts from
This is called an absolute path
© Alex Ufkes, 2022 39
Absolute VS Relative Path
Alternatively, we can provide a relative path:
A relative path is just that, relative.
Relative to the working directory.
Absolute path starts with /, relative
does not.
© Alex Ufkes, 2022 40
Absolute VS Relative Path
Relative path can jump multiple levels of the file tree:
Shortcut to
home dir
© Alex Ufkes, 2022 41
Absolute VS Relative Path
Going back up the tree? Use .. to go to parent directory
© Alex Ufkes, 2022 42
Absolute VS Relative Path
Going back up the tree? Use .. to go to parent directory
Multiple levels at a time!
© Alex Ufkes, 2022 43
Display Directory Contents
Navigation with cd
Use ls to display contents of working directory
© Alex Ufkes, 2022 44
More Linux Commands
cd <path> Change directory using absolute or relative path
cd .. Move up to parent directory
ls List contents of current directory (dir in Windows)
ls <path> List contents of <path>
© Alex Ufkes, 2022 45
More Linux Commands
cd <path> Change directory using absolute or relative path
cd .. Move up to parent directory
ls List contents of current directory (dir in Windows)
ls <path> List contents of <path>
pwd Print working directory
Useful when working directory is
abbreviated or not displayed in the shell!
© Alex Ufkes, 2022 46
More Linux Commands
cd <path>
cd ..
cat <file> Display file contents, no paging
ls tac <file> Like cat, but lines in reverse order
ls <path>
pwd
© Alex Ufkes, 2022 47
More Linux Commands
cd <path>
cd ..
more Like cat, but paged (cat displays everything at once)
ls spacebar gives next screenful, enter gets next line, q quits
ls <path>
pwd
cat <file>
tac <file>
© Alex Ufkes, 2022 48
More Linux Commands
cd <path>
cd ..
more Like cat, but paged (cat displays everything at once)
ls spacebar gives next screenful, enter gets next line, q quits
ls <path>
pwd
less Like more, has extended commands/features. How ironic.
cat <file>
tac <file>
© Alex Ufkes, 2022 49
More Linux Commands
cd <path>
cd ..
cp <source> <dest> copies <source> to <dest>
ls works for directories as well
ls <path>
pwd
cat <file>
tac <file>
more
less
© Alex Ufkes, 2022 50
More Linux Commands
cd <path>
cd ..
cp <source> <dest> copies <source> to <dest>
ls works for directories as well
ls <path>
pwd
rm <file> deletes (removes) a file
cat <file>
tac <file>
more
less
© Alex Ufkes, 2022 51
More Linux Commands
cd <path>
cd ..
mkdir <name> Create a new directory called <name>
ls rmdir <name> remove/delete directory. Must be empty!
ls <path>
pwd
cat <file>
tac <file>
more
less
cp
rm
© Alex Ufkes, 2022 52
More Linux Commands
cd <path>
cd ..
mkdir <name> Create a new directory called <name>
ls rmdir <name> remove/delete directory. Must be empty!
ls <path>
pwd
cat <file>
tac <file>
more
less
cp
rm
© Alex Ufkes, 2022 53
Delete all nested files and directories manually
(booo) or use an option
© Alex Ufkes, 2022 54
More Linux Commands
cd <path>
cd ..
mv <src> <dest> Dest can be
ls in a different directory, thus moving the file.
ls <path>
pwd
cat <file>
tac <file>
more
less
cp
rm
mkdir
rmdir
© Alex Ufkes, 2022 55
To access parent directory, we use ..
Current directory is represented using a single dot .
When using mv (and other commands) we can do this:
the file name, just use . to
© Alex Ufkes, 2022 move to current directory 56
More Linux Commands
cd <path>
cd ..
wc <file> Gives size of file (lines, words, chars)
ls
ls <path>
pwd
cat <file>
tac <file>
more
less
cp
rm
mkdir
rmdir
mv
I only count three lines,
\n at the end)
© Alex Ufkes, 2022 57
More Linux Commands
cd <path>
cd ..
wc <file> Gives size of file (lines, words, chars)
ls touch <file> Creates an empty file called <file>
ls <path>
pwd
If file exists, update modification time
cat <file>
tac <file>
more
less
cp
rm
mkdir
rmdir
mv
© Alex Ufkes, 2022 58
More Linux Commands
cd <path>
cd ..
wc <file> Gives size of file (lines, words, chars)
ls touch <file> Creates an empty file called <file>
ls <path>
pwd
If file exists, update modification time
cat <file> # Comments out everything following the #
tac <file>
more
less
cp
rm
mkdir
rmdir
mv Notice: Multiple arguments
to touch, perfectly legal.
© Alex Ufkes, 2022 59
Quality of Life Features
cd <path>
cd ..
ls
ls <path>
pwd Not universal, but most terminals support the following:
cat <file>
tac <file>
Use the UP arrow key to cycle through previously
more entered commands
less
cp
Use the TAB key to autocomplete. Great for long
rm filenames or directories. Only works if no ambiguity.
mkdir
rmdir
mv
wc
touch
#
© Alex Ufkes, 2022 60
Practice Exercise
cd <path>
cd ..
From Dr. notes:
ls
ls <path> Give the sequence of commands that you would issue from your home
pwd directory to do the following, in the order given:
cat <file>
Create 2 directories, lab1 and lab2,
tac <file>
more
Create a subdirectory of lab1 called documentation;
less Copy file /usr/courses/cps393/dwoit/labs/doc.txt into your directory
cp called documentation;
rm Move your documentation/doc.txt into directory lab2 instead;
mkdir List the contents of directory documentation using an absolute path;
rmdir List contents of directory documentation again using a relative path;
mv Make directory lab2 your current working directory;
wc
List contents of file doc.txt;
touch
#
Move back into your home directory;
Delete your file doc.txt, then delete directory documentation, then,
using an absolute path, delete lab1 and lab2.
© Alex Ufkes, 2022 61
Linux Commands: Options
Adding options to a command modifies the
behavior of that command from the default.
Rather than having five similar commands,
have one command with five options.
Options follow the command itself, prefixed
with a dash -
© Alex Ufkes, 2022 62
Linux Command Options: rm
Remember this?
© Alex Ufkes, 2022 63
Linux Command Options: rm
rm r <dir>
dir
and all files and sub-dirs
© Alex Ufkes, 2022 64
Linux Command Options: ls
ls t sorts newest to oldest
ls R recursively lists all subdirs and files
ls p print / following directory names
ls l long list, more properties
Can combine options! These are all the same,
and order us usually irrelevant:
ls l t ls t l
ls lt ls -tl
© Alex Ufkes, 2022 65
Manual Pages
Memorizing commands is hard enough, let alone all their options
Luckily, Linux comes with a built in manual accessible from the command line
Called the manual pages
Accessed from the shell using the man command:
© Alex Ufkes, 2022 66
man man
A manual for how to use the manual
© Alex Ufkes, 2022 67
man <command>
The simplest (and most primitive) way to get help:
© Alex Ufkes, 2022 68
Use UP and DOWN arrows to
navigate
Options are listed near the top,
with their descriptions
© Alex Ufkes, 2022 69
Use UP and DOWN arrows to
navigate
Options are listed near the top,
with their descriptions
man page for ls is 284 lines long
We will learn many commands
and many options
Impossible to cover everything
Familiarize yourself with the
man pages, get comfortable
navigating them
© Alex Ufkes, 2022 70
man -k <query>
Performs a keyword search for <query>
man k zip
These commands
can be looked up in
man as well!
© Alex Ufkes, 2022 71
© Alex Ufkes, 2022 72
Manual Pages
They can also be found online.
Great idea to have a browser open alongside
your shell window when practicing
https://linux.die.net/man/
https://www.gnu.org/software/bash/manual/html_node/
Do not rely entirely on web-based manual,
as tests may/will require bash shell only.
© Alex Ufkes, 2022 73
info
More user-friendly (maybe? I guess?) version of man:
Try it out, YMMV
© Alex Ufkes, 2022 74
Security &
Permission
© Alex Ufkes, 2022 75
Security & Permissions
Question: blackhats from doing this?
rm r <some important system directory>
Each user has a userid, password, and home directory.
By default, only userid may manipulate files under
/home/userid
You do not have permission to manipulate important
system files existing outside your home directory
You do not have permission to manipulate files under
© Alex Ufkes, 2022 76
Owners and Groups
Each file and directory has an owner and a group
associated with it. This can be seen using ls -l:
Owner Group
Here, I, aufkes, am the owner of both
files, and the group is named aufkes.
© Alex Ufkes, 2022 77
Owners and Groups
Each file and directory has an owner and a group associated with it
When you create a file, you become its owner
Users have one primary group but can join many others.
For example, all of you are in the cps393 group on moon.
When users join a group, they gain access to files and
folders with that group ID.
user was in when that file was created.
Typically your primary group (aufkes for me)
© Alex Ufkes, 2022 78
Creating and Joining Groups
In Linux generally, users can create groups and add
other users to those groups.
On CS moons, only the sysadmins have this power.
I, and Dr. Woit, must request that the sysadmins add
you all to cps393, for example.
What about
this stuff?
Owner and group
© Alex Ufkes, 2022 79
Permissions
What about
this stuff?
I >
directory
u g o Each of:
owner group everyone
else
owner (u) group members (g) other users (o)
I can read/write hello.txt have a set of :
read (r) write (w) execute (x)
permissions.
© Alex Ufkes, 2022 80
For
; ÷ %•
-
mm
'
* Hit
an
Haniya
Neeley *agar and
*
Yana Hagiwara rwx
file: Can be looked at, copied
r dir: Can look at contents (with ls), but cannot view files within.
Would need r perm on file, and x perm on directory.
file: Can modify or delete the file
w dir: Can add or delete entries to/from directory.
A file in the directory can be modified without w on dir.
file: Can run it, if it is runnable (executable)
X dir: Can access entry in dir, if its name is known. Cannot list
contents. Weaker than r permission.
© Alex Ufkes, 2022 81
Examples: More in Dr. Notes
dmason wants to keep all his files/dirs private:
\
Perms?
Group and other have no permissions at all: ---
aufkes wants anyone who knows the name and location of
/home/aufkes/labs/myfile to be able to execute it, but does not
want anyone to be able to copy it, see its contents, or delete it
Perms?
Exactly --x for other on myfile
At least --x for other on all dirs down filetree to myfile so
anyone can GET to it: /home, /home/aufkes, /home/aufkes/labs
© Alex Ufkes, 2022 82
-w-: Write Without Read?
Seems strange but can be done!
If a user has -w- permissions on a file, that file
can be changed but not read.
How can we change it? Not in an editor, as that
would require viewing it.
There are other ways to modify a file that do not
require viewing it.
Remember file I/O you may have done in Python
or Java? Open the file in write mode.
Can also use echo and input redirection. Later.
© Alex Ufkes, 2022 83
Changing Permissions
ls l, how can we change them?
Only the owner can change permissions, and
can do so using the chmod command
chmod (ugoa)(+-=)(rwx) name(s)
(ugoa) user, group, other, all
(+-=) + adds perm, - removes perm, = sets to given perm
(rwx) read, write, execute
© Alex Ufkes, 2022 84
chmod: Usage Examples
If the original permissions for myFile are -rwxr-xr--
-rwxr-xr-- # Original
chmod g+w myFile -rwxrwxr-- # Added write to group
chmod ug-x myFile -rw-rw-r-- # Removed x from u,g
chmod a+r+w+x myFile -rwxrwxrwx # Added r,w,x to all
chmod go=rx myFile -rwxr-xr-x # set g,o to r-x
chmod g=x,o+w myFile -rwx--xrwx # set g to x, added w to o
© Alex Ufkes, 2022 85
This is default for user-created
files, set by sysadmins
© Alex Ufkes, 2022 86
chmod: Alternative Permission Format
Notice that rwx triple can be represented in binary:
--- (000) 0
--x (001) 1 Use a 3-digit octal literal to represent
-w- (010) 2 ugo permissions in one shot:
-wx (011) 3
chmod 160 myFile
r-- (100) 4
--x for user, rw- for group, --- for others
r-x (101) 5
rw- (110) 6 Not as intuitive, but potentially much
rwx (111) 7 faster to type
© Alex Ufkes, 2022 87
Practice Exercise
From Dr. notes:
Create a file called tst1, and a directory called
dog, in your home directory.
Copy tst1 to a file called cat in directory dog.
Move into dog.
Remove write and execute permissions for
user (owner) on file cat.
Now try to erase cat.
What happens? Why? How can you erase it?
(give two different ways, if possible)
© Alex Ufkes, 2022 88
Input/Output (IO) Streams
ls writes information to the terminal (output)
passwd writes to the terminal (output) before
reading in a new password from the keyboard (input)
How does the shell know where to read from (keyboard)?
Where to write to (terminal window)?
© Alex Ufkes, 2022 89
CPS209 Reminder
Streams are a core concept in Java I/O
System.out, System.err are output streams
System.in is an input stream
Conceptually, a stream is an endless flow of data.
A stream is a software construct, connected to the
actual data source (or destination).
Java programs can read from and write to a stream.
© Alex Ufkes, 2022 90
CPS209 Reminder
A stream is a software construct, connected to the actual data
source (or destination).
Input
Source Program
Stream
Output
Program Destination
Stream
© Alex Ufkes, 2022 91
CPS209 Reminder
A stream is a software construct, connected to the actual data
source (or destination).
Keyboard System.in Program
Program System.out Terminal
© Alex Ufkes, 2022 92
IO Streams in Linux
The shell assigns three standard streams to any command:
stdin When command needs to read input, it looks in
stdin, and reads whatever is there.
stdout When command produces output, it sends it
down stdout.
stderr When command produces error message, it
sends it down stderr.
© Alex Ufkes, 2022 93
IO Streams in Linux
stdin, stdout, stderr are typically attached to devices:
stdin passwd stdout
stderr
© Alex Ufkes, 2022 94
IO Streams in Linux
stdin passwd
Realize that passwd has no idea what the keyboard is.
It reads from stdin, whatever that happens to be
connected to.
The same is true of stdout, stderr, and the monitor.
© Alex Ufkes, 2022 95
What About Files?
We tell the commands cat and wc to ignore stdin and use hello.txt instead:
cat hello.txt # read from file hello.txt, not stdin
wc hello.txt # read from file hello.txt, not stdin
hello.txt cat,wc
© Alex Ufkes, 2022 96
Huh?
wc with no arguments reads from stdin until
EOF is reached (wc also reads files until EOF).
Try it! Type stuff, then enter EOF using CTRL-D
Try it using cat as well, see how it differs
© Alex Ufkes, 2022 97
IO Redirection
Redirect stdout to a file instead of monitor using > or >>
Interesting!
lsfile created before ls
command executes!
> will overwrite file if it
exists, create it if not.
>> appends if file exists
© Alex Ufkes, 2022 98
IO Redirection
Can redirect stdin also! And both at the same time!
wc input redirected from lsfile
(like calling wc with file arg,
subtle difference here)
Output redirected to wcfile
© Alex Ufkes, 2022 99
Redirect stderr? Sure, why not?
Use 2>
© Alex Ufkes, 2022 100
Redirect stderr? Sure, why not?
Use 2>
Why 2>?
Each stream given a number by shell.
Stdin = 0, stdout = 1, stderr = 2
ls >outfile is same as ls 1>outfile
cat <infile is same as cat 0<infile
Redirect stdout and stderr at
the same time using &>
© Alex Ufkes, 2022 101
Devices
Disks and printers and modems and so on
In Linux, we access devices as though they were (special) files
Typically found in directory /dev
/dev/stdin
stdin/stdout/stderr are: /dev/stdout
/dev/stderr
Displaying file contents on stderr could be done:
cp hello.txt /dev/stderr
© Alex Ufkes, 2022 102
Devices
© Alex Ufkes, 2022 103
Devices
When OS recognizes operation
on a special file (device), it calls
the appropriate device driver to
perform the operation.
© Alex Ufkes, 2022 104
/dev/null
Data goes in, cannot come back out.
Sending data to /dev/null throws it
away forever
/dev/null Common to send errors here
We know the errors are happening
Out of sight out of mind!
© Alex Ufkes, 2022 105
/dev/null
Imagine a long list of files for wc, some may not exist.
do exist:
Handy!
© Alex Ufkes, 2022 106
Week 1
© Alex Ufkes, 2022 107
In Summary
Intro to Linux and Bash
Linux: what/who/when/why
Accessing and navigating the Bash shell
Basic shell commands, command options
Linux security and permissions
Input/Output (IO) streams, redirection
© Alex Ufkes, 2022 108
© Alex Ufkes, 2022 109