Question

Chapter 8: Searching, Extracting, and Archiving Data Exercise 8.a: Using grep, find, and regular expressions (Objective...

Chapter 8: Searching, Extracting, and Archiving Data

Exercise 8.a: Using grep, find, and regular expressions (Objective 3.2)

Linux Distribution: Fedora (non-root user & password needed)

Desktop Environment: GNOME 3

1.   If you have not already done so, boot up your computer (or virtual machine) to start the Fedora Linux distribution.

2.   When the machine has booted up, access the tty2 virtual terminal by pressing Ctrl+Alt+F2.

3.   Log on to a regular user’s account by typing in the username at the login prompt and entering the user account’s password at the Password prompt.

4.   Try out the grep command by typing: grep hostname /etc/profile and press Enter. You should see a single line from the /etc/profile file displayed, similar to:
HOSTNAME = `/bin/hostname 2>/dev/null`
The grep command allows you to search through files and/or directories using something called "regular expressions." In this case you are just using a character sequence made up of a series of characters, hostname, to search for the characters within the file, /etc/profile.

5.   In the preceding step, you searched for hostname and all the characters to be found had to be lowercase. Now have grep ignore the characters' case by typing: grep -i hostname /etc/profile and press Enter. You should now see two lines from the /etc/profile file displayed, similar to:
HOSTNAME = `/bin/hostname 2>/dev/null`
export PATH USER LOGNAME MAIL HOSTNAME ...
The -i option tells the grep command to ignore case. Therefore, it will find the characters you specify in both upper and lower cases.

6.   Now you will try out using grep to search for file names that contain the word hostname and are in the /etc directory.
Type grep -d skip hostname /etc/* and press Enter. Notice that you get both the filename and the line from the files that contain the word hostname. Don't worry if you get some "permission denied" messages. Remember, you are logged in as a regular user and will not have access to several files. This is a great way to search through configuration files for a setting, if you cannot remember the configuration file name. The -r option will let you search recursively through various directories.

Exercise 8.a: Using grep, find, and regular expressions (Objective 3.2) (continued)

7.   Try to locate just the root user's password file record by entering the command grep root /etc/passwd and press Enter. Notice you got two records instead of one, because the character sequence "root" is embedded within another /etc/passwd record.

8.   This time, you will be able to get grep to locate just the root user's password file record, by searching for only the record that starts with the character sequence "root." Type grep ^root /etc/passwd and press Enter. Now you should see only root's password file record. The ^ metacharacter in front of your character sequence will only return lines that have that character sequence at the line’s beginning.

9.   Try to locate every record in the hosts file that ends with the character sequence "local host" by typing grep localhost /etc/hosts and press Enter. Notice you got two records instead of one, and one of the records does not end with the character sequence "localhost."

10. This time, you will be able to get grep to locate only the records that end in the word "localhost" by typing grep localhost$ /etc/hosts and pressing Enter. Now you should see only one record, and that record actually ends with the word "localhost." The $ metacharacter at the back of your character sequence will only return lines that have the character sequence at the line’s end.

11. Using an extended regular expression, you will search for one of two possible character sequences, your username or root, in the /etc/passwd file. Using your username for the Username in the command, type grep -E “(Username|root)” /etc/passwd and press Enter. You should see at least two /etc/passwd file records containing either your username or root. The -E indicates that an extended regular expression will be used.

12. Instead of searching for information within a file, now you will be digging out information about a file. Type wc /etc/hosts and press Enter. You will get three numbers back; record the numbers you received from the command here:____________________. The wc command produces output that shows three statistics for the file: line count, word count, and byte count.

13. Try another file with wc: Type wc /etc/passwd and press Enter. How many lines are in the /etc/passwd file? Record your answer here:__________. Remember, the first number returned is the line count.

14. Instead of searching for information about files, you will be searching for files themselves. Type find /usr/share/doc/ -name COPYING and press Enter. You should “find” lots of files! Remember that the COPYING files are typically where software licenses are kept for various Linux software applications. The -name option of the find command allows you to search for a file by name.

15. Compare the locate command with the find command by typing locate COPYING and press Enter. Did locate find more files than the find command? It’s hard to tell, isn’t it! Remember, the locate command searches though a database of file names and their locations, so it is typically a little quicker than the find command.

Homework Answers

Answer #1

//I have ran the above commands in my local system

Answer:

For the 1st blank which is in #12

$ wc /etc/hosts

  9 32 213 /etc/hosts

The wc command produces output that shows three statistics for the file: line count =9 , word count = 32, and byte count = 213.

For the 2nd blank which is in #13

$ wc /etc/passwd

103 279 6393   /etc/passwd

The wc command produces output that shows three statistics for the file: line count =103 , word count = 279, and byte count = 6393.

I have ran the commands in my local system. If you want you can also run the commands(2) below in your local system.

wc /etc/hosts and wc /etc/passwd

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT