Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.

Similar presentations


Presentation on theme: "Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1."— Presentation transcript:

1 Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1

2 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.

3 The general format of the grep command is: grep pattern filename where: patternis the phrase or pattern the user wants to find. filename is the name of the target file.

4 Example: Assume that the file horror.story contains: 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. It smelled of soap. The water dripped into

5 $ 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. $ _

6 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.

7 If the pattern consists of more than one word, then the user must enclose the pattern in double quotes. Example: $ grep “of soap” horror.story  It smelled of soap. The water dripped into

8 Some of the options available for the grep command are: 1.The -v Option The -v option will display all lines except those containing the pattern. Example: $ grep -v “of soap” 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.

9 2.The -n Option The -n option will display the lines together with their line number. Example: $ grep -n “of soap” horror.story  4: It smelled of soap. The water dripped into

10 3.The -c Option The -c option will simply give the number of lines that contain the pattern. Example: $ grep -c the horror.story  3 $ _

11 4.The -y Option The -y option causes UNIX to ignore the difference between uppercase and lowercase letters. Example: $ grep -y 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. It smelled of soap. The water dripped into

12 5.The -l Option The -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.

13 Example: $ grep -l INLINUX *  grades.this.term subjects.last.term $ _

14 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

15 Example: If the file fruits.doc originally contains: Mango Grapes Apple Banana Then executing the command: $ sort fruits.doc  Apple Banana Grapes Mango $ _ Take note that the sort command does not change the contents of the file.

16 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.

17 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 filename This will display the last 5 lines of the file.

18 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.

19 The find command is useful in: 1.Locating lost files 2.Locating related files 3.Locating recently modified files 4.Locating files not recently modified 5.Checking file organization and security

20 The general format of the find command is: find directories criteria action where: directoriesspecify a list one or more directories the user wants the find command to search. find will search these directories and all of its subdirectories. criteriaspecify the characteristics of the files that are of interest. actionspecifies what the user wants the file command to do with the files it finds.

21 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.

22 Sample Output: $ find /usr/mitch -name mitch.doc -print /usr/mitch/mitch.doc /usr/mitch/document/mitch.doc $

23 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.

24 Other uses of the file command are: 1.$ find / -user mitch -print This command will search for all files owned by mitch starting at the root directory. 2.$ find /tmp -group account -print This command will search for all files owned by any member of the group account at the /tmp directory.

25 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.

26 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.

27 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

28 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.

29 Example: $ alias dir =‘ls –l’ After executing this command, the user may use dir instead of ls -l.

30 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.

31 The general format of the wc command is: wc filename(s) Examples: $ wc letter.doc  41362 letter.doc This means that the file letter.doc has 4 lines, 13 words, and 62 characters.

32 $ wc letter.doc mitch.doc  413 62 letter.doc 310 51 mitch.doc 723113 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.

33 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 $ _

34 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.doc  13 letter.doc $ _

35 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.doc  62 letter.doc $ _

36 As in other commands, a user may combine several options in one command line. Example: $ wc -lc letter.doc  462 letter.doc $ _

37 End of Part 3 Unit 1


Download ppt "Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1."

Similar presentations


Ads by Google