Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.
-
Upload
beatrice-haynes -
Category
Documents
-
view
222 -
download
1
Transcript of Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.
![Page 1: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/1.jpg)
Introduction to Linux
Instructor: Bennett M. TanyagPART – 3Unit 1
![Page 2: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/2.jpg)
THE grep COMMAND
The grep command is primarily for pattern searching. Users can use this command to search a set of files for one or more phrases or patterns. If the pattern exists, then grep will print all the lines that contain the said pattern.
![Page 3: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/3.jpg)
The general format of the grep command is:grep pattern filenamewhere:pattern is the phrase or pattern the
user wants to find.filename is the name of the target file.
![Page 4: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/4.jpg)
Example:Assume that the file horror.story contains:
And he slowly entered the cell and went to thesink. He reached out trembling and touched thelather on a brush. It was real. It felt warm.It smelled of soap. The water dripped into
![Page 5: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/5.jpg)
$ grep the horror.story
And he slowly entered the cell and went to the
sink. He reached out trembling and touched the
lather on a brush. It was real. It felt warm.
$ _
![Page 6: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/6.jpg)
If the pattern does not exist, then UNIX will simply display the $ prompt again.
Notice that the third line matched since the is part of lather.
![Page 7: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/7.jpg)
If the pattern consists of more than one word, then the user must enclose the pattern in double quotes.
Example:
$ grep “of soap” horror.storyIt smelled of soap. The water dripped into
![Page 8: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/8.jpg)
Some of the options available for the grep command are:1. The -v OptionThe -v option will display all lines except those containing the pattern.Example:
$ grep -v “of soap” horror.storyAnd he slowly entered the cell and went to thesink. He reached out trembling and touched thelather on a brush. It was real. It felt warm.
![Page 9: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/9.jpg)
2. The -n Option
The -n option will display the lines together with their line number.
Example:
$ grep -n “of soap” horror.story4: It smelled of soap. The water dripped
into
![Page 10: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/10.jpg)
3.The -c OptionThe -c option will simply give the number of lines that contain the pattern.Example:
$ grep -c the horror.story3$ _
![Page 11: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/11.jpg)
4. The -y OptionThe -y option causes UNIX to ignore the difference between uppercase and lowercase letters.Example:
$ grep -y the horror.storyAnd he slowly entered the cell and went to thesink. He reached out trembling and touched thelather on a brush. It was real. It felt warm.It smelled of soap. The water dripped into
![Page 12: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/12.jpg)
5. The -l OptionThe -l option is a special option of grep that causes UNIX to print or display the filenames of all files the contain the pattern of interest.
The command: grep -l pattern *
searches all files in the current working directory that contain pattern and prints the filenames of these files.
![Page 13: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/13.jpg)
Example:
$ grep -l INLINUX *grades.this.term
subjects.last.term
$ _
![Page 14: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/14.jpg)
THE sort COMMAND
The sort command prints the contents of a file in alphabetical or numerical order.
The general format of the sort command is:
sort filename
![Page 15: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/15.jpg)
Example:If the file fruits.doc originally contains:MangoGrapesAppleBananaThen executing the command:$ sort fruits.docAppleBananaGrapesMango$ _
Take note that the sort command does not change the contents of the file.
![Page 16: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/16.jpg)
THE head AND tail COMMANDS The head and tail commands are similar to
the cat command except that a user can specify the number of lines to be displayed.
The head command can display the first 10 lines of a file while the tail command can display the last 10 lines of a file.
![Page 17: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/17.jpg)
The user can also specify the number of lines he or she wants to view.
Example:1.head -5 filename
This will display the first 5 lines of the file.
2. tail -5 filenameThis will display the last 5 lines
of the file.
![Page 18: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/18.jpg)
THE find COMMAND
The find command is a powerful file searching tool that enables you to locate all files that meet a certain set of criteria.
![Page 19: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/19.jpg)
The find command is useful in:
1.Locating lost files2.Locating related files3.Locating recently modified files4.Locating files not recently modified5.Checking file organization and security
![Page 20: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/20.jpg)
The general format of the find command is:find directories criteria action
where:directories specify a list one or more directories
the user wants the find command to search. find will search these directories and all of its subdirectories.
criteria specify the characteristics of the files that are of interest.
action specifies what the user wants the file command to do with the files it finds.
![Page 21: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/21.jpg)
Examples:
1. $ find /home/benz -name benz.doc -print This command will search for all files with
filename benz.doc starting at directory /usr/benz (and its subdirectories). When the find command locates the files of interest, it will print the pathnames of the files on the screen.
![Page 22: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/22.jpg)
Sample Output:
$ find /usr/mitch -name mitch.doc -print
/usr/mitch/mitch.doc
/usr/mitch/document/mitch.doc
$
![Page 23: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/23.jpg)
The find command can also accept wildcards in a filename provided that the user encloses it in double quotes:
Example:$ find . -name “andaya*” -print
This command will search for all files with filenames beginning with andaya starting at the current directory (and its subdirectories). When the find command locates the files of interest, it will print them on the screen.
![Page 24: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/24.jpg)
Other uses of the file command are:
1. $ find / -user mitch -printThis command will search for all files owned by mitch starting at the root directory.
2. $ find /tmp -group account -printThis command will search for all files owned by any member of the group account at the /tmp directory.
![Page 25: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/25.jpg)
3.$ find / -mtime -7 -print
This command will search for all files that have been modified within the past seven days starting at the root directory.
4.$ find /usr -mtime +30 -print
This command will search for all files that have not been modified within the past thirty days starting at /usr directory.
![Page 26: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/26.jpg)
5. $ find / -atime -7 -print
This command will search for all files that have been accessed within the past seven days starting at the root directory.
![Page 27: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/27.jpg)
THE alias COMMAND
An alias allows a user to introduce abbreviations for commands that he or she uses often.
The general format of the alias command:
alias alias_name command
![Page 28: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/28.jpg)
Example:$ alias find_mitch find / -name mitch.doc -print
After executing this command, the user can type find_mitch at the prompt instead of the command find / -name mitch.doc -print to search for all files with the filename mitch.doc starting at the root directory.
Some UNIX users use the alias command to “rename” some of the commands so that they resemble DOS commands.
![Page 29: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/29.jpg)
Example:
$ alias dir =‘ls –l’
After executing this command, the user may use dir instead of ls -l.
![Page 30: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/30.jpg)
THE wc COMMAND
The wc (word count) command counts the number of characters, words, and lines in one or more files. If the user specifies multiple files, wc provides an individual count for each file and totals for the group.
![Page 31: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/31.jpg)
The general format of the wc command is:wc filename(s)
Examples:$ wc letter.doc4 13 62 letter.doc
This means that the file letter.doc has 4 lines, 13 words, and 62 characters.
![Page 32: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/32.jpg)
$ wc letter.doc mitch.doc4 13 62 letter.doc
3 10 51 mitch.doc
7 23 113 total
This means that the file letter.doc has 4 lines, 13 words, and 62 characters while the file mitch.doc has 3 lines, 10 words, and 51 characters.
![Page 33: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/33.jpg)
The wc command has several options:1. The -l Option
The -l option causes the wc command to display only the number of lines in the file.Example:$ wc -l letter.doc
4 letter.doc$ _
![Page 34: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/34.jpg)
2. The -w Option
The -w option causes the wc command to display only the number of words in the file.
Example:
$ wc -w letter.doc13 letter.doc
$ _
![Page 35: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/35.jpg)
3. The -c Option
The -c option causes the wc command to display only the number of characters in the file.
Example:
$ wc -c letter.doc62 letter.doc
$ _
![Page 36: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/36.jpg)
As in other commands, a user may combine several options in one command line.Example:$ wc -lc letter.doc
4 62 letter.doc$ _
![Page 37: Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.](https://reader035.fdocuments.us/reader035/viewer/2022062304/56649f425503460f94c622a7/html5/thumbnails/37.jpg)
End of Part 3 Unit 1