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 Machinemorelessgrep - Finding Strings inside Fileshead - View the Top of a Filetail - View the Bottom of a Filesort - Print a File in Sorted OrderAre there any questions before I begin?
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.
In today's class I will continue talking about utilities.
And discuss command completion which can save you time and effort.
man or infomore or lessnano, vi, vim, emacs or any text editorscd cd tmp ls
bash tmp.sh
& : | * ? ' " [ ] ( ) $ < > { } # / \ ! ~
pwd - Show Your Current Directorypwd (print working directory) shows
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 ...
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 ...ls an argument ...ls has multiple 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 ...$ hostname pe15
-i option hostname will print the IP address of the host machine
$ hostname -i 158.121.104.215
catmorelessmoremore has been around for a long timelessless is an improved version of moremore ...lessless is loaded with featuresless$ ls 01_class_notes_it116.html 22_class_notes_it116.html 02_class_notes_it116.html 23_class_notes_it116.html 03_class_notes_it116.html 24_class_notes_it116.html 04_class_notes_it116.html 25_class_notes_it116.html 05_class_notes_it116.html 26_class_notes_it116.html ...
ls -l on the file for today's Class Notes ...$ ls -l 05_class_notes_it116.html -rw-r--r-- 1 ghoffman faculty 35194 Apr 22 10:20 05_class_notes_it116.html
$ ls -l 0
01_class_notes_it116.html 04_class_notes_it116.html 07_class_notes_it116.html
02_class_notes_it116.html 05_class_notes_it116.html 08_class_notes_it116.html
03_class_notes_it116.html 06_class_notes_it116.html 09_class_notes_it116.html
[516] (pe15) ghoffman - ~/public_html/it116_html/class_notes_it116
$ ls -l 0
grep - Finding Strings inside Filesgrepgrep to find every line in 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 -n lists line numbers
$ grep -n White red_sox.txt 23:2011-07-29 Red Sox @ White Sox Loss 1-3 24:2011-07-30 Red Sox @ White Sox Win 10-2 25:2011-07-31 Red Sox @ White Sox Win 5-3
grepman pagegrep on an almost daily basisgrep to find every file that uses that particular tablehead - View the Top of a Filehead to see the first few lines of the filehead without an argument $ 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-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 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 Ordersortsort 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
applessort -n (number) will sort a file by numbers
$ 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.