Search for question
Question

Ch 8-Searching, Extracting, and Archiving Data Lab Exercises This lab must be performed on a Fedora virtual machine (or physical machine). Using grep, find, and regular expressions in Fedora (Objective 3.2) 1. Boot up your Fedora VM and log on to a regular user account. 2. Launch a virtual terminal and log in as a regular (non-root) user. 3. Type grep hostname /etc/profile get and print the regular expression hostname from /etc/profile 4. Take a screen shot of the results and paste them here: 5. Type man grep. Press space bar once. What will the -i option do? 6. Quit man. Press up arrow to recall the grep command from history. Insert -i right after grep like this: grep -i hostname /etc/profile 7. How many additional instances of hostname were found by ignoring case? 8. Type grep -d skip hostname /etc/* The -d skip option tells grep to skip directories. The * is a wildcard which means to search everything in the /etc directory. Just ignore any "permission denied" messages, since you are a regular (non-root) user. 9. Type cat /etc/passwd to display the /etc/passwd file. 10. Since there was too much to display at one time, up arrow & pipe the output to less like this: cat /etc/passwd | less 11. Use arrows to scroll up and down through the passwd file. Screen shot your regular user account: 12. Type q to terminate the cat display. Type clear to clear the screen. 13. Type grep open /etc/passwd Open is salmon colored. How many instances of open were found? 14. Type grep ^open /etc/passwd to display only those files with "open" at the beginning. Screen shot: 15. Type grep bash$ /etc/passwd. What do think the $ character does in this application? 16. To find your username or root, type grep -E "(Username[root)" /etc/passwd Enter your username instead of Username. The -E option indicates that you are using an extended regular expression. How many entries were found? 17. Now, you will search for information about files. wc shows line count, word count, and byte count. Type wc /etc/hosts. Now cat /etc/hosts. Were the results of the wc command accurate? 18. How many lines are in the /etc/passwd file? 19. While grep searches for info within a file, and wc provides info about a file, find locates files Type find /usr/share/doc/ -name COPYING 20. Another similar command is locate. Find files as follows: locate COPYING 21. Use the up arrow and append: find /usr/share/doc/ -name COPYING | wc. Note: the | pipe symbol is located on the \ key. In the same manner, pipe the output of the other command to wc Which command found more files named COPYING? 22. Logical operators: Type echo "Hello". Now type echo "Hello" && echo "2nd Hello"J. The && symbols are a logical and operator which tells the OS to also execute the next command. 23. Up arrow, change && to ||, execute. || is a logical or operator, so only the first command is executed. Redirection and Pipes (Objective 3.2) 1. Boot up your Fedora VM and log on to a regular user account. 2. Launch a virtual terminal and log in as a regular (non-root) user. 3. Type echo "Hello" and press Enter. By default, the output of echo was directed to your display. The display is known as Standard Output or STDOUT. 4 Redirect STDOUT to a file by typing echo "Hello" > new_file. What was displayed? 5. Type cat new_file to see where the echo output was redirected. > is the redirection operator. 6. Redirect output again by typing cat new_file > second_file…. Display the contents of second_file. 7. Type echo "I am redirected"> new_file. Now type cat new_file. Was the new data appended to the old data, or did it overwrite it? 8. Append to the output of the file: echo "I appended this" >> new_file. 9. Type cat new_file. > redirect operator for STDOUT >>append operator for STDOUT 10. So far you have just been redirecting STDOUT. Now you will redirect Standard Error (STDERR). 11. Type cat /etc/PASSWORDJ. Since there is no such file, your will get an error message. 12. By default, STDERR messages are directed to the display. 13. Now, redirect and append the error message: cat /etc/PASSWORD 2>> new_file. Note: The number 2 was used to distinguish the STDERR error message (2>>) from STDOUT (>>). 2> redirect operator STDERR 2>>append operator for STDERR 14. Take a look at how the file new_file looks now by typing cat new_file. The error message should now be appended at the end of new_file's contents. 15. Replace new_file's contents with the error message: Type cat /etc/PASSWORD 2> new_file. 16. Use the cat command to verify that the contents were replaced. 17. Remove the files used previously by typing rm -i *file. Type y when prompted. 18. Create 4 new files in your home dir by typing touch fileA.dat fileB.dat fileC.dat fileD.dat…. 19. Verify the presence of all four files by typing Is file?.dat.. 20. Pipe (shift \) the output to the sort command by typing Is file?.dat | sort.. 21. Now recall the command and reverse the sort order by appending it to Is file?.dat | sort -r 22. Display and save output with the tee command: Is file?.dat | sort -r | tee fileE.dat. 23. Verify by typing cat fileE.dat.. 24. To remove files using the xargs command, type Is file?.dat | xargs rm.. 25. Type Is to verify that all of the file?.dat files were deleted. Compression and Archiving (Objective 3.1) 1. Boot up your Fedora VM and log on to a regular user account. 2. Launch virtual terminal 3 and log in as a regular (non-root) user. 3. Type touch file_a.log file_b.log file_c.log file_d.log and verify their presence. 4. Compress these files by typing gzip file*.log. 5. Is the files to verify that they were all compressed using gzip and renamed. 6. Use gzip to uncompress the files by typing gunzip file*.gz and is file* to verify. 7. Using history, recall the command and compress the files using by typing bzip2 file*.log. 8. Verify their presence, then uncompress them by typing bunzip2 file*.bz2J. 9. To create a single file from a group of files, type tar cvf file.tar file*.log. 10. What is the name of the new file? What color is it? This is the color of archived files 11. "c" creates the tar file; "v" = verbose (tell you what is going on); "f" comes before the filename. 12. Now create and compress an archive file using gzip by typing tar zcvf file.tar.gz file*.log. 13. A compressed tar file is called a tarball. What is the name of your tarball? 14. Create a new directory by typing mkdir unpack_tarë. This is where you will unpack the tarball. 15. Move the gzip tarball to the new directory by typing mv file.tar.gz unpack_tar/.. 16. What color is the new directory? 17. Notice that you used a relative directory reference when you moved the tarball. 18. Change directory into the new directory. What command did you use? 19. List the contents of the unpack_tar directory to verify that the tarball is present. 20. Uncompress and unpack the tarball by typing tar zxvf file.tar.gz. 21. Refer to table 8.7. What does the x (instead of c) do? 22. List the contents of the directory. Do you see the 4 .log files, the tarball file, or all of the above? 23. Return to your home directory by typing cd. 24. Remove the directory and its contents by typing rm -ir unpack_tar. Type y to each prompt. 25. List your home directory to verify that it was successful. 26. Remove the files created for compression and archiving by typing rm -i file*.*, y for each.