Bookmark this page

Lab: Manipulating Files and Directories

Create files by using redirection, and organize files and directories.

Modify file contents, ownership, and permissions.

Outcomes

  • Organize files and directories.

  • Redirect command output into files.

  • Use a text editor to modify files.

  • Create compressed archives.

  • Use sudo privileges with commands.

  • Change the ownership and permissions of files.

If you did not reset your workstation and server machines at the end of the last chapter, then save any work 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 environment for this exercise, and to ensure that all required resources are available.

[student@workstation ~]$ lab start compreview-review1

In this exercise, you create some files on the servera machine. Make sure to use the exact file names from the exercise and create the files in the /home/student directory.

The Solution section for this lab displays command-line steps for each specification, but you can use graphical or command-line tools to perform the required tasks.

To work on this comprehensive review lab, imagine the following scenarios.

You requested a new computer and the IT Support team is ready to fulfill the request. To prepare your new workstation, the IT Support team requested some files, inventories, and back ups of the servera machine.

Additionally, a client asked you to organize and update some ebook files.

Specifications

  • The IT Support team requested an archive called ~/user_backup.tar.gz. This archive must contain a copy of the /etc/passwd file called ~/user_info.txt, and a copy of the /etc/shadow file called ~/user_passwords.txt. Remove all system accounts and leave all regular accounts in both files. If required, use sudo privileges to create the copies.

    The team also requested an inventory of the /etc directory. You must find all files and directories, including any items that failed the search. Save successful results in the etc_success.txt file, and failed results to the etc_errors.txt file.

    Finally, the team requested a copy of the documentation for the mutt email client. You can identify the documentation directory by finding a path that contains the doc directory. Copy all files from the resulting directory that have a .rc file extension to the new ~/mutt-rc directory.

  • A client requested that you organize the books in the ~/books/unsorted directory on the servera machine before the books are published. Identify the category of each book by reviewing the file name. Create the necessary directories to match the following directory structure and move each book to the corresponding directory:

    books/
    ├── fiction
    │   ├── children
    │   ├── mystery
    │   └── romance
    └── nonfiction
    │   ├── dictionary
    │   ├── history
    │   └── travel
    └── unsorted
  • The client also requested the following operations:

    The file name for each book includes a number; delete any file that contains a four or higher in its name.

    Change the file extension to .pdf for all files in the ~/books/travel directory.

    Create the ~/books/fiction/crime symbolic link that points to the mystery directory.

    Copy the children directory with all its contents to the nonfiction directory.

    Assign the librarians group as the owner of the books directory. Set all permissions for the owner user and the librarians group. Set read and execute permissions for others.

    Save the total size of the books directory in a human-readable size unit to the books_size.txt file. The file must look like the following example:

    13M
  1. As the student user on the servera machine, create the ~/user_backup.tar.gz archive, which includes the user_info.txt and user_passwords.txt files. Copy the /etc/passwd file and name it ~/user_info.txt. Remove all system account entries from the user_info.txt file.

    Copy the /etc/shadow file and name it ~/user_passwords.txt. If required, use sudo privileges to create the copy. Make sure that the student user owns the user_passwords.txt file. In the user_passwords.txt file, remove all entries that do not correspond to the users that are listed in the user_info.txt file.

    1. Log in to the servera machine as the student user. Use student as the password.

      [student@workstation ~]$ ssh student@servera
      student@servera's password: student
      [student@servera ~]$
    2. Copy the /etc/passwd file to the user_info.txt file in your home directory.

      [student@servera ~]$ cp /etc/passwd user_info.txt
    3. Use a text editor to remove all system users. The UIDs of system users are less than 1000.

      The user_info.txt file should look similar to the following example:

      [student@servera ~]$ cat user_info.txt
      student:x:1000:1000:Student User:/home/student:/bin/bash
      devops:x:1001:1001:Devops User:/home/devops:/bin/bash
      cloud-user:x:1002:1002:Cloud User:/home/cloud-user:/bin/bash
    4. Use sudo privileges with the cat command to view the /etc/shadow file and redirect the output to the user_passwords.txt file. Use student as the password.

      [student@servera ~]$ sudo cat /etc/shadow > user_passwords.txt
      [sudo] password for student: student
      [student@servera ~]$ ls -l user_passwords.txt
      -rw-r--r--. 1 student student 1201 Dec  3 01:17 user_passwords.txt
    5. Modify the user_passwords.txt file so that it matches all the user entries that are listed in the user_info.txt file.

      The user_passwords.txt file should look similar to the following example:

      [student@servera ~]$ cat user_passwords.txt
      student:$6$8oIjLCsc$/...:19313:0:99999:7:::
      devops:$6$MZcUmP2e$o/...:19313:0:99999:7:::
      cloud-user:!!:19340:0:99999:7:::
    6. Create the user_backup.tar.gz archive that includes the user_info.txt and user_passwords.txt files.

      [student@servera ~]$ tar --create --gzip --file user_backup.tar.gz \
      user_info.txt user_passwords.txt
      [student@servera ~]$ ls -l user_backup.tar.gz
      -rw-r--r--. 1 student student 453 Dec  3 01:24 user_backup.tar.gz
  2. Create an inventory of the /etc directory by finding all its files and directories. Save the resulting files and directories to the etc_success.txt file. If the search returns any errors, then save the errors to the etc_errors.txt file.

    1. Find all files and directories in the /etc directory.

      [student@servera ~]$ find /etc
      /etc
      /etc/pki
      ...output omitted...
      find: '/etc/pki/rsyslog': Permission denied
      ...output omitted...
      find: '/etc/sudoers.d': Permission denied
      ...output omitted...

      The /etc directory contains files that the student user does not have permissions to view, so the command returns some error messages.

    2. Find all files and directories in the /etc directory. Redirect the results to the etc_success.txt file, and redirect all errors to the etc_errors.txt file.

      [student@servera ~]$ find /etc > etc_success.txt 2> etc_errors.txt
    3. Verify that the etc_success.txt file does not contain errors.

      [student@servera ~]$ cat etc_success.txt
      /etc
      /etc/pki
      /etc/pki/tls
      /etc/pki/tls/ct_log_list.cnf
      ...output omitted...
    4. Verify the contents of the ~/etc_errors.txt file.

      [student@servera ~]$ cat etc_errors.txt
      find: '/etc/pki/rsyslog': Permission denied
      find: '/etc/dhcp': Permission denied
      ...output omitted...
  3. Create a copy of the documentation for the mutt email client. You can identify the documentation directory by finding a path that contains the doc directory. Copy all files from the resulting directory that have a .rc file extension to the new ~/mutt-rc directory.

    1. Search the entire file system for files and directories that are called mutt. Discard any error messages to make it easier to identify the correct location.

      [student@servera ~]$ find / -iname 'mutt' 2>/dev/null
      /usr/bin/mutt
      /usr/share/licenses/mutt
      /usr/share/bash-completion/completions/mutt
      /usr/share/doc/mutt

      The documentation directory for the mutt package is /usr/share/doc/mutt.

    2. Change into the directory and list the files that have a .rc file extension.

      [student@servera ~]$ cd /usr/share/doc/mutt
      [student@servera mutt]$ ls -l *.rc
      -rw-r--r--. 1 root root  441 May  2  2020 Mush.rc
      -rw-r--r--. 1 root root 1022 May  2  2020 Pine.rc
      -rw-r--r--. 1 root root  606 May  2  2020 Tin.rc
      -rw-r--r--. 1 root root 4833 May 11  2022 gpg.rc
      -rw-r--r--. 1 root root 1704 May 11  2022 pgp2.rc
      -rw-r--r--. 1 root root 1788 May 11  2022 pgp5.rc
      -rw-r--r--. 1 root root 1720 May 11  2022 pgp6.rc
      -rw-r--r--. 1 root root 4102 May 11  2022 smime.rc
    3. Create the mutt-rc directory in your home directory, and copy the .rc files from the mutt directory into the new directory.

      [student@servera mutt]$ mkdir ~/mutt-rc
      [student@servera mutt]$ cp *.rc ~/mutt-rc
    4. Verify the contents of the mutt-rc directory.

      [student@servera mutt]$ ls -l ~/mutt-rc
      ...output omitted...
      -rw-r--r--. 1 student student 1720 Dec  3 02:26 pgp6.rc
      -rw-r--r--. 1 student student 4102 Dec  3 02:26 smime.rc
    5. Return to the student user home directory.

      [student@servera mutt]$ cd ~/
      [student@servera ~]$
  4. Organize the ~/books directory contents. Review the books directory, and create the necessary directories to match the following directory structure.

    books/
    ├── fiction
    │   ├── children
    │   ├── mystery
    │   └── romance
    └── nonfiction
    │   ├── dictionary
    │   ├── history
    │   └── travel
    └── unsorted

    Each file name includes a category type that matches a directory name; move each file to the matching directory. Each file name also includes a number; delete any file that contains a four or higher in its name.

    1. Review the books directory contents.

      [student@servera ~]$ tree books/
      books/
      └── unsorted
          ├── children_book_1.epub
          ├── children_book_2.epub
          ├── children_book_3.epub
          ├── dictionary_book_1.epub
          ├── dictionary_book_2.epub
          ├── dictionary_book_3.epub
          ├── history_book_1.epub
      ...output omitted...
    2. Delete all files that contain a four or higher in its name.

      [student@servera ~]$ cd books/unsorted/
      [student@servera unsorted]$ rm *4* *5* *6* *7*
    3. Verify that the unwanted files no longer exist.

      [student@servera unsorted]$ ls -l
      ...output omitted...
      -rw-r--r--. 1 student student 14 Dec  2 23:35 children_book_3.epub
      -rw-r--r--. 1 student student 14 Dec  2 23:35 dictionary_book_1.epub
      -rw-r--r--. 1 student student 14 Dec  2 23:35 dictionary_book_2.epub
      -rw-r--r--. 1 student student 14 Dec  2 23:35 dictionary_book_3.epub
      ...output omitted...
    4. Create the required directory structure.

      [student@servera unsorted]$ cd ~/books/
      [student@servera books]$ mkdir -p fiction/mystery
      [student@servera books]$ mkdir fiction/children fiction/romance
      [student@servera books]$ mkdir -p nonfiction/dictionary
      [student@servera books]$ mkdir nonfiction/history nonfiction/travel
    5. Verify the books directory structure. Use the tree command with -d option to list only directories.

      [student@servera books]$ tree -d ~/books
      books/
      ├── fiction
      │   ├── children
      │   ├── mystery
      │   └── romance
      └── nonfiction
      │   ├── dictionary
      │   ├── history
      │   └── travel
      └── unsorted
    6. Organize the files by moving them in the directories that match their category names.

      [student@servera books]$ cd unsorted/
      [student@servera unsorted]$ mv children* ../fiction/children/
      [student@servera unsorted]$ mv dictionary* ../nonfiction/dictionary/
      [student@servera unsorted]$ mv history* ../nonfiction/history/
      [student@servera unsorted]$ mv mystery* ../fiction/mystery/
      [student@servera unsorted]$ mv romance* ../fiction/romance/
      [student@servera unsorted]$ mv travel* ../nonfiction/travel/
  5. Manage the books directory and its contents per the client's request.

    Set .pdf as the new file extension for all files in the ~/books/travel directory.

    Create the ~/books/fiction/crime symbolic link that points to the mystery directory.

    Copy the children directory with all its contents to the nonfiction directory.

    Make sure that the books directory and its contents are owned by the librarians group. The student user and the librarians group must have permissions to read, write, and execute. Set read and execute permissions for others.

    1. Update all files in the travel directory from the .epub extension to the .pdf extension.

      [student@servera unsorted]$ cd ~/books/nonfiction/travel/
      [student@servera travel]$ mv travel_book_1.epub travel_book_1.pdf
      [student@servera travel]$ mv travel_book_2.epub travel_book_2.pdf
      [student@servera travel]$ mv travel_book_3.epub travel_book_3.pdf
    2. Create the crime symbolic link that points to the mystery directory.

      [student@servera travel]$ cd ../../fiction/
      [student@servera fiction]$ ln -s mystery crime
      [student@servera fiction]$ ls -l
      total 0
      drwxr-xr-x. 2 student student 90 Dec  3 02:05 children
      lrwxrwxrwx. 1 student student  7 Dec  3 02:09 crime -> mystery
      drwxr-xr-x. 2 student student 87 Dec  3 02:06 mystery
      drwxr-xr-x. 2 student student 87 Dec  3 02:06 romance
    3. Copy the children directory with all its contents to the nonfiction directory.

      [student@servera fiction]$ cp -r children/ ../nonfiction/
      [student@servera fiction]$ ls ../nonfiction/children
      children_book_1.epub  children_book_2.epub  children_book_3.epub
    4. Make sure that the entire books directory structure is owned by the librarians group. Set all permissions for the student user and the librarians groups; set read and execute permissions for others. Use your sudo privilege with the chown and chmod command.

      [student@servera fiction]$ cd ~/
      [student@servera ~]$ sudo chown -R :librarians books/
      [sudo] password for student: student
      [student@servera ~]$ sudo chmod -R 0775 books/
      [student@servera ~]$ ls -ld books
      drwxrwxr-x. 5 student librarians 56 Dec  3 02:04 books
  6. Save the total size of the books directory in a human-readable size unit to the books_size.txt file.

    1. Check the total size of the books directory including all its content, and use a human-readable size unit.

      [student@servera ~]$ du -h books
      ...output omitted...
      84K     books
    2. Save the total size of the books directory to the books_size.txt file.

      [student@servera ~]$ echo 84K > books_size.txt
      [student@servera ~]$ cat books_size.txt
      84K
    3. Exit the servera machine.

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

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 compreview-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 compreview-review1

Revision: rh104-9.1-3d1f2bc