Bookmark this page

Lab: Manage Files from the Command Line

Note

If you plan to take the RHCSA exam, then use the following approach to maximize the benefit of this Comprehensive Review: attempt each lab without viewing the solution buttons or referring to the course content. Use the grading scripts to gauge your progress as you complete each lab.

Manage files, redirect a specific set of lines from a text file to another file, and edit the text files.

Outcomes

  • Manage files from the command line.

  • Display a specific number of lines from text files and redirect the output to another file.

  • Edit text files.

If you did not reset your workstation and server machines at the end of the last chapter, then save any work that you want to keep from earlier exercises on those machines, and reset them now.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

[student@workstation ~]$ lab start rhcsa-rh124-review1

Specifications

  • Log in to serverb as the student user.

  • Create the /home/student/grading directory.

  • Create three empty files called grade1, grade2, and grade3, in the /home/student/grading directory.

  • Capture the first five lines of the /home/student/bin/manage file in the /home/student/grading/review.txt file.

  • Append the last three lines of the /home/student/bin/manage file to the /home/student/grading/review.txt file. Do not overwrite any existing text in the /home/student/grading/review.txt file.

  • Copy the /home/student/grading/review.txt file to the /home/student/grading/review-copy.txt file.

  • Edit the /home/student/grading/review-copy.txt file so that the Test JJ line appears twice.

  • Edit the /home/student/grading/review-copy.txt file to remove the Test HH line.

  • Edit the /home/student/grading/review-copy.txt file so that a line with A new line exists between the Test BB line and the Test CC line.

  • Create the /home/student/hardcopy hard link to the /home/student/grading/grade1 file. You must create the hard link after completing the earlier step to create the /home/student/grading/grade1 file.

  • Create the /home/student/softcopy symbolic link to the /home/student/grading/grade2 file.

  • Save the output of a command that lists the contents of the /boot directory to the /home/student/grading/longlisting.txt file. The output should be a "long listing" that includes file permissions, owner and group owner, size, and modification date of each file. The output should omit hidden files.

  1. Log in to serverb as the student user.

    [student@workstation ~]$ ssh student@serverb
    ...output omitted...
    [student@serverb ~]$
  2. Create the /home/student/grading directory. If the /home/student directory is your current directory, then you do not need to specify the absolute path to the grading directory when creating it.

    [student@serverb ~]$ mkdir grading
  3. In the /home/student/grading directory, create three empty files called grade1, grade2, and grade3.

    1. Create the empty files called grade1, grade2, and grade3 in the /home/student/grading directory. Apply the brace expansion shell feature to create all three files with a single touch command.

      [student@serverb ~]$ touch grading/grade{1,2,3}
    2. Verify that the grade1, grade2, and grade3 files exist in the /home/student/grading directory.

      [student@serverb ~]$ ls grading/
      grade1  grade2  grade3
  4. Copy the first five lines of the /home/student/bin/manage file to the /home/student/grading/review.txt file.

    1. View the first five lines of the /home/student/bin/manage file and redirect the output to the /home/student/grading/review.txt file. Use the single redirection symbol (>) to overwrite any existing content in the file.

      [student@serverb ~]$ head -5 bin/manage > grading/review.txt
    2. Verify that the /home/student/grading/review.txt file contains the following text:

      Test AA
      Test BB
      Test CC
      Test DD
      Test EE
  5. Append the last three lines of the /home/student/bin/manage file to the /home/student/grading/review.txt file. Use the double redirection symbol (>>) to append the output and preserve the contents of the file.

    1. View the last three lines of the /home/student/bin/manage file and append the output to the /home/student/grading/review.txt file.

      [student@serverb ~]$ tail -3 bin/manage >> grading/review.txt
    2. Verify that the /home/student/grading/review.txt file contains the following text:

      Test AA
      Test BB
      Test CC
      Test DD
      Test EE
      Test HH
      Test II
      Test JJ
  6. Copy the /home/student/grading/review.txt file to the /home/student/grading/review-copy.txt file.

    1. Navigate to the /home/student/grading directory.

      [student@serverb ~]$ cd grading/
      [student@serverb grading]$
    2. Copy the /home/student/grading/review.txt file to the /home/student/grading/review-copy.txt file.

      [student@serverb grading]$ cp review.txt review-copy.txt
    3. Navigate back to the home directory of the student user.

      [student@serverb grading]$ cd
      [student@serverb ~]$
  7. Edit the /home/student/grading/review-copy.txt file to have two sequential Test JJ lines.

    1. Use the vim text editor to open the /home/student/grading/review-copy.txt file.

      [student@serverb ~]$ vim grading/review-copy.txt
    2. From the command mode in vim, scroll down to the Test JJ line. Press the y key twice to copy the line of text, and press the p key to paste it below the cursor. Type wq to save the changes and quit vim. Verify that the /home/student/grading/review-copy.txt file contains the following text:

      Test AA
      Test BB
      Test CC
      Test DD
      Test EE
      Test HH
      Test II
      Test JJ
      Test JJ
  8. Edit the /home/student/grading/review-copy.txt file to remove the Test HH line.

    1. Use the Vim text editor to open the /home/student/grading/review-copy.txt file.

      [student@serverb ~]$ vim grading/review-copy.txt
    2. From the command mode in Vim, scroll down to the Test HH line. Press the d key twice on your keyboard to delete the line of text. Type wq to save the changes and quit vim. Verify that the /home/student/grading/review-copy.txt file contains the following text:

      Test AA
      Test BB
      Test CC
      Test DD
      Test EE
      Test II
      Test JJ
      Test JJ
  9. Edit the /home/student/grading/review-copy.txt file so that the line with A new line exists between the Test BB line and the Test CC line.

    1. Use the Vim text editor to open the /home/student/grading/review-copy.txt file.

      [student@serverb ~]$ vim grading/review-copy.txt
    2. From the command mode in Vim, scroll down to the Test CC line. Press the i key to switch to the insert mode while keeping the cursor at the beginning of the Test CC line. From the insert mode, press Enter to create a blank line above the cursor. Use the up arrow to navigate to the blank line and create the line of A new line text. Press the Esc key to switch back to the command mode. Type wq to save the changes and to quit Vim. Verify that the /home/student/grading/review-copy.txt file contains the following text.

      Test AA
      Test BB
      A new line
      Test CC
      Test DD
      Test EE
      Test II
      Test JJ
      Test JJ
  10. Create the /home/student/hardcopy hard link to the /home/student/grading/grade1 file.

    1. Create the /home/student/hardcopy hard link to the /home/student/grading/grade1 file.

      [student@serverb ~]$ ln grading/grade1 hardcopy
    2. View the link count of the /home/student/grading/grade1 file.

      [student@serverb ~]$ ls -l grading/grade1
      -rw-r--r--. 2 student student 0 Mar  6 16:45 grading/grade1
  11. Create the /home/student/softcopy symbolic link to the /home/student/grading/grade2 file.

    1. Create the /home/student/softcopy symbolic link to the /home/student/grading/grade2 file.

      [student@serverb ~]$ ln -s grading/grade2 softcopy
    2. View the properties of the /home/student/softcopy symbolic link.

      [student@serverb ~]$ ls -l softcopy
      lrwxrwxrwx. 1 student student 14 Mar  6 17:58 softcopy -> grading/grade2
  12. List the contents of the /boot directory and redirect the output to the /home/student/grading/longlisting.txt file. The output should be a long listing that includes the file permissions, owner and group owner, size, and modification date of each file. The output should omit hidden files.

    1. View the contents of the /boot directory in the long listing format, and omit hidden files. Redirect the output to the /home/student/grading/longlisting.txt file.

      [student@serverb ~]$ ls -l /boot > grading/longlisting.txt
    2. Return to the workstation system as the student user.

      [student@serverb ~]$ exit
      logout
      Connection to serverb closed.

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade rhcsa-rh124-review1

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish rhcsa-rh124-review1

Revision: rh124-9.3-770cc61