Edit Page

Lab 2: The Linux Filesystem and Common Utilities

The Linux file system and security model. Manage and organize files from the bash shell prompt. Create backups and mount external storage.

Objectives

  1. Learn the basics of the filesystem structure and operations.
  2. Use command line tools to navigate the filesystem and display the contents of directories in a tree-like format.
  3. Use common utilities such as cp, mv, mkdir, rm, rmdir.
  4. Understand the permission-based security model of the filesystem.
  5. Performing a backup using rsync.
  6. Adding and removing additional storage devices.

Setup

  • Start by updating the package index and updating existing packages:

    sudo apt update
    sudo apt upgrade -y
    
  • Install tree and rsync if they are not already included on your system by default:

    • tree, which is a tool that lists the contents of directories in a tree-like format.
    • rsync, which is a fast file-copying tool that is used to transfer and synchronize files between both local and remote systems.
    sudo apt install tree
    sudo apt install rsync
    
  • Obtain an external storage device (e.g., external USB hard disk or flash drive).

Notes

  • Below is a list of commands you will be using in these lab exercises. Make sure you read the manual of each one using the command man. For example, running man tree will open up the manual for the tree program. You can scroll up or down using the keyboard and exit by pressing the q key.

    • tree
    • rsync
    • mkdir
    • chown
    • chmod
    • mount
    • umount
  • The term “your home directory” refers to the directory under /home/<your-user-name> or the directory that you can navigate to using cd ~.

    • Replace <your-user-name> with your logged in user name. If you are not sure, run the command whoami.
  • If you attempt to change the owner of a file or directory and receives “operation not permitted”, then prefix the command with sudo (e.g., sudo chown root somefile.txt).

Lab Exercises

  1. Getting info
  • Obtain the following results
    • Use the tree tool to list the content of the /home directory and its sub-directories in a tree-like format.
    • When would you use tree over ls?
  1. Create a directory called code under your home directory (i.e., /home/<your-user-name>) with the following structure.

    NOTE: Replace <your-user-name> with your logged in user name. If you are not sure, run the command whoami or use tilde ~/, which is a “shortcut” to denote your home directory.

    code/
    └── my-project
          ├── LICENSE.txt
          ├── NOTICE.txt
          ├── README.txt
          └── src
              ├── assembly
              ├── it
              ├── main
              │   ├── filters
              │   ├── java
              │   ├── resources
              │   └── webapp
              ├── site
              └── test
                  ├── filters
                  ├── java
                  └── resources
    
    • Find the owner and group owner of the file README.txt?
    • Edit the file README.txt using a text editor and write your name inside it.
    • Use the command chmod to change the write permission of the owner, so the owner can’t write to the file. Keep the write permission for the group. List the permissions using ls -l code/my-project/README.txt to inspect it. Now, edit the file and save it. What happens?
    • Change the write permission of both the owner and group owner, so they can’t write to the file. Now, edit the file and save it. What happens?
  2. Create a new directory called project-2 under /home/<your-user-name>/code. mkdir code/project-2.

    • Copy the content of code/my-project into code/project-2 using one command using: cp -r code/my-project/* code/project-2.
    • Rename project-2 directory into java-starter.
    • Remove the directory code/my-project and its content.
  3. Use the rsync tool to make a backup (sync the content) of the directory code into /home/<your-user-name>/backup:

    • Create a new directory /home/<your-user-name>/backup
    • Run rsync -r /home/<your-user-name>/code/ /home/<your-user-name>/backup/
      • Note that there is a trailing slash “/” at the end of the source directory. This will sync the content of the source directory into the target directory.
    • Now, add 100 files under /home/<your-user-name>/code/java-starter/src/main/java/ using:
      • touch code/java-starter/src/main/java/file{1..100}.java
        • What happens to the directory at /home/<your-user-name>/backup/java-starter/src/main/java/ ? List it?
      • Now run rsync again, rsync -r /home/<your-user-name>/code/ /home/<your-user-name>/backup/.
        • What happens to the directory at /home/<your-user-name>/backup/java-starter/src/main/java/ ? List it?
  4. File owner and group:

  • Use the command ls -l to find the owner and group owner of the ~/code/java-starter/NOTICE.txt file.
  • Use the command chown to change the user owner of the file ~/code/java-starter/NOTICE.txt into the root user. Now, open the file and edit it? What happens?
  • Use the command chown to change the user owner and group of the directory ~/code/java-starter into root and group wheel.
  • Use the command chown to change the user owner and group of the directory ~/code/java-starter and its subdirectories into root and group wheel.
  1. File access permissions:
  • Use the command ls to inspect the access permissions of the file ~/code/java-starter/LICENSE.txt. What can the following user categories do: user owner, group, all others?
  • Use the command chmod to change the permissions of the directory ~/code/java-starter and its subdirectories where you grant read and write permission to the owner user, none to group members, and none to others. What happens when you run ls on the ~/code/java-starter directory ? Why?
  • Undo the previous steps by changing the owner and group into your default user name and group name. Also, change the access permissions of the file ~/code/java-starter/LICENSE.txt to the earliest values you listed in the first step.
  1. Add an external storage and mount it:
  • Run ls -lh /dev/sd (then hit tab)
  • Plug in an external storage device.
  • Find the name of the external storage. Type ls -lh /dev/sd (then hit tab twice)
    • You may see the new device as sdb1
  • Create a mounting point mkdir -p /mnt/my-usb-disk
  • Mount it using the mount /dev/<sdb1> /mnt/my-usb-disk command.
  • Create a directory inside the mounted disk. mkdir /mnt/my-usb-disk/test/
  • Unmount it using umount /mnt/my-usb-disk. How?

Submission

Submit your answers with screenshots showing the commands you executed as a PDF file by the due date.

Lab submissions are due one hour before the next week’s lab begins.