Bookmark this page

Lab: Managing Files from the Command Line

In this review, you will manage files, redirect a specific set of lines from a text file to another file and edit the text files.

Outcomes

You should be able to:

  • Manage files from the command line.

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

  • Edit text files.

Copy any files or work you wish to keep to other systems before resetting. Reset the workstation, servera, and serverb systems now. Wait until the workstation, servera, and serverb systems are started.

Log in to workstation as student using student as the password.

On workstation, run lab rhcsa-rh124-review1 start to start the comprehensive review. This script creates the necessary files to set up the environment correctly.

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

Instructions

Accomplish the following tasks on serverb to complete the exercise.

  • Create a new directory called /home/student/grading.

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

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

  • Append the last three lines of /home/student/bin/manage-files to the file /home/student/grading/manage-files.txt. You must not overwrite any text already in the file /home/student/grading/manage-files.txt.

  • Copy /home/student/grading/manage-files.txt to /home/student/grading/manage-files-copy.txt.

  • Edit the file /home/student/grading/manage-files-copy.txt so that there should be two sequential lines of text reading Test JJ.

  • Edit the file /home/student/grading/manage-files-copy.txt so that the Test HH line of text must not exist in the file.

  • Edit the file /home/student/grading/manage-files-copy.txt so that the line A new line should exist between the line reading Test BB and the line reading Test CC.

  • Create a hard link named /home/student/hardlink to the file /home/student/grading/grade1. You will need to do this after creating the empty file /home/student/grading/grade1 as specified above.

  • Create a soft link named /home/student/softlink to the file /home/student/grading/grade2.

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

  1. Create a new directory called /home/student/grading.

    1. From workstation, open an SSH session to serverb as student.

      [student@workstation ~]$ ssh student@serverb
      ...output omitted...
      [student@serverb ~]$ 
    2. Use the mkdir command to create the /home/student/grading directory.

      [student@serverb ~]$ mkdir grading

      As you ran the preceding command while in the home directory of the student user, you did not specify the absolute path to the grading directory while creating it.

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

    1. Use the touch command to 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. Use the ls command to verify that the grade1, grade2, and grade3 files exist under the directory /home/student/grading.

      [student@serverb ~]$ ls grading/
      grade1  grade2  grade3
  3. Capture the first five lines of the /home/student/bin/manage-files file in the /home/student/grading/manage-files.txt file.

    1. Use the head command to view the first five lines of the file /home/student/bin/manage-files and redirect the output to the file /home/student/grading/manage-files.txt.

      [student@serverb ~]$ head -5 bin/manage-files > grading/manage-files.txt

      The preceding command uses the single redirection symbol (>) to save the command output to /home/student/grading/manage-files.txt so that any existing content in the file gets overwritten.

    2. Verify that the file /home/student/grading/manage-files.txt contains the following text.

      Test AA
      Test BB
      Test CC
      Test DD
      Test EE
  4. Append the last three lines of /home/student/bin/manage-files to the file /home/student/grading/manage-files.txt. You must not overwrite any text already in the file /home/student/grading/manage-files.txt.

    1. Use the tail command to view the last three lines of the file /home/student/bin/manage-files and append the output to /home/student/grading/manage-files.txt.

      [student@serverb ~]$ tail -3 bin/manage-files >> grading/manage-files.txt

      The preceding command uses the double redirection symbol (>>) to append the output to /home/student/grading/manage-files.txt so that the existing contents in the file are preserved.

    2. Verify that the file /home/student/grading/manage-files.txt contains the following text.

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

    1. Use the cd command to navigate to the directory /home/student/grading.

      [student@serverb ~]$ cd grading/
      [student@serverb grading]$ 
    2. Use the cp command to copy the /home/student/grading/manage-files.txt file to /home/student/grading/manage-files-copy.txt.

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

      [student@serverb grading]$ cd
      [student@serverb ~]$ 
  6. Edit the file /home/student/grading/manage-files-copy.txt so that there should be two sequential lines of text reading Test JJ.

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

      [student@serverb ~]$ vim grading/manage-files-copy.txt
    2. From the command mode in vim, scroll down to the line that has the Test JJ line of text. Press the y key twice on your keyboard 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/manage-files-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

      Notice that the preceding content includes two copies of the Test JJ line of text.

  7. Edit the file /home/student/grading/manage-files-copy.txt so that the Test HH line of text must not exist in the file.

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

      [student@serverb ~]$ vim grading/manage-files-copy.txt
    2. From the command mode in vim, scroll down to the line that has the Test HH line of text. 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/manage-files-copy.txt file contains the following text.

      Test AA
      Test BB
      Test CC
      Test DD
      Test EE
      Test II
      Test JJ
      Test JJ

      Notice that the preceding content does not include the Test HH line of text.

  8. Edit the file /home/student/grading/manage-files-copy.txt so that the line A new line exists between the line reading Test BB and the line reading Test CC.

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

      [student@serverb ~]$ vim grading/manage-files-copy.txt
    2. From the command mode in vim, scroll down to the line that has the Test CC line of text. Press the i key on the keyboard to switch to the insert mode while keeping the cursor at the beginning of the Test CC line of text. From the insert mode, press the Enter key on the keyboard to create a blank line above the cursor. Use the up arrow to navigate to the blank line and create the A new line line of text. Press the Esc key on the keyboard to switch back to the command mode. Type :wq to save the changes and quit vim. Verify that the /home/student/grading/manage-files-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

      Notice that the preceding content includes the A new line line of text.

  9. Create a hard link named /home/student/hardlink to the file /home/student/grading/grade1.

    1. Use the ln command to create the hard link named /home/student/hardlink to the file /home/student/grading/grade1. You will need to do this after creating the empty file /home/student/grading/grade1 as specified above.

      [student@serverb ~]$ ln grading/grade1 hardlink
    2. Use the ls -l command to view the link count of the /home/student/grading/grade1 file.

      [student@serverb ~]$ ls -l grading/grade1
      -rw-rw-r--. 2 student student 0 Mar  6 16:45 grading/grade1
  10. Create a soft link named /home/student/softlink to the file /home/student/grading/grade2.

    1. Use the ln -s command to create the soft link named /home/student/softlink to the file /home/student/grading/grade2.

      [student@serverb ~]$ ln -s grading/grade2 softlink
    2. Use the ls -l command to view the properties of the /home/student/softlink soft link.

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

    1. Use the ls -l command to view the contents of the directory /boot in the long listing format and redirect the output to the file /home/student/grading/longlisting.txt.

      [student@serverb ~]$ ls -l /boot > grading/longlisting.txt
    2. Log out of serverb.

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

Evaluation

On workstation, run the lab rhcsa-rh124-review1 grade command to confirm success of this exercise.

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

Finish

On workstation, run lab rhcsa-rh124-review1 finish to complete the comprehensive review. This script deletes the files and directories created during the start of the comprehensive review and ensures that the environment on serverb is clean.

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

This concludes the comprehensive review.

Revision: rh124-8.2-df5a585