2 Shell Tools
2 Shell Tools
Shell Tools
Winter 2023
Akshay Srivatsan, Ayelet Drazen, Jonathan Kula
1
Outline
3. Basic Commands
4. Pipes
5. Conclusion
2
Outline
3. Basic Commands
4. Pipes
3
UNIX
• The shell (as we recognize it) began with the UNIX operating system in 1969.1
• UNIX was made at Bell Labs by Ken Thompson and Dennis Ritchie.
• UNIX introduced what is now called “the UNIX philosophy.”
• Almost all modern computing is derived from the legacy of UNIX.
1
Dennis M. Ritchie and Ken Thompson. The UNIX time-sharing system.
Commun. ACM, 17(7):365–375, jul 1974
4
UNIX
• The shell (as we recognize it) began with the UNIX operating system in 1969.1
• UNIX was made at Bell Labs by Ken Thompson and Dennis Ritchie.
• UNIX introduced what is now called “the UNIX philosophy.”
• Almost all modern computing is derived from the legacy of UNIX.
1
Dennis M. Ritchie and Ken Thompson. The UNIX time-sharing system.
Commun. ACM, 17(7):365–375, jul 1974
4
Outline
3. Basic Commands
4. Pipes
5
Anatomy of a Computer
6
Anatomy of a Computer
6
Anatomy of a Computer
6
Anatomy of a Computer
6
Anatomy of a Computer
6
Anatomy of a Computer
6
Anatomy of a Computer
6
Anatomy of a Computer
6
Anatomy of a Computer
7
Userspace
7
Userspace
7
Userspace
Definition (userspace)
Userspace is the set of programs that come bundled with an OS kernel, which
allow a user to perform various tasks.
7
Running Programs
8
Running Programs
8
Running Programs
Definition (shell)
A shell is the outermost layer of an operating system; it lets a user run userspace
programs, which in turn let a user interact with their computer’s hardware.
8
Running Programs
Definition (shell)
A shell is the outermost layer of an operating system; it lets a user run userspace
programs, which in turn let a user interact with their computer’s hardware.
9
Types of Shell
9
Types of Shell
While all of these shells can start programs, only the UNIX shell (and its derivatives)
can combine them. 9
Types of Shell
While all of these shells can start programs, only the UNIX shell (and its derivatives)
can combine them.
9
Outline
3. Basic Commands
4. Pipes
10
Original
12
Outline
3. Basic Commands
4. Pipes
13
The UNIX File Abstraction
14
The UNIX File Abstraction
14
The UNIX File Abstraction
14
Outline
3. Basic Commands
4. Pipes
5. Conclusion
15
The UNIX Shell
16
The Prompt
Example (prompt)
An default shell prompt might look like this:
[akshay@akshays-thinkpad ~]$
This PROMPT will print every time the shell is ready to accept another command. It
probably looks different on your computer, since different shells have different
defaults. Regardless of what it looks like right now, you can customize it to look like
whatever you want. In most cases, you can get back to it by pressing CTRL-C on
your keyboard.
17
The Prompt
Username
Example (prompt: username)
This part of the prompt is your username:
[akshay@akshays-thinkpad ~]$
This is probably the same as the username you use to log into your computer. By
default, you're auto-logged into the shell using your normal user account.
18
The Prompt
Hostname
Example (prompt: hostname)
This part of the prompt is your computer’s hostname:
[akshay@akshays-thinkpad ~]$
This is your computer's name on whatever network it's connected to. Generally you
don't really care about this unless you have multiple computers.
19
The Prompt
Current Directory
Example (prompt: current directory)
This part of the prompt is your current directory.
[akshay@akshays-thinkpad ~]$
This is your WORKING DIRECTORY; “directory” is just a fancy name for “folder”, like
you'd have in Windows Explorer or macOS Finder.
By default, you start in your HOME DIRECTORY, which is the folder that contains
Documents, Downloads, Pictures, Videos, etc. The home directory is abbreviated
as a tilde (~) because it's so common.
20
Outline
3. Basic Commands
3.1 Directories
3.2 Files
4. Pipes
21
5. Conclusion
Outline
3. Basic Commands
3.1 Directories
3.2 Files
4. Pipes
22
5. Conclusion
Listing Files
In Windows Explorer or macOS Finder, the current directory is always visible. In the
shell, you have to ask for the list of current files manually.
23
Listing Files
In Windows Explorer or macOS Finder, the current directory is always visible. In the
shell, you have to ask for the list of current files manually.
The LIST command is called ls.
23
Listing Files
In Windows Explorer or macOS Finder, the current directory is always visible. In the
shell, you have to ask for the list of current files manually.
The LIST command is called ls.
Example (ls)
On my computer, the results look like this:
[akshay@akshays-thinkpad ~]$ ls
Desktop Downloads Music Public Videos
Documents Dropbox Pictures Templates
[akshay@akshays-thinkpad ~]$
24
You are here
24
You are here
24
You are here
25
Changing Directories
25
Changing Directories
25
Not Changing Directories
26
Not Changing Directories
26
Making Directories
• Now that we're on the desktop, let's create a new directory to do some
experiments in.
• This is the equivalent of right-clicking and selecting “New Folder”.
27
Making Directories
• Now that we're on the desktop, let's create a new directory to do some
experiments in.
• This is the equivalent of right-clicking and selecting “New Folder”.
The MAKE DIRECTORY command is called mkdir.
27
Making Directories
• Now that we're on the desktop, let's create a new directory to do some
experiments in.
• This is the equivalent of right-clicking and selecting “New Folder”.
The MAKE DIRECTORY command is called mkdir.
Example (mkdir)
Create a directory called “cs45-test-directory” and cd into it:
$ mkdir cs45-test-directory
$ cd cs45-test-directory/
$ mkdir cs45-test-directory
$ cd cs45-test-directory/
28
Directory Review
Shortcut Name
~ Home Directory
/ Root Directory
. Current Directory
.. Parent Directory
Table: Directory Shortcuts
3. Basic Commands
3.1 Directories
3.2 Files
4. Pipes
29
5. Conclusion
Output
30
Output
30
Output
30
Input
31
Input
31
Input
31
Input
The CONCATENATE command is called cat. It can also be used for input.
Example (cat)
Read text from the user:
$ cat
this is a test
this is a test
line 2
line 2
$
The cat command will print out whatever you type into it… forever. To get it to stop,
you can either “kill” it by pressing CTRL-C, or tell it “end of file” by pressing CTRL-D. 31
Creating Files
• There are a few different ways to create files. Let's start with the simplest.
32
Creating Files
• There are a few different ways to create files. Let's start with the simplest.
The TOUCH FILE command is called, intuitively, touch. While we don't care about
“touching” files as such, it has the handy side effect of creating files.
32
Creating Files
• There are a few different ways to create files. Let's start with the simplest.
The TOUCH FILE command is called, intuitively, touch. While we don't care about
“touching” files as such, it has the handy side effect of creating files.
Example (touch)
To create a file called “[Link]”:
$ touch [Link]
$ ls
[Link]
$
32
Renaming Files
The MOVE FILE command is called mv. It can also be used to rename files.
33
Renaming Files
The MOVE FILE command is called mv. It can also be used to rename files.
Example (mv)
To rename a file called “[Link]” to “[Link]”:
$ mv [Link] [Link]
$ ls
[Link]
$
33
Deleting Files
34
Deleting Files
Example (rm)
To remove a file called “[Link]”:
$ rm [Link]
$
34
Writing to Files
35
Writing to Files
$ ls -l
total 0
-rw-r--r-- 1 akshay akshay 0 Dec 18 11:59 [Link]
35
Writing to Files
$ ls -l
total 0
-rw-r--r-- 1 akshay akshay 0 Dec 18 11:59 [Link]
One very useful flag which is supported by almost every command is --help. 35
Writing to Files
36
Writing to Files
36
Writing to Files
36
Writing to Files
36
Reading from Files
• Just like STANDARD OUTPUT, the input to our programs is also a file.
37
Reading from Files
• Just like STANDARD OUTPUT, the input to our programs is also a file.
• By default, this is called STANDARD INPUT, and comes from our terminal.
37
Reading from Files
• Just like STANDARD OUTPUT, the input to our programs is also a file.
• By default, this is called STANDARD INPUT, and comes from our terminal.
• The shell also lets us REDIRECT standard input to come from a file.
37
Reading from Files
• Just like STANDARD OUTPUT, the input to our programs is also a file.
• By default, this is called STANDARD INPUT, and comes from our terminal.
• The shell also lets us REDIRECT standard input to come from a file.
Example (input redirection)
To print a file called “[Link]”:
37
I/O(/E?)
38
I/O(/E?)
38
I/O(/E?)
38
Redirection Operators
3
It’s uncommon to redirect standard error, but there are some valid reasons to (which we’ll see later in
the quarter).
39
Outline
3. Basic Commands
4. Pipes
5. Conclusion
40
Environment Variables
41
Environment Variables
41
All the Environment Variables
The ENVIRONMENT VARIABLE command env prints all the environment variables
which are currently set.
42
All the Environment Variables
The ENVIRONMENT VARIABLE command env prints all the environment variables
which are currently set.
Example (env)
To print every environment variable:
$ env
MAIL=/var/spool/mail/akshay
PWD=/home/akshay
XDG_SESSION_TYPE=wayland
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin
HOME=/home/akshay
USERNAME=akshay
[...]
42
Pipes
43
Pipes
43
Pipes
43
Pipes
43
Pipes
$ env | wc --lines
78
43
Benefits of Pipes
Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.
44
Benefits of Pipes
Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.
44
Benefits of Pipes
Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.
44
Benefits of Pipes
Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.
44
More Piping
45
More Piping
45
Outline
3. Basic Commands
4. Pipes
5. Conclusion
46
Getting Help
• The shell is far more complicated than we can possibly cover in an 80-minute
lecture.
47
Getting Help
• The shell is far more complicated than we can possibly cover in an 80-minute
lecture.
• Most commands have lots of flags and options.
47
Getting Help
• The shell is far more complicated than we can possibly cover in an 80-minute
lecture.
• Most commands have lots of flags and options.
• We already talked about the --help flag, which usually gives you a brief
summary of how to use a command.
47
The System Manual
48
The System Manual
48
The System Manual
48
The System Manual
$ man wc
48
The System Manual
$ man man
48
To be continued…
• We'll continue exploring shell tools in Lecture 3: Data Manipulation and Shell
Scripting.
49
To be continued…
• We'll continue exploring shell tools in Lecture 3: Data Manipulation and Shell
Scripting.
• There's no class on Monday, January 16th (Martin Luther King Jr. Day); we'll
pick back up on Wednesday, January 18th.
49
To be continued…
• We'll continue exploring shell tools in Lecture 3: Data Manipulation and Shell
Scripting.
• There's no class on Monday, January 16th (Martin Luther King Jr. Day); we'll
pick back up on Wednesday, January 18th.
• We'll post Assignment 1 soon, but it'll only be due on Monday, January 23rd
(twelve days from now). It'll cover Lectures 2 (this one) and 3 (next week).
49
To be continued… (Continued)
• In the meantime: try doing file management from the terminal. We didn't cover
every command you'll need, so if you don't know how to do something, try
searching the manual using apropos or searching the web.
50
To be continued… (Continued)
• In the meantime: try doing file management from the terminal. We didn't cover
every command you'll need, so if you don't know how to do something, try
searching the manual using apropos or searching the web.
• Take a look at the UNIX filesystem hierarchy layout, documented in man hier.
• Use man pages, [Link] or [Link] to find out
more about shell commands.
50
To be continued… (Continued)
• In the meantime: try doing file management from the terminal. We didn't cover
every command you'll need, so if you don't know how to do something, try
searching the manual using apropos or searching the web.
• Take a look at the UNIX filesystem hierarchy layout, documented in man hier.
• Use man pages, [Link] or [Link] to find out
more about shell commands.
Be Careful!
The shell often doesn't warn you when you're doing dangerous things! Be sure to
read the man page before running commands you find on the internet. Be especially
careful with the REMOVE FILE command, rm, or when using the > (overwrite) operator.
50
Sudo
51
Sudo
51
Sudo
51
Sudo
51
Sudo
51
Sudo Warning
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
52
Interesting Commands
54
References
55