Bookmark this page

Lab: Managing Files in Linux

Edit files, use filtering and redirection, and manipulate command output.

Outcomes

  • Interpret directory listings and identify file types.

  • Perform command output redirection into files.

  • Use a command-line text editor to change files.

  • Use command pipelines to manipulate command output.

As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available.

[student@workstation ~]$ lab start files-review

In this exercise, you create several text files. You can create these text files by redirecting command output. Make sure to use the file names from the exercise, and create the files in the /home/student directory.

Instructions

  1. In the Files application, navigate to the rhel_system_roles directory and locate the hidden file. Inspect the hidden file and create the attributes.txt file to save specific attributes of the hidden file.

    The contents of the attributes.txt file should look similar to the following sample:

    file name: example
    file size: 500 bytes
    file type: data
    1. Open the Files application by clicking ActivitiesFiles. Navigate to the rhel_system_roles directory. Click the application's menu at the upper right, and then select the Show Hidden Files checkbox.

    2. Identify the hidden file by reviewing the file name. The hidden file name starts with a period (.) character.

      Files in the rhel_system_roles directory

      The .ansible-lint file is a hidden file, so it matches the description.

    3. On the command line, review the contents of the rhel_system_roles directory, including hidden files.

      [student@workstation ~]$ ls -la rhel_system_roles
      total 680
      drwxr-xr-x.  7 root root   4096 Nov 17  2022 .
      drwxr-xr-x.  3 root root     31 Nov 17  2022 ..
      -rw-r--r--.  1 root root    192 Sep 29  2022 .ansible-lint
      -rw-r--r--.  1 root root  18092 Sep 29  2022 COPYING-firewall
      -rw-r--r--.  1 root root   1057 Sep 29  2022 COPYING-kdump
      ...output omitted...
    4. Determine the hidden file's type.

      [student@workstation ~]$ file rhel_system_roles/.ansible-lint
      rhel_system_roles/.ansible-lint: ASCII text
    5. Create the attributes.txt file to save the hidden file's name, size, and file type.

      If you are using the echo command, be sure to append each new line.

      [student@workstation ~]$ echo "file name: .ansible-lint" > attributes.txt
      [student@workstation ~]$ echo "file size: 192 bytes" >> attributes.txt
      [student@workstation ~]$ echo "file type: ASCII text" >> attributes.txt
  2. Create the time.txt file to save the output of the timedatectl command. Use a text editor to remove all leading white space characters from each line of the file.

    Use a pipe to view the contents of the time.txt file and to sort the file contents in reverse alphabetical order by using the sort command. Create the time_sorted.txt file and save the sorting results.

    If you are not sure how to use the sort command, use the sort --help command.

    1. Create the time.txt file to save the output of the timedatectl command.

      [student@workstation ~]$ timedatectl > time.txt
    2. Use the nano editor to edit the time.txt file. Remove all leading white space characters from each line of the file.

      Press Ctrl+x to exit the editor, and then press Y and Enter to save your changes.

      [student@workstation ~]$ nano time.txt
    3. Confirm that your results look similar to the following example:

      [student@workstation ~]$ cat time.txt
      Local time: Sat 2023-10-21 08:33:59 EDT
      Universal time: Sat 2023-10-21 12:33:59 UTC
      RTC time: Sat 2023-10-21 12:33:59
      Time zone: America/New_York (EDT, -0400)
      System clock synchronized: yes
      NTP service: active
      RTC in local TZ: no
    4. Access the help information for the sort command, and look for an option to reverse the sort.

      [student@workstation ~]$ sort --help
      Usage: sort [OPTION]... [FILE]...
        or:  sort [OPTION]... --files0-from=F
      Write sorted concatenation of all FILE(s) to standard output.
      ...output omitted...
        -r, --reverse               reverse the result of comparisons
      ...output omitted...

      The sort command -r option sorts the content in reverse alphabetical order.

    5. View the contents of the time.txt file and use a pipe to sort the output in reverse alphabetical order. Redirect the standard output to the new time_sorted.txt file.

      [student@workstation ~]$ cat time.txt | sort -r > time_sorted.txt
  3. Review the number of words in the existing words file. Create the words_count.txt file to save the number of words.

    Additionally, create the zzz.txt file to save the last 26 lines of the words file. Using the nano editor, replace all capital Z letters with lowercase z letters and save your changes.

    1. Review the number of words in the words file.

      [student@workstation ~]$ wc -l words
      479826 words
    2. Record the correct number in the words_count.txt file.

      [student@workstation ~]$ echo 479826 > words_count.txt
    3. Capture the last 26 lines of the words file in the zzz.txt file.

      [student@workstation ~]$ tail -n 26 words > zzz.txt
    4. Use the nano editor to replace all uppercase Z letters with lowercase z letters.

      [student@workstation ~]$ nano zzz.txt

      In the editor screen, press Ctrl+\ and type an uppercase Z in the Search (to replace) prompt. In the Replace with: prompt, type a lowercase z and press Enter. Then, type a to replace all instances of uppercase Z letters.

      Press Ctrl+x to exit the editor, and then press Y and Enter to save your changes.

  4. Identify the file type of the /var/log/lastlog file. Try to view the file by using the cat command.

    You can reset the terminal by using the stty sane command if the terminal becomes nonfunctional.

    Use the lastlog command to process the /var/log/lastlog file. From the output, identify the terminal port that the gdm user used for their last log in, and save this value in the new gdm.txt file.

    1. Identify the type of the /var/log/lastlog file.

      [student@workstation ~]$ file /var/log/lastlog
      /var/log/lastlog: data

      The /var/log/lastlog file is a generic data file that the file command cannot identify more specifically.

    2. Try to view the file by using the cat command.

      [student@workstation ~]$ cat /var/log/lastlog

      Because the lastlog file is a binary file, the output is unreadable. If the terminal becomes nonfunctional, reset it by using the stty sane command.

    3. Use the lastlog command to process the /var/log/lastlog binary database file.

      [student@workstation ~]$ lastlog
      Username         Port     From            Latest
      root             pts/0                    Thu Oct 19 07:38:18 -0400 2023
      bin                                       **Never logged in**
      daemon                                    **Never logged in**
      adm                                       **Never logged in**
      sync                                      **Never logged in**
      gdm              tty1                     Sat Oct 21 09:45:21 -0400 2023
      ...output omitted...

      The output lists every user on the system and when they last logged in. The four columns include the username, the terminal port, the remote IP address of the user, and when they logged in.

    4. Identify the terminal port that the gdm user used to log in, and capture the value in the gdm.txt file.

      [student@workstation ~]$ echo tty1 > gdm.txt
  5. Identify how many city-based time zone files are stored in the zoneinfo directory for the continent of Asia. Create the asian_cities.txt file with the required time zone file names.

    Count the cities in the asian_cities.txt file and save the result in the new asian_cities_count.txt file.

    Obtain a sorted list of time zone files for European cities. From the resulting list, save the first ten cities to the 10_cities.txt file. Also, reverse each line of the 10_cities.txt file characterwise and save the results in the 10_cities_reversed.txt file.

    1. List the contents of the zoneinfo directory. You can use the long listing option to display an ordered list of the contents.

      [student@workstation ~]$ ls -l zoneinfo/
      total 392
      drwxr-xr-x.  2 student student   4096 Oct 23 16:17 Africa
      drwxr-xr-x.  6 student student   4096 Oct 23 16:17 America
      drwxr-xr-x.  2 student student    187 Oct 23 16:17 Antarctica
      drwxr-xr-x.  2 student student     26 Oct 23 16:17 Arctic
      drwxr-xr-x.  2 student student   4096 Oct 23 16:17 Asia
      drwxr-xr-x.  2 student student   4096 Oct 23 16:17 Atlantic
      ...output omitted...
    2. List the contents of the zoneinfo/Asia directory.

      [student@workstation ~]$ ls zoneinfo/Asia/
      Aden       Bahrain     Chongqing
      Almaty     Baku        Chungking
      Amman      Bangkok     Colombo
      ...output omitted...
    3. Create the asian_cities.txt file to list all time zone file names under the Asia directory.

      [student@workstation ~]$ ls zoneinfo/Asia/ > asian_cities.txt
    4. Count the cities from the previous step and save the result in the asian_cities_count.txt file.

      [student@workstation ~]$ cat asian_cities.txt | wc -l > asian_cities_count.txt
    5. Sort the time zone files for European cities. Save the first ten cities to the 10_cities.txt file.

      [student@workstation ~]$ ls zoneinfo/Europe | head > 10_cities.txt
    6. Reverse each line of the 10_cities.txt file characterwise and save the results in the 10_cities_reversed.txt file.

      [student@workstation ~]$ rev 10_cities.txt > 10_cities_reversed.txt

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 files-review

Finish

As the student user on the workstation machine, 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 files-review

Revision: rh104-9.1-3d1f2bc