pwd
- Show Your Current Directorycd
- Change Directoryls
- List the Contents of a Directorycat
- Print the Contents of a Filerm
- Delete a Filemkdir
- Create a Directoryrmdir
- Delete a Directorycp
- Copy Filesmv
- Move a File or Directoryecho
- Print Text to the Terminalhostname
- Print the Name of Your Host Machinegrep
- Finding Strings inside Fileshead
- View the Top of a Filetail
- View the Bottom of a Filesort
- Print a File in Sorted OrderYou can connect to Gradescope to take weekly graded quiz today during the last 15 minutes of the class.
Once you start the quiz you have 15 minutes to finish it.
You can only take this quiz today.
There is not makeup for the weekly quiz because Gradescope does not permit it.
If you have the textbook you should read chapter 3, The Utilities.
I have posted homework 3.
As usual, it will be due this coming Sunday at 11:59 PM.
You will lose 2 points for every day that your assignment is late.
Every Unix file has a time stamp that changes every time the file is modified
I look at this time stamp to determine whether a submission is late.
If your assignment is not working by the due date, you can continue to work on it but you will lose a few points.
If your assignment is working, do not go back and change it, because that will change the time stamp and result in a late penalty.
If you get an email from me saying an assignment is missing do not email me about this.
I get far too many emails.
Instead of sending me an email, fix the problem.
If you do not know how to fix it, post a question on Piazza or contact the Class Assistant.
I collect homework assignments and and check Class Exercises several times during the week.
I will check or collect your assignment later in the week.
Are there any questions before I begin?
man
or info
more
or less
nano
, vi
, vim
, emacs
or any text editorscd cd tmp ls
bash tmp.sh
& : | * ? ' " [ ] ( ) $ < > { } # / \ ! ~
pwd
- Show Your Current Directorypwd
(print working directory) prints your current directory
pwd
cd
- Change Directorycd
(change directory) changes your current directorycd
to move from one directory to another
cd DIRECTORY_NAME
cd
with an argument it moves you to that directorycd
without an argument it takes you to your
home directory
cd ..
cd ../..
ls
- List the Contents of a Directoryls
shows you the files and directories inside a directoryls
with or without an argumentls
a directory name, it lists what is inside that directoryls
an argument it lists the current directoryls
has many options-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
-a
(for all) option also lists files whose names begin with . (dot)cat
- Print the Contents of a Filecat
(concatenate) prints a file
$ cat foo.txt foo bar blecth
-n
option cat
displays line numbers
$ cat -n numbered_lines.txt 1 Line 1 2 Line 2 3 Line 3 4 Line 4 5 Line 5
rm
- Delete a Filerm
(remove) deletes a file
rm FILENAME
rm *
mkdir
- Create a Directorymkdir
(make directory) creates a directory
mkdir DIRECTORY_NAME
rmdir
- Delete a Directoryrmdir
(remove directory) deletes a directory
rmdir DIRECTORY_NAME
rmdir
will not work unless the directory is emptycp
- Copy Filescp
(copy) makes a copy of a file or a directory
cp FILENAME DIRECTORY_OR_NEW_FILENAME
cp
takes two arguments-r
optionmv
- Move a File or Directorymv
(move) is a command that does two different things
mv FILENAME_OR_DIRECTORY_NAME NEW_DIRECTORY
mv FILENAME_OR_DIRECTORY_NAME NEW_FILENAME_OR_NEW_DIRECTORY_NAME
mv
takes two argumentsecho
- Print Text to the Terminalecho
simply prints whatever comes after it to the screen
$ echo Hello world! Hello world!
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:...
-n
option it does not advance to the next linehostname
- Print the Name of Your Host Machinehostname
command prints the network name of the machine you are using
$ hostname pe15
-i
option hostname
will print the IP address of the host machine
$ hostname -i 158.121.104.215
-d
option hostname
will show the domain name
$ hostname -d cs.umb.edu
more
and less
less
is an improved version of more
more
, it's less
less
has more featuresless
you can use the arrow keys to move up and down one lineless
also has a search featuremore
gets to the end of a file ...less
...grep
- Finding Strings inside Filesgrep
(get regular expression)
searches a file
grep
uses the following format
grep [-OPTIONS] SEARCH_STRING FILE ...
$ cat red_sox.txt 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 2011-07-05 Red Sox vs Blue Jays Win 3-2 2011-07-06 Red Sox vs Blue Jays Win 6-4 2011-07-07 Red Sox vs Orioles Win 10-4 2011-07-08 Red Sox vs Orioles Win 10-3 2011-07-09 Red Sox vs Orioles Win 4-0 2011-07-10 Red Sox vs Orioles Win 8-6 2011-07-15 Red Sox @ Rays Loss 6-9 2011-07-16 Red Sox @ Rays Win 9-5 2011-07-17 Red Sox @ Rays Win 1-0 2011-07-18 Red Sox @ Orioles Win 15-10 2011-07-19 Red Sox @ Orioles Loss 2-6 2011-07-20 Red Sox @ Orioles Win 4-0 2011-07-22 Red Sox vs Mariners Win 7-4 2011-07-23 Red Sox vs Mariners Win 3-1 2011-07-24 Red Sox vs Mariners Win 12-8 2011-07-25 Red Sox vs Royals Loss 1-3 2011-07-26 Red Sox vs Royals Win 13-9 2011-07-27 Red Sox vs Royals Win 12-5 2011-07-28 Red Sox vs Royals Loss 3-4 2011-07-29 Red Sox @ White Sox Loss 1-3 2011-07-30 Red Sox @ White Sox Win 10-2 2011-07-31 Red Sox @ White Sox Win 5-3
grep
like this
$ grep Win red_sox.txt
2011-07-02 Red Sox @ Astros Win 7-5
2011-07-03 Red Sox @ Astros Win 2-1
2011-07-05 Red Sox vs Blue Jays Win 3-2
2011-07-06 Red Sox vs Blue Jays Win 6-4
2011-07-07 Red Sox vs Orioles Win 10-4
2011-07-08 Red Sox vs Orioles Win 10-3
2011-07-09 Red Sox vs Orioles Win 4-0
2011-07-10 Red Sox vs Orioles Win 8-6
2011-07-16 Red Sox @ Rays Win 9-5
2011-07-17 Red Sox @ Rays Win 1-0
2011-07-18 Red Sox @ Orioles Win 15-10
2011-07-20 Red Sox @ Orioles Win 4-0
2011-07-22 Red Sox vs Mariners Win 7-4
2011-07-23 Red Sox vs Mariners Win 3-1
2011-07-24 Red Sox vs Mariners Win 12-8
2011-07-26 Red Sox vs Royals Win 13-9
2011-07-27 Red Sox vs Royals Win 12-5
2011-07-30 Red Sox @ White Sox Win 10-2
2011-07-31 Red Sox @ White Sox Win 5-3
grep
is case sensitive
$ grep win red_sox.txt
grep
to ignore case you must run it with the -i
option
$ grep -i win red_sox.txt
2011-07-02 Red Sox @ Astros Win 7-5
2011-07-03 Red Sox @ Astros Win 2-1
2011-07-05 Red Sox vs Blue Jays Win 3-2
2011-07-06 Red Sox vs Blue Jays Win 6-4
2011-07-07 Red Sox vs Orioles Win 10-4
2011-07-08 Red Sox vs Orioles Win 10-3
2011-07-09 Red Sox vs Orioles Win 4-0
2011-07-10 Red Sox vs Orioles Win 8-6
2011-07-16 Red Sox @ Rays Win 9-5
2011-07-17 Red Sox @ Rays Win 1-0
2011-07-18 Red Sox @ Orioles Win 15-10
2011-07-20 Red Sox @ Orioles Win 4-0
2011-07-22 Red Sox vs Mariners Win 7-4
2011-07-23 Red Sox vs Mariners Win 3-1
2011-07-24 Red Sox vs Mariners Win 12-8
2011-07-26 Red Sox vs Royals Win 13-9
2011-07-27 Red Sox vs Royals Win 12-5
2011-07-30 Red Sox @ White Sox Win 10-2
2011-07-31 Red Sox @ White Sox Win 5-3
grep -r
will search recursively through a directorygrep -v
returns all lines that do not match the search string
$ grep -v Win red_sox.txt
2011-07-04 Red Sox vs Blue Jays Loss 7-9
2011-07-15 Red Sox @ Rays Loss 6-9
2011-07-19 Red Sox @ Orioles Loss 2-6
2011-07-25 Red Sox vs Royals Loss 1-3
2011-07-28 Red Sox vs Royals Loss 3-4
2011-07-29 Red Sox @ White Sox Loss 1-3
grep
man
pagegrep
on an almost daily basisgrep
to find every file that uses that particular tablehead
- View the Top of a Filehead
displays the first 10 lines of a file
$ head red_sox.txt 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 2011-07-05 Red Sox vs Blue Jays Win 3-2 2011-07-06 Red Sox vs Blue Jays Win 6-4 2011-07-07 Red Sox vs Orioles Win 10-4 2011-07-08 Red Sox vs Orioles Win 10-3 2011-07-09 Red Sox vs Orioles Win 4-0 2011-07-10 Red Sox vs Orioles Win 8-6 2011-07-15 Red Sox @ Rays Loss 6-9
head
a number as an option it will display that number of lines
$ head -5 red_sox.txt
2011-07-02 Red Sox @ Astros Win 7-5
2011-07-03 Red Sox @ Astros Win 2-1
2011-07-04 Red Sox vs Blue Jays Loss 7-9
2011-07-05 Red Sox vs Blue Jays Win 3-2
2011-07-06 Red Sox vs Blue Jays Win 6-4
tail
- View the Bottom of a Filetail
is like head
except it prints the last 10 lines of a file
$ tail red_sox.txt 2011-07-22 Red Sox vs Mariners Win 7-4 2011-07-23 Red Sox vs Mariners Win 3-1 2011-07-24 Red Sox vs Mariners Win 12-8 2011-07-25 Red Sox vs Royals Loss 1-3 2011-07-26 Red Sox vs Royals Win 13-9 2011-07-27 Red Sox vs Royals Win 12-5 2011-07-28 Red Sox vs Royals Loss 3-4 2011-07-29 Red Sox @ White Sox Loss 1-3 2011-07-30 Red Sox @ White Sox Win 10-2 2011-07-31 Red Sox @ White Sox Win 5-3
tail
a number as an option to specify the number of lines printed
$ tail -4 red_sox.txt
2011-07-28 Red Sox vs Royals Loss 3-4
2011-07-29 Red Sox @ White Sox Loss 1-3
2011-07-30 Red Sox @ White Sox Win 10-2
2011-07-31 Red Sox @ White Sox Win 5-3
tail
is especially useful when looking at log filessort
- Print a File in Sorted Ordersort
prints a file with the lines sorted
$ cat fruit.txt grapes pears oranges cranberries apples melons blueberries $ sort fruit.txt apples blueberries cranberries grapes melons oranges pears
sort
does not change a filesort
prints the lines of a file in alphabetical order ...sort -r
(reverse) will sort the file in reverse alphabetical order
$ sort -r fruit.txt
pears
oranges
melons
grapes
cranberries
blueberries
apples
sort -n
(number) will sort a file by number
$ cat numbers_jumbled.txt
11
1
17
2
3
15
4
5
14
6
13
7
8
9
10
12
16
18
19
20
$ sort numbers_jumbled.txt
1
10
11
12
13
14
15
16
17
18
19
2
20
3
4
5
6
7
8
9
$ sort -n numbers_jumbled.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-nr
$ sort -nr numbers_jumbled.txt 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Class Exercise 5 is the first exercise where you will have to write a shell script.
You will create a text file using nano
, and type the Linux
commands into it.
You must create this file in your ex5 directory.
Please note that the first few sections of Class Exercise 5 should not be included in your ex5.sh script.
Test your script to see that it works.