cd - Change Directorypwd - Show Your Current 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 OrderThe reading assignment for this week is chapter 3 of Sobell, The Utilities.
You will find a list of all reading assignments here.
There is a link to this page on the class web page.
I have posted Homework 3 here.
As usual, it will be due next Sunday at 11:59 PM.
The is the first homework assignment that will require you to write a script.
Your script must meet certain requirements that I mentioned in the last class.
Let's review the requirements for homework scripts.
cd cd tmp ls
bash tmp.sh
man or infomore or lessnano, vi, vim, emacs or any text editors
& : | * ? ' " [ ] ( ) $ < > { } # / \ ! ~cd - Change Directorycdcd without an argument ...cd ..
pwd - Show Your Current Directorypwd will print you your current location in the filesystempwd takes no argumentsls - List the Contents of a Directoryls lists the contents of a directoryls work
ls without an argument ...ls with the -l (long) optionls -a shows the "invisible" files ...cat - Print the Contents of a Filecatcat -n to print a number for each line of the filerm - Delete a Filermrm *rm will not remove a directory unless you use special optionsmkdir - Create a Directorymkdirrmdir - Delete a Directoryrmdir is used to remove a directoryrmdir will not work unless the directory is emptycp - Copy Filescp copies files or directoriescp with the -r optionmv - Move a File or Directorymv command to move a file or directory ...mvecho - 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:::
echo with a variable ...-n option ...echo with no option
$ cat echo_no_option.sh #! /bin/bash # demonstrates the use of echo without the -n option echo Hello World! echo About to leave
$ ./echo_no_option.sh Hello World! About to leave
echo use the -n option
$ cat echo_with_n_option.sh #! /bin/bash # demonstrates the use of echo with the -n option echo -n Hello World! echo About to leave
$ ./echo_with_n_option.sh Hello World!About to leave
hostname - Print the Name of Your Host Machinehostname command prints the network name ...$ hostname itserver6
-i option ...hostname will print the IP address of the host machine
$ hostname -i 192.168.107.136
more and lessless is an improved version of moremore ...lessless has more featuresless you can use the arrow keys to move up and down one linemore gets to the end of a file ...less ...grep - Finding Strings inside Filesgrep (get regular expression) is a utility which searches a text file ...grep uses the following format
grep [-OPTIONS] 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 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 ,,,-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 directory ...grep -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-3grep ...man pagegrep on an almost daily basisgrep to find every file that references that 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 ...
$ 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-4tail - 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 ...
$ 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 the contents of a file ...$ cat fruit.txt grapes pears oranges cranberries apples melons blueberries $ sort fruit.txt apples blueberries cranberries grapes melons oranges pears
sort looks at the beginning of each line of a file ...sort does not change the file itself ...sort many useful options ...man pagessort -r (reverse) will sort the file in reverse alphabetical order
$ sort -r fruit.txt
pears
oranges
melons
grapes
cranberries
blueberries
applessort -n (number) will sort a file by number ...
$ cat numbers.txt
11
1
17
2
3
15
4
5
14
6
13
7
8
9
10
12
16
18
19
20
$ sort numbers.txt
1
10
11
12
13
14
15
16
17
18
19
2
20
3
4
5
6
7
8
9
$ sort -n numbers.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20sort does not change the file on which it is run ...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.
When your script works, come see me so I can test it with my test script.