IT 244: Introduction to Linux/Unix
Class 14 - Midterm Review
Tips and Examples
Review
Microphone
Homework 7
I have posted homework 7
here.
It is NOT due this coming Sunday.
Instead it is due Sunday, October 27th.
This is to give you time to study for the midterm.
And to give me time to score it.
Midterm
The Midterm exam for this course will be held on Tuesday,
October 22nd.
It will be given in this room.
It will consist of 25 questions like those on the quizzes.
60% of the questions will come from the Graded Quizzes.
The other 40% of points will be questions that I create specially for this exam.
For these questions you will have to know
- Absolute and relative pathnames
- The PATH system variable
- Access permissions
- Redirection & and pipes
- grep
- Utilities
Today's class will be a review session.
You will only be responsible for the material in today's Class Notes
on the exam.
If for some reason you cannot take the exam on the date mentioned above
you must contact me to make alternate arrangements.
The Midterm is given on paper.
I scan each exam paper and upload the scans to Gradescope.
I score the exam on Gradescope.
You will get an email from Gradescope with your score when I am done.
The Midterm is a closed book exam.
You are not allowed to any resource, other than what is in your head,
while taking the exam.
Cheating on the exam will result in a score of 0 and will be reported to the Administration.
Remember your Oath of Honesty.
To prevent cheating, certain rules
will be enforced during the exam.
Remember, the Midterm and Final determine 50% of your grade.
Quiz 5
I have posted the answers to Quiz 5
here.
No Class Exercise or Class Quiz
Today is a review session.
There will no Class Exercise or Class Quiz today.
Questions
Are there any questions before I begin?
Studying for the Midterm with Flashcards
- 60% of the point on the Midterm come from Class Quiz Questions
- You can use the flashcards you create for these questions
when studying for the Midterm
- But not all Class Quiz Questions will appear on the Midterm
- If the flashcard question covers a topic not in the Midterm Review ...
- you do not have to study it for the exam
- You should remove these flashcards from your collection
Review
Retrieving Your Last Command Line Entry
- To retrieve the previous command, hit the up arrow key, ↑
- You can do this several times to go back to any previous command
- The down arrow key, ↓, takes you in the opposite direction
Aborting a Running Program
- You can abort a running program using Control C
Getting Help with Unix Commands
- Many Unix utilities have a help option
- The help option provides some basic information about the command
- The help option for GNU programs is
--help
- For more information, use
man
or info
- Follow
man
or info
with the name of the command
Special Characters
Quoting and Escaping
- You can turn off the special meaning of a character
by preceding it with a backslash, \
- You can also turn off special meanings by enclosing a string in quotes
- You can continue a command onto the next line
by typing a backslash just before hitting Enter
- The backslash turns off the special meaning of the newline character
cd
- Change Directory
- To change your directory use
cd
- If you run
cd
without an argument, it will take you to your home directory
- To go to the directory above your current directory, use
cd ..
pwd
- Show Your Current Directory
pwd
will print you your current location in the filesystem
pwd
takes no arguments
ls
- List the Contents of a Directory
ls
lists the contents of a directory
- To see the contents of directory work, run
ls work
- When you use
ls
without an argument ...
- it lists the contents of the current directory
- For more information run
ls
with the -l
(long) option
ls -a
displays the "invisible" files
whose name begins with a .
cat
- Print the Contents of a File
- To display the contents of a file, use
cat
rm
- Delete a File
- To remove a file, use
rm
- To remove all files in a directory, use
rm *
- Be very careful when you use this construction
- There is no file recovery mechanism in Unix
rm
will not remove a directory unless you use a special option
mkdir
- Create a Directory
- You create a directory using
mkdir
rmdir
- Delete a Directory
rmdir
is used to remove a directory
rmdir
will not work unless the directory is empty
cp
- Copy Files
cp
copies files or directories
- To copy a directory and all its files and sub-directories ...
- use
cp
with the -r
option
mv
- Move a File or Directory
- Use the
mv
command to move a file or directory from one place to another
- To rename a file or directory, you also use
mv
echo
- Print Text to the Terminal
echo
prints text to the terminal
$ echo Hello
Hello
- You can use
echo
to print the value of a system variable
- But you must put a $ before the variable name
$ echo $SHELL
/bin/bash
- Pagers are programs that display the contents of a file one screenful at a time
- The two pagers that Unix supplies are
more
and less
- Hitting the Space bar advances to the next screen
- Hitting the Enter or Return key takes you down one line
less
, just to be confusing, has more features than more
Pathname Completion
- When typing a long file name it is easy to make a mistake
- To make life easier, Unix provides a feature called
pathname completion
- You type a few characters, then hit the Tab key
- Unix will supply the rest if there is only one file or directory that
matches
- If there is more than one match ...
- Unix will supply as much as it can and then beep
- If there is no match, it will also beep
- If you don't get a complete match, hit Tab a second time
- Bash will display a list of all possible matches
grep
- Finding Strings inside Files
head
- View the Top of a File
head
prints the first 10 lines of any file
- If you want a different number of lines ...
- follow
head
with a dash, - , and a number
head -20 foo.txt
tail
- View the Bottom of a File
tail
prints the last 10 lines of any file
- If you want a different number of lines ...
- follow
tail
with a dash, - , and a number
tail -20 foo.txt
sort
- Print a File in Sorted Order
sort
prints a sorted list of the lines in a file to the terminal
sort
does not change the file
sort
prints the lines of a file sorted by the first few characters in the line
- To sort in reverse order use the
-r
option
sort
, by default, sorts in alphabetical order
- This is a problem when the first characters on a line are numbers
- That's because 11 will sort before 2
- To sort in numerical order use
sort -n
- To sort in reverse numerical order use
sort -nr
Pipes - Stringing Programs Together
which
- Finding a Program File
finger
- Get information on Users
finger
, like mv
, has two functions
- When run with a name or username as an argument
finger
provides information about that user
$ finger ghoffman
Login: ghoffman Name: Glenn Hoffman
Directory: /home/ghoffman Shell: /bin/bash
On since Thu Oct 20 07:00 (EDT) on pts/12 from 130.44.154.89
3 seconds idle
Mail forwarded to glenn.hoffman@umb.edu
New mail received Fri Jul 8 13:31 2022 (EDT)
Unread since Fri Jul 8 13:30 2022 (EDT)
Plan:
Office: Science 3-92A (Pterosaur poster on door)
Office Hours: Tuesday & Thursday, 12:30 - 1:30 PM and 5:30 - 6:30
Classes: IT 244-11 TuTh 11:00 - 12:15 McCormack M03-0430
IT 116-11 TuTh 2:00 - 3:15 McCormack M03-0430
IT 117-11 TuTh 4:00 - 5:15 Wheatley W01-0061
Home Address: 40 Central Street
Somerville, MA 02143
617-821-3936 (9am - 9pm only)
- When used without an argument,
finger
shows every user currently logged in
$ finger
Login Name Tty Idle Login Time Office Office Phone
emperor Jean Gerard pts/13 7:14 Oct 19 19:34 (73.61.22.4)
emperor Jean Gerard pts/20 8:41 Oct 19 19:46 (73.61.22.4)
ericode Eriksson Xavier Osori pts/0 4d Oct 15 13:39 (10.200.5.52)
ericode Eriksson Xavier Osori pts/23 4d Oct 15 18:09 (10.200.5.52)
estevao Estevao Lordeiro pts/10 1:54 Oct 20 05:15 (66.31.196.187)
gghinita Gabriel Ghinita pts/4 18 Oct 20 04:25 (176.202.52.62)
ghoffman Glenn Hoffman pts/12 Oct 20 07:00 (130.44.154.89)
kkfletch Kenneth Fletcher *pts/19 1d Oct 13 17:00 (172.18.23.172)
ldavid Leonard David *pts/9 9 Oct 20 06:58 (172.18.23.176)
ldavid Leonard David pts/11 11 Oct 20 06:41 (172.18.23.176)
morgan Sean Geary pts/25 13:14 Oct 19 17:50 (10.200.5.63)
rickm Richard Martin *pts/8 1d Oct 18 22:23 (24.147.140.64)
rouilj John P. Rouillard *pts/6 7:32 Oct 13 13:44 (71.184.142.113)
teebanj Tayo Banjo pts/14 16:30 Oct 12 15:27 (158.121.33.16)
tmullaly Thomas Mullaly *pts/1 16:55 Oct 17 16:13 (158.121.33.14)
tmullaly Thomas Mullaly *pts/15 1d Oct 17 19:06 (216.180.91.178)
The Hierarchical Filesystem
- Unix uses a hierarchical filesystem
- This means there is one directory at the top, called the
root directory
- The root directory is indicated by a simple slash character, /
- All other directories are contained within the root directory ...
- or one of its many subdirectories
- This structure is called a tree because it looks like a tree turned upside down
- A hierarchical filesystem also resembles a family tree
- So we often use terms that describe family members when talking about directories
- The directory up one level from your current directory is called the
parent directory
- All directories above the current directory are called
ancestors
- All directories inside the current directories are called
child directories
- All directories below the current directory can be called
descendants
- All directories and files within the same parent directory are called
siblings
Case Sensitivity
- Unix is case sensitive
- This means that "Foo", "foo" and "FOO" are three different things
- At least as far as Unix is concerned
Filename Extensions
- Extensions are characters that appear at the end of the filename ..
- after a period
- Extensions are not recognized by the Unix filesystem
- As far as Unix is concerned they are just legal characters that are part of the filename
- Some Unix programs expect their files to have certain extensions
- For example, the C compiler,
gcc
expects the filenames of source code files to end in .c
- The Java compiler,
javac
expects Java source files to have .java
at the end of the filename
- These extensions are required by the program not by Unix
Current Directory
- When you connect to a Unix machine you always have a position in the filesystem
- This location is your
current directory
- Relative pathnames always start with your current directory
Your Home Directory
Navigating the Hierarchical File Systems
- Any file or directory in the filesystem will be one of four positions ...
- relative to your current directory
- It can be inside your current directory
- It can be below your current directory
- It can be above your current directory
- It can be off to the side of your current directory
- In this last case, you must go up ...
- before you can go down to reach the file
Attendance
Hidden Filenames
- A file whose filename begins with a period, . , is a "hidden" or "invisible" file
ls
does not display these files unless you use the -a
option
- These files are used to configure your Unix environment
The . and .. Directory Entries
- Every directory has at least two entries . and ..
- When a new directory is created these are the first two entries
- . stands for the current directory
- .. stands for the parent directory of your current directory
- .. is the directory immediately above your current location
- . is most often used in two circumstances
- To run a program in your current directory
- To move or copy a file to your current directory
Pathnames
- Every file has a
pathname which is used to access the file
- A pathname has two components
- The name of the file
- A path to reach the file
- The path is a list of directories that you must go through ...
- to reach the file you want
- A pathname is like an address on a letter
- A name and directions to get there
- The name of the file is always at the end of a pathname
- Slash, / is used to mark the end of a directory name
- When a / is the first character in a pathname it stands for the root directory
- There are two types of pathnames
Absolute Pathnames
- The top of the filesystem is a directory called the root directory
- The root directory is represented by a single slash character, /
- It can stand alone ...
- or appear as the first character before a directory name
- An absolute path
is a list of directories starting with the root directory ...
- and ending with the directory that contains the file
- When you add the filename to the end of an absolute path ...
- you have an
absolute pathname
Tilde ~ in Pathnames
Relative Pathnames
- Absolute pathnames are useful because you can use them anywhere
- But they are long and easy to mistype
- For most purposes, it is easier to use
relative pathnames
- In a relative pathname, the path starts from your current directory
- In an absolute pathname, the path starts from the root, /
- All absolute pathnames start with a slash, /,
or a tilde, ~
- Relative pathnames never do
- As far as Unix is concerned ...
- it makes no difference whether you use an absolute ...
- or relative pathname
- There are four types of relative pathnames
- When the file is in your current directory
- When the file is in a subdirectory of your current directory
- When the file is in a directory that is an ancestor of your current directory
- When the file is in a directory that is neither an ancestor or descendant of the current directory
Relative Pathnames in Your Current Directory
- A relative pathname of a file or directory inside your current directory is simple
- It is the name of that file or directory
Relative Pathnames in a Subdirectory
- Things get a little more complicated ...
- when you are dealing with a file in a subdirectory
- Here, you must list every directory between your current directory ...
- and the file you want
- You must put a slash, / after each directory name
Relative Pathnames above the Current Directory
- When the file or directory is above the current directory ...
- you can't list the directory names
- Instead, you have to use the special .. entry in each directory
- Use one .. for each directory up the chain in the path
- Use a slash, /, between each ..
Relative Pathnames Neither above Nor below the Current Directory
- What if the file is neither above nor below?
- Here you have to go up to a common ancestor ...
- and then down to the directory that holds what you want
- The path starts with one or more ..
- You keep going up until you get to a directory ...
- that is the ancestor of your current directory ...
- and an ancestor of the file you are trying to reach
- Once you get to the common ancestor ...
- you go down to the directory that holds the file
Access Permissions
- All Unix files and directories have
access permissions
- The access permissions can only be changed by the owner of a file or directory
- The permissions determine who gets to do what with the file or directory
- By default, the owner of a file or directory is the user account that created it
- Every file, directory or device on a Unix filesystem has three types of permissions
- Each access permission can either be on or off
- If you have read permission
on a file you can look at the data in the file
- If you only have read permission, you cannot change a file
- To change a file you need write permission
- You cannot run a program or script file ...
- unless you have
execute permission on that file
- Each of the three permissions is set either on or off to three classes of users:
- The owner
- The group
- Every other account
- Every file or directory has an owner
- The account that created the file is the owner
- A group is a collection of Unix accounts
- A group can only be set up by a system administrator
- Every file or directory is assigned to a group
- The last class of users is everyone else
- Any account that is not the owner or a member of the group
Viewing Access Permissions
chmod
Using chmod
with Numeric Arguments
The root Account
- On every Unix or Linux system there is a special account named
root
- root can access any file or run any program
- root is an administrator account
- It is used for system configuration and maintenance
Directory Access Permissions
- Access permissions work differently for directories ...
- than they do for files
- Read and write permissions for a directory are similar to those for a file
- Read permission allows you to list the contents of that directory using
ls
- Write permission allows you to create, delete or change the name of
entries in that directory
- Write permission on a directory
does not allow you to change the contents of a file in that directory
- Write permission on a directory does not apply to the directory itself
- If you have write permission on a directory you can change what's inside it
- But you cannot rename or delete the directory itself
- To do that you must write permission on its parent directory
- Execute permission on a directory allows you to enter it using
cd
Links
- Links
are like shortcuts on a Windows machine ...
- or aliases on a Mac
- Links allow you to move around the filesystem using short names
- Each of you has an entry in your home directory called it244
- In the home directory of my test account, it244gh, I have such a link
$ ls -l it244
lrwxrwxrwx 1 it244gh faculty 34 Sep 2 13:10 it244 -> /courses/it244/s24/ghoffman/it244gh
- This is a link to/courses/it244/s24/ghoffman/it244gh
- If you
cd
into this location and use pwd
$ pwd
/home/it244gh
$ cd it244
$ pwd
/home/it244gh/it244
- This path reflects the route you took to get here
- But it is not the real pathname of the directory
- You can only see that if you use
pwd
...
- with the
-P
(note the capitalization) option
$ pwd
/home/it244gh/it244
$ pwd -P
/courses/it244/s24/ghoffman/it244gh
The Two Types of Links
- There are two types of links
- Hard links
- Symbolic, or soft, links
- Hard links are older
- A hard link is like a duplicate file name
- Hard links can only point to files not directories
- You can only have a hard link to a file on the same disk partition
- Symbolic links
are much more flexible
- You can use either an absolute or relative pathname when creating a symbolic link
- A symbolic link can point to a file or directory anywhere in the filesystem
- Deleting a symbolic link does not delete the file or directory it points to
ln
Running a Program in Unix
- When you run a program in Unix you create a
process
- A process is a running program
- Processes have resources
- Memory in RAM in which they run
- The data streams standard input, standard output, and standard error
- Time on the CPU to do their work
- Bash cannot create a process
- Only the kernel can
- A process starts when you type a command and hit Enter
- Bash asks the kernel to create a process for the program
- It then goes to sleep until the process ends
- When the process ends it sends a status code back to the kernel
- The kernel then wakes up Bash and gives it the status code
- A status code of 0 means the process ran without error
- A number greater than 0 indicates some kind of error
Command Options
- Options modify the behavior of the command
- Options are usually preceded by one or two dashes, -
- GNU programs frequently have options that are preceded by two dashes, --
- The options in GNU programs are usually words
The PATH System Variable
Running a Program in the Current Directory
- For security reasons, you shold never put the current directory,
. ...
- in the PATH list
- Then how do you run a program inside your current directory?
- You can do this using the following construction
./PROGRAM_NAME
- This will always work regardless of the contents of PATH
Standard Input, Standard Output and Standard Error
- Every Unix process always has access to three different data streams
- Standard Input
- Standard Output
- Standard Error
- Standard input
is where the program normally gets input
- By default, standard input is the keyboard
- Standard output
is where the program normally sends its output
- By default, standard output is the terminal
- Standard error
is where the program normally sends error messages
- By default, standard error is the same as standard output: the terminal
The Keyboard and Screen as Standard Input and Standard Output
- When a program does not specify the source of input ...
- it comes from standard input
- By default, standard input is keyboard
- When a program does not specify the destination of output ...
- it goes to standard output
- By default, standard output is the screen
- When a program does not specify where error messages should go ...
- they goes to standard error
- By default, standard error also is the screen
Redirection
- Redirection
is telling Unix to take data from, or send it to, a different place
- Redirection is one of the features that makes Unix so flexible
- You can take input from something other than the keyboard
- Like a file
- You can send output to something other than the screen
- Like another file
- Redirection is what makes pipes possible
- A pipe sends the output from one program into the input of another
- The output of the first command is redirected from the screen ...
- to the input of the second command
Redirecting Standard Output
Redirecting Standard Input
Redirecting Standard Output Can Destroy a File
- Redirecting standard output to an existing file overwrites its contents
- You lose the original contents of that file
Adding Output to an Existing File
/dev/null
- Sometimes a program will do something useful but produce output you don't want
- For situations like this, Unix provides /dev/null
- Any output you send to /dev/null will disappear
- It will never appear on the screen
- Redirecting input to come from /dev/null ...
- gives a program the empty strin
- /dev/null is most useful when dealing with error messages
- Since error message normally go to the terminal ...
- they will be mixed up with the regular output
- Sending error messages to /dev/null prevents this from happening