IT 244: Introduction to Linux/Unix
Class 4
General Advice
Course Work
Tips and Examples
Review
New Material
Microphone
Questions? Issues?
Does anybody have any questions before I begin?
Homework 2
I have posted homework 2.
As usual, it will be due this coming Sunday at 11:59 PM.
Today's Class
Today I will talk about certain characters that have special
meaning on the command line.
I will also discuss some of the most important Unix utilities.
General Advice
Make a Study Schedule
- One of the most important thing you can do to do better in class ...
- is to make a study schedule ...
- and keep to it
- A consistant study routine is the single most important thing
you can do ...
- to improve your grade
- You should set aside regular chunks of time for this course ...
- and stick to it as best you can
- The semester is long - 14 weeks
- Most of you have many responsibilites
- Conflicting demands on your time can cause you to lose focus
- Having a regular schedule can keep you on track ...
- and prevent panic attacks before exams
- Time is a finite resource
- Once you have wasted an hour ...
- you can never get it back
- If you find your schedule is not working ...
- make some changes
- But do not use that as an excuse to abandon a schedule
- There is an old saying
- If you fail to plan ...
- you are planning to fail
Course Work
First Graded Quiz
- The first graded quiz will be given on Tuesday next week
- The question on the quiz are taken from the ungraded quizzes ...
- of the previous week
- You should expect a Graded Quiz each week on Tuesday
- If you do not take the quiz when it is offered ...
- you cannot take it at another time
- If you miss a quiz but have an excuse, send me an email
Gradescope
- In order to receive your scores in Gradescope ...
- you need to sign in to my Gradescope section
- You do not have to register with Gradescope
- In the next few days I will enter all your UMB addresses
into Gradescope
- You will then receive an email from Gradescope
- What happens next depends on whether you are already
registered with Gradescope
- If you are new to Gradescope
- Click the "Set your password" link
- Confirm your password
- Click Save
- You will then be taken to the page for your course
- If you are already registered with Gradescope
- Click the link to the course
- At the login screen enter your UMB email
address and your Gradescope password
- If you have forgotten you password, click the "Forgot
your password"link
- You will find more information about Gradescope
here.
Tips and Examples
Use Flash Cards to Prepare for Graded Quizzes
- To prepare for the Graded Quizzes you need to study the Class Quiz answers for the previous week
- The best way to do this is to use flash cards
- You take an index card and write the question on one side and the answer on the other
- You should make a set of flash cards for each class
- Label them with the Class Number and put a rubber band around them
- Keep these cards after the graded quiz
- You will need them for studying for the Midterm and Final
- I made flash cards when taking courses for my Masters degree
- I went through them on my way to class on the T
- There are programs that you can use to create virtual flash cards
- But the simple act of writing each card helps you remember the material
Always Check Your Work
Recorded Class Meetings
- All lectures for this class are recorded
- Use this resource if you miss a class ...
- or don't quite understand something mentioned in class
- You will find links to the recordings of class meetings
here
Review
The Unix script Command
- The
script command creates a transcript of your Unix session
- It records everything you type on the command line ...
- and all the output the command creates
- By default,
script creates a text file named typescript
in your current directory
- Running
script a second time will overwrite the previous typescript file
- To prevent this from happening run
script -a
- With the
-a option, script will add to the previous
typescript file ...
- so you won't lose your work
- When you have finished with your
script session you must type "exit"
- Otherwise you will lose the entire session
Correcting Mistakes on the Command Line
- You can correct mistakes on the Unix command line
- This is called command line editing
- Use the following Control key combinations
- Control A - Move the text insertion point to the beginning of the command line
- Control E - Move the text insertion point to the end of the command line
- Control K - Remove everything from the text insertion point to the end of the line
- Control U - Remove everything from the text insertion point to the beginning of the line
- Control Y - Pastes back on the command line what
was removed with either Control K or Control U
- The Left and Right arrow keys, ← and → move the
text insertion point
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
Stopping 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
- Some commands use
-h as the help option
- But most use
--help
- For more information, use
man or info
- Follow
man or info with the name of the command
Attendance
New Material
Special Characters
Quoting and Escaping
pwd - Show Your Current Directory
- You are always somewhere in the filesystem
- If you are not where you need to be ...
- commands might not work as you expect
- You can always find your current location with
pwd ...
- which stands for print working directory
- The command does not take an argument
- So a call to
pwd looks like this
pwd
- Here is what I see when I run
pwd
after connecting to pe15
$ pwd
/home/ghoffman
cd - Change Directory
- When you first connect to pe15
you will be in your home directory
- Usually you will need to go to another directory to do your work
- To do this you will need to use the
cd command
cd stands for change directory
- Unix commands are usually very short ...
- to make them easier to type
cd changes your location in the filesystem
- A call to
cd looks like this
cd DIRECTORY_NAME
- If I were in my home directory and wanted to go to the
tmp directory inside my home directory ...
- I would type
$ cd tmp
- If you use
cd with an argument it moves you to that directory
- If you use
cd without an argument it takes you to your
home directory
- This can be useful
- As you begin to work with Unix you may get lost ...
- and not be in the directory you need to be
- If that happens simply type
cd without an argument
- That will take you back to your home directory ...
- from which you can start again
- Sometimes you need to go up the chain of directories
cd can do this if you give it the argument
..
cd ..
- To go up more than one directory use multiple instances of ..
- Each .. must be separated by a /
- To go up two directories enter
cd ../..
ls - List the Contents of a Directory
ls (list) is one of the most basic Unix commands
- It lists the files and directories inside a directory
- A call to
ls looks like this
ls DIRECTORY
- You can use
ls with or without an argument
- If you give
ls a directory as an argument ...
- it lists what is inside that directory
$ ls it244_test
1 ex06.sh ex12.sh ex20.sh ex24.sh five_lines.txt
1.sh ex07.sh ex13.sh ex21.sh ex25.sh script_improvements.txt
ex02.sh ex08.sh ex16.sh ex22.sh ex26.sh step_comment_check.sh
ex03.sh ex09.sh ex17.sh ex22_sum.sh ex27.sh
ex04.sh ex10.sh ex18.sh ex23.sh ex_list.sh
ex05.sh ex11.sh ex19.sh ex23_sum.sh exnn.sh
- When you don't give
ls an argument it lists the current directory
$ pwd
/home/ghoffman
$ ls
bin course_materials it116 it244 stdwrk tmp
bugs demos it116_test it244_test submitted wrk
code haoyu it117 mail testing xrchiv
course_files html it117_test public_html tests_taken
- The command line does not give you much information
- It it easy to lose track of where you are
- When this happens, certain things will not work as you would expect
- Running
ls can help you know you are in the right directory
- Until you get more comfortable with the command line ...
- it's a good idea to use
ls right after using cd
- If the listing looks wrong, you are in the wrong directory
- Like many Unix commands
ls has many options
- But two are used most often
- The
-l (el) option gives you a long listing
$ ls -l
total 20
-rw-rw-r-- 1 ghoffman 103 Sep 11 14:34 basic.css
-rw-r--r-- 1 ghoffman 3560 Aug 29 13:30 emacs_cheat_sheet.html
-rw-r--r-- 1 ghoffman 701 Aug 29 13:30 index.html
drwxr-xr-x 6 ghoffman 512 Sep 15 14:11 it_244
-rw-r--r-- 1 ghoffman 6831 Aug 29 13:30 tips.html
-rw-r--r-- 1 ghoffman 6052 Aug 29 13:30 unix_cheat_sheet.html
- We'll talk more about this when we discuss access permissions
- Directories can contain files that are used to configure Unix
- These files have names that beging with .
- Normally you don't see these "invisible" files
- If you want to see them, use the
-a (for all) option to ls
$ ls -a
. .bash_profile.bak .emacs.d it244 .nano .subversion
.. .bashrc .forward it244.d .pinerc test_setup.sh
.addressbook .bashrc~ it116 it480 .plan tmp
.bash_history .cache it116.d .local .plan2 .viminfo
.bash_profile .config it117 .login .profile.d
.bash_profile~ .emacs it117.d .msgsrc .ssh
- We'll discuss these files later in the course
cat - Print the Contents of a File
rm - Delete a File
mkdir - Create a Directory
- Directories allow you to organize your files
- You will have to create a new directory for every Class Exercise ...
- and homework assignment
- You create a directory using the
mkdir command
mkdir stands for make directory
- A call to
mkdir has the following format
mkdir DIRECTORY_NAME
- The new directory will be created inside the current directory
rmdir - Delete a Directory
- You cannot delete a directory using
rm
- Instead you need to use
rmdir
rmdir stands for remove directory
- A call to
rmdir has the following format
rmdir DIRECTORY_NAME
rmdir will not work unless the directory is empty
- Before you use
rmdir you must ...
cd into the directory to be removed ...
- remove all files with
rm
rm *
...
- and return to the above directory with
cd
cd ..
- Then use
rmdir to remove the directory
cp - Copy Files
- On the command line yoy can make copies of files and directories ...
- using the
cp command
cp stands for copy
cp takes two arguements
- The first is the name of the file or directory you want to copy
- The second is one of two things
- Either the directory where you want the copy to be made ...
- or the name of the new file or directory
- To copy a file use the format
cp FILENAME DIRECTORY_OR_NEW_FILENAME
- To copy a directory use the format
cp -r DIRECTORY TARGET_DIRECTORY
- Here is an example of copying a file to a directory
$ ls
dir foo.txt
$ ls dir # dir is empty. there is nothing in it
$ cp foo.txt dir
$ ls dir # now dir has a copy of the file
foo.txt
- Here is an example of making a copy of the file ...
- in the same directory ...
- with a new name
$ ls
dir foo.txt
$ cp foo.txt bar.txt
$ ls
bar.txt dir foo.txt
- To copy a directory you must use the
-r option
mv - Move a File or Directory
mv is a command that can do 2 things
- It can move a file or directory from one location in the filesystem ...
- to another
- Or it can rename a file or directory
mv stands for move
- To move a file or directory to a different place use the format
mv FILENAME_OR_DIRECTORY_NAME NEW_DIRECTORY
- To change the name of a file or directory use the formal
mv FILENAME_OR_DIRECTORY_NAME NEW_FILENAME_OR_NEW_DIRECTORY_NAME
- In either case,
mv takes two arguments
- The first argument is always the the name of the file or directory
- When used to move, the the second argument is the new location
- Here is an example using
mv to move a file to a new location
$ ls
foo.txt it244 work
$ ls work # the work directory is empty
$ mv foo.txt work
$ ls # foo.txt is no longer where it once was
it244 work
$ ls work # foo.txt has been moved to the work directory
foo.txt
- Here is an example using
mv to change the name of a file
$ ls
foo.txt it244 work
$ mv foo.txt bar.txt
$ ls
bar.txt hold it244 work
man Page Sections
- The files that are shown when we use the
man command
are called man pages
- They are divided into the following sections
- 1 User commands
- 2 Programming interfaces for kernel system calls
- 3 Programming interfaces to the C library
- 4 Special files such as device nodes and drivers
- 5 File formats
- 6 Games and amusements such as screen savers
- 7 Miscellaneous
- 8 System administration commands
- Section 1 is the most usefull to ordinary users
- But the others can help you learn about Linux
- For example,
apropos shows
more than 1 entry for the passwd command
apropos passwd
...
passwd (1)
passwd (5)
...
- Running
man passwd shows you the section 1 entry ...
- which is the command to change your password
- But runnin
man 5 passwd shows the format
of the file /etc/passwd ...
- which is one of the Linux files with password data
- Don't get too excited about the games section 6
- As far as I can tell it only lists two programs
- The first create a silly character graphic
$ cowsay Hi there
__________
< Hi there >
----------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
- The second prints something you might see in a fortune cookie
$ fortune
You'll feel much better once you've given up hope.
- The man pages are stored in /usr/share/man
Class Exercise
Class Quiz