Linux+ Lab Series (LX XXXXXXXXXXLab 7b: Using the BASH Shell Chapter 4 - Using the BASH Shell to Search Documents and Edit Document ContentDocument Version: XXXXXXXXXXObjective ...

1 answer below »



UNIX/Linux Class. Using Oracle VM VirtualBoxOracle VM VirtualBox and Red Hat Enterprise LinuxRed Hat Enterprise Linux program






Linux+ Lab Series (LX0-101) Lab 7b: Using the BASH Shell Chapter 4 - Using the BASH Shell to Search Documents and Edit Document Content Document Version: 2023-02-26 Objective In this lab, you will perform the following tasks: 1. Practice shell redirection operators (> and >>) using standard file descriptors. 2. Practice the find and grep commands for finding files on the filesystem. 3. Edit documents using the tr (translate) and touch commands. 4. Practice the use of pipes for executing multiple commands in series. 5. Sort documents and view the content of very large documents using head and tail commands. In this task, we will issue various commands and redirect the output from stdout (standard output) and stderr (standard error) to a file instead of the terminal screen. When a command is issued, the output is displayed in the terminal. This is considered stdout (standard output). Standard input (stdin) is typically the keyboard, but files can also be used as stdin. Standard error (stderr) has error messages sent to the terminal by default. Thus, stdout and stderr use the same output venue by default, the terminal. 1. Launch the Red Hat Linux virtual machine. The virtual machine will display a login screen. 2. Use the redirection symbol > to redirect the output from stdout (terminal) to a file. Type the following: echo “Hello World” echo “Hello World” > mymessage cat mymessage Your output should be similar to the following: The first command echoes the message to the terminal (stdout). The second command redirects the output. Instead of sending it to the terminal, the output is sent to a file. The last command displays the contents of the file mymessage. 2. Now, examine another approach. Type the following commands: cat mymessage echo Greetings > mymessage cat mymessage Your output should be like the following: Notice that using one redirection symbol overwrites an existing file. Using >>, you append to a file: 3. Append data to a file using >>. Cat mymessage echo “How are you” >> mymessage cat mymessage Your output should be like the following: Notice that by using two redirection symbols, all existing data is preserved and the new data is appended at the end of the file.. find is a powerful command-line utility that locates files based on some user-specified criteria and prints the pathname of each matched object. To look for a specific file, run the following command from the root (/). The command contains the exact name for the file you are searching for. Find . -name file22.txt Output: ./test/file22.txt ./sales/file22.txt Note that the results include the path name. This is important if you don’t know the directory where the file is located, or when it is in more than one place. 4. Run the following command and observe the output: find /etc -name hosts Your output should be like the following: Notice the error messages indicating you do not have permission to access certain files/directories. Also notice that these are error messages; if I want to capture them to a file, it needs extra steps: The error message.txt file above is empty because these messages were sent to the stderr stream In Bash and other Linux shells, when a program is executed, it uses three standard I/O streams. Each stream is represented by a numeric file descriptor: •0 - stdin, the standard input stream. •1 - stdout, the standard output stream. •2 - stderr, the standard error stream. A file descriptor is just a number representing an open file. The input stream provides information to the program, generally by typing in the keyboard. The program output goes to the standard input stream and the error messages goes to the standard error stream. By default, both input and error streams are printed on the screen. Redirecting Output Redirection will capture the output from a program and send it to a file. Streams can be redirected using the n> operator, where n is the file descriptor number. When n is omitted, it defaults to 1, the standard output stream. For example, the following two commands are the same; both will redirect the command output (stdout) to the file. To redirect the standard error (stderr) use the 2> operator: command 2> errormessage.txt You can write both stderr and stdout to two separate files: command 2> error.txt 1> output.txt 5. To redirect stderr (error messages) to a file issue the following command: find /etc -name hosts 2> errmsg.txt cat errormsg.txt Your output should be like the following: The file descriptor for stderr is the number 2, so it is used along with the > symbol to redirect the sdterr output to a file called errormsg.txt. On the screenshot above, the cat command is displaying the lines inside the errormsg.txt file. Notice that some records can be accessed (see below); to separate the output data stream from the errors 6. Redirect stdout and stderr to two separate files, type: find / -name root 2> all_error.txt 1> all_output.txt Your output should be like the following: 7. To display a screenful of the error messages type: cat all_error.txt | more 8. To display the data stream, Type cat all_output.txt | more (see below) 9. Standard input (stdin) could also be redirected. Normally stdin comes from the keyboard, but sometimes we want it to come from a file instead. This is great when you want to do something like capitalize data that is inputted from the keyboard as the tr (translate) command will translate a set of characters into another set: tr a-z A-Z this is interesting how do I stop this? Your output should be like the following: Press Ctrl-c to exit the tr command. Execute the following commands to convert to UPPER CASE the output stream (on the previous steps) by using the tr command: head all_output.txt tr a-z A-Z < all_output.txt="" |="" head="" your="" output="" should="" be="" similar="" to="" the="" following:="" 10.="" the="" xargs="" command="" can="" be="" used="" to="" take="" a="" list="" of="" "names"="" from="" the="" output="" of="" another="" command="" and="" generate="" a="" new="" command="" that="" uses="" these="" names="" as="" arguments.="" in="" the="" following="" example,="" you="" will="" use="" a="" command="" called="" cut="" to="" extract="" all="" of="" the="" usernames="" from="" a="" database="" called="" etc/passwd="" (a="" file="" that="" contains="" user="" account="" information).="" first,="" try="" running="" the="" command="" cut="" by="" itself:="" cut="" -d:="" -f1="">< etc/passwd="" your="" output="" should="" be="" like="" the="" following:="" 11.="" the="" output="" in="" the="" previous="" example="" was="" unordered="" and="" scrolled="" off="" the="" screen.="" in="" the="" next="" step,="" you="" are="" going="" to="" execute="" the="" following="" command="" to="" take="" the="" output="" of="" the="" cut="" command="" and="" send="" it="" into="" the="" sort="" command="" to="" provide="" some="" order="" to="" the="" output:="" cut="" -d:="" -f1="">< etc/passwd="" |="" sort="" your="" output="" should="" be="" like="" the="" following:="" 12.="" now="" the="" output="" is="" sorted,="" but="" it="" still="" scrolls="" off="" the="" screen.="" the="" xargs="" command="" will="" take="" all="" of="" the="" usernames="" and="" place="" them="" as="" arguments="" (with="" a="" space="" between="" each="" argument,="" not="" on="" separate="" lines)="" to="" the="" echo="" command.="" execute="" the="" following="" command:="" cut="" -d:="" -f1="">< etc/passwd="" |="" sort="" |="" xargs="" echo="" your="" output="" should="" be="" similar="" to="" the="" following:="" using="" find="" and="" locate="" to="" find="" files="" in="" this="" next="" part="" of="" this="" lab,="" we="" will="" practice="" various="" options="" and="" features="" of="" the="" find="" command.="" the="" find="" command="" is="" a="" very="" flexible="" command="" with="" a="" host="" of="" options="" allowing="" users="" to="" locate="" files="" based="" on="" a="" vast="" array="" of="" criteria="" such="" as="" filename,="" size,="" date,="" type,="" permission,="" etc.="" the="" basic="" command="" construct="" is:="" find=""> -criteria The command will begin the search in the directory specified and recurse (look inside) all of the subdirectories. The locate command yields results faster as it searches a database containing all the files in the system. The “database” is updated once a day so it can become outdated. 1. Search for files beginning in the root directory of the system / containing the name “desktop.” sudo find / -name desktop Your output should be similar to the following: 2. Find files that were modified (or created) less than 120 minutes ago in the specified directory. find /home/gnossa -mmin -120 Your output should be similar to the following: 3. Execute the following commands to find files in the /usr directory that are larger than 20MB in size: find /usr -size +30M and press Enter and then Type: find /usr -size +20M | xargs Your output should be similar to the following (for larger than 30 Mbytes – 20 Mbytes is too large): The second command line was used to merge the file sizes were larger than 20MB: Note that separate lines were merged into a single space of separation for a shorter output. 4. Find files of type “directory” in the specified location. find /var/spool/ -type d Your output should be similar to the following: There is second command demonstrates the -ls option. Normally the default displays just file names. The -ls option provides much more file details as seen on the next screenshot: find /var/spool/ -type d -ls In this hands-on lab you will learn to manipulate large files, as well as look at specific parts of these files to get information about the files; find text in files; compare files; and sort files. All kinds of neat stuff! you will also learn new options to increase the functionality of various document manipulation commands. Task 4-1 The pipe operator | directs the output of one command to the input of another command. In UNIX/ Linux, this operator is very useful for combining commands on one line and yielding output that is easier to read or use. In this project, you use the pipe operator to direct the output of the ls command to the input of the more command so you can “more” easily view the entire contents of a large directory. To redirect the output of the ls command to the more command: 1. Type ls -l /etc and press Enter. Notice that the output of the command scrolls by quickly. 2. Type ls -l /etc I more and press Enter. 3. Notice the output fills the screen and pauses with the prompt "More" displayed on the bottom line. Each time you press the spacebar, the output advances to the next screen. Press the spacebar to scroll a screen at a time or press Enter to advance one line at a time. You also can type q at any point to exit the display of the directory contents. Task 4-2 *** Screenshot Required **** The pipe operator also enables you to combine multiple commands on a single line. In this project, you pipe the contents of a directory listing into the sort command and then pipe the result into the more command. To connect several commands with the pipe operator: 1. Type ls Ietc I sort -r I more and press Enter. This command redirects the directory listing of the Ietc directory to the sort -r command. sort -r sorts the directory listing in reverse order. The sort command's output is redirected to the more command. 2. After you execute the command, you should see the listing of the /etc directory in reverse order. 3. Press the spacebar until the display output is finished Task 4-3 *** Multiple Screenshots Required – Please Replace the Screenshots below with your screenshots **** In this project, you will use several features of the grep command and you learn to combine it with the head command for more manageable output. The head command is used for retrieving the first 10 lines of a file. You can combine the grep and head commands to retrieve only the first 10 lines containing a particular word or phrase. In this taskyou use grep with head to find the first 10 lines in the /etc directory (and subdirectories) that contains the word "binary" To display lines in a file containing a particular word or phrase: 1. To see all the lines in the /etc system directory (and subdirectories)
Answered 1 days AfterFeb 28, 2023

Answer To: Linux+ Lab Series (LX XXXXXXXXXXLab 7b: Using the BASH Shell Chapter 4 - Using the...

Sumit Kumar answered on Mar 02 2023
33 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here