Chapter 5: Create, View and Edit Text Files

Welcome to my blog post documenting my learning journey through the RH124 course! In Chapter 5, we focused on creating, viewing, and editing text files using the Vim editor. We also explored how to save output or errors to a file with shell redirection and process command output through multiple command-line programs with pipes.

In addition, we learned how to set shell variables to run commands and edit Bash startup scripts to modify the behaviour of the shell and programs that are run from the shell.

In this blog post, I will share my experience with the Chapter 5 lab, which had 13 tasks, and explain how I solved each task. I hope this post will be helpful for others who are also learning about creating and editing text files with Vim editor.

Let's dive into the task list and explore how I tackled each one!

Question 1. On workstation, create the lab_file shell variable and assign editing_final_lab.txt as the value. List the student home directory, including hidden directories and files, and redirect the output to the editing_final_lab.txt file using the shell variable.

Solution: To accomplish this task, I used the following commands:

lab_file=editing_final_lab.txt ls -al > $lab_file

In the first command, I created a shell variable named lab_file and assigned the value editing_final_lab.txt to it. It's important to note that there should be no spaces between and after the equal sign when assigning a value to a variable in Bash.

In the second command, I listed the contents of the student home directory, including hidden directories and files, using the ls -al command. I then redirected the output of this command to the editing_final_lab.txt file by using the $lab_file variable with the greater than sign (>).

Question 2. Use Vim to edit the editing_final_lab.txt file. Use the lab_file shell variable.

Solution:

To complete Task 2, I used the vim command with the lab_file shell variable to edit the editing_final_lab.txt file. Using Vim to edit files is a common practice in this course, so I entered the command vim $lab_file to open the file. The $ sign was used to call out the value of the lab_file variable which I had set up in Task 1.

Question 3. Enter the line-based visual mode of Vim. Your screen output may be different than these examples. Remove the first three lines of the editing_final_lab.txt file.

Solution:

In the Vim editor, I entered line-based visual mode by pressing "Shift+V". This allowed me to select lines based on entire lines, rather than individual characters. Using the down arrow key twice, I selected the first three lines. To remove the lines, I simply typed "x". This command deletes the selected lines and moves the cursor to the beginning of the next line.

Question 4. Enter the visual mode of Vim. Remove the last seven characters from the first column on the first line. Preserve only the first four characters of the first column. Use the arrow keys to position the cursor at the last character of the first column on the first line. Delete the selection by typing x. Solution:

I used the arrow keys to move the cursor to the fifth character of the first column on the first line, and then I pressed 'V' to enter visual mode. I used the arrow keys to select the last seven characters of the line, and then I pressed 'x' to delete them.

Question 5. Enter the visual block mode of Vim. Repeat the operation of the previous step, but this time select from the second to the last line. Preserve only the first four characters of the first column.

Solution:

In task 4, I removed the last seven characters from the first column on the first line and preserved only the first four characters. I repeated the same operation for this task but in the visual block mode. To enter visual block mode, I used the Ctrl+V command. Then, I used the arrow keys to highlight the last seven characters of the first column on the second to the last line. Finally, I deleted the highlighted characters by typing 'x'.

Question 6. Enter the visual block mode of Vim and remove the fourth column of the file.

Solution:

To remove the fourth column of the file, I positioned my cursor at the first letter of the fourth column, and then I entered the visual block mode by using the "Ctrl+V" control sequence. I used the right arrow keys to highlight every letter in the fourth column's first line of the fourth column then I pressed the down key until I had selected the entire column. Once the column was highlighted, I pressed the "x" key to delete the selection. This removed the entire fourth column from the file.

Question 7. Enter the visual block mode of Vim to remove the time column, leaving the month and day columns on all lines.

Solution:

To remove the time column and preserve the month and day columns on all lines, I positioned my cursor at the first character of the time column. I then entered the visual block mode by using "Ctrl+V". Next, I used the arrow keys to highlight the time column for all the lines in the file.

Finally, I deleted the selection by typing ‘x’.

Question 8. Enter the visual line mode of Vim and remove the rows that contain the Desktop and Public strings.

Solution: To remove the rows that contain the "Desktop" string, I entered the command mode and typed "/Desktop" to search for the string. Once the search results were displayed, I used the arrow keys to move the cursor to the beginning of the line that contained the string, and then I entered the visual line mode with uppercase V. The full line was selected, and I deleted the selection by typing x.

To remove the rows that contain the "Public" string, I repeated the above process, but this time I used the command "/Public" to search for the string, and then I entered the visual line mode to delete the selection.

Question 9. Save your changes and exit the file.

Solution:

After editing the "editing_final_lab.txt" file in Vim, it's important to save the changes and exit the file properly. Here's how you can do that:

  1. First, ensure you are in the Vim command mode by pressing the "Esc" key on your keyboard.

  2. Then, type ":wq" (without the quotes) and press the "Enter" key.

  3. The ":" puts you in extended command mode, "w" means write, and "q" means quit.

  4. This command will save your changes and exit the file.

It's important to save your changes before quitting Vim, as any unsaved changes will be lost.

Question 10. Back up the editing_final_lab.txt file and append the date (in seconds) at the end of the file name preceded with an underscore (_) character. Use the lab_file shell variable.

Solution:

The command **cp** is used to make a copy of a file. Here I am making a copy of **editing_final_lab.txt** and renaming it with the date in seconds using the **date** command and appending it to the file name with an underscore character **_**. The **$()** notation executes the command date +%s and returns the output as a string to be appended to the file name.

Question 11. Append a dashed line to the editing_final_lab.txt file. The dashed line should contain 12 dash (-) characters for this lab to be graded correctly. Use the lab_file shell variable.

Solution:

The echo command with 12 dashes creates a dashed line, and >> is used to append the output to the editing_final_lab.txt file. The >> is used instead of > to avoid overwriting the file.

Question 12. List the content of the Document directory, and append the output to the editing_final_lab.txt file, displaying the output in the terminal. Use the tee command and the lab_file shell variable.

Solution:

ls Documents/ | tee -a $lab_file

The command above lists the files in the Documents/ directory and pipes the output to the tee command, which writes the output to the standard output and to the file specified by the "-a" option. The file is specified using the variable "$lab_file". In this case, the output indicates only one file in the Documents/ directory called lab_review.txt.

Question 13. Confirm that the directory listing is at the bottom of the lab file. Use the lab_file shell variable.

Solution: vim $lab_file

To confirm that the directory listing is at the bottom of the lab file, I opened the file with the Vim editor and checked for the directory listing. The directory listing should be at the bottom of the file, which indicates that the "ls" command we ran in Task 12 was successful.

Conclusion

In this lab, I explored how to redirect output to a file or program, edit text files from the shell prompt, and change the shell environment by setting and retrieving shell variables. I also learned about a text editor called Vim and how to use it in different modes.

I hope you found this lab helpful and learned new things that you can apply to your daily Linux system administration tasks. Don't hesitate to share your experiences or ask questions in the comment section below. Your feedback and input are always welcome and appreciated!