Tutorial 2 - Unix / Linux File Management

From CDOT Wiki
Revision as of 17:30, 30 August 2021 by Jason Michael Carman (talk | contribs) (Part 2: Viewing Directory Contents / Copying & Moving Directories)
Jump to: navigation, search

UNIX / LINUX FILE MANAGEMENT CONCEPTS


Main Objectives of this Practice Tutorial

  • Understand the purpose of directories and directory pathnames.
  • Use common Unix / Linux commands to perform directory management tasks.
  • Use Text Editors to create and modify text files
  • Use Common Unix / Linux commands to manage and manipulate text files.



Tutorial Reference Material

Course Notes
Linux Command/Shortcut Reference
Slides:Tutorials:


File Management Text Editors /
File Content

KEY CONCEPTS

Unix / Linux Directories

In Unix / Linux (as opposed to MS Windows), there are no drive letters (such as C:, or D:).

All files and directories appear under a single ancestor directory called "the root directory".

To better organize files (eg. text, images, documents, spreadsheets, programs) within your Matrix account, they should be stored in directories. To further organize many files, directories
may contain sub-directories.

The Unix/Linux file system is hierarchical, like other operating systems such as Windows, Mac OSX, etc. In Unix / Linux (as opposed to MS Windows), there are no drive letters (such as C:, or D:).
All files and directories appear under a single ancestor directory called the "root directory".

Learning how to issue Linux commands for navigating and manipulating directory and files within the the Linux filesystem are essential skills for Linux users and Linux system administrators (i.e. sysadmins).

In the Linux (Unix) OS, the "root directory" / is the starting directory, and other "child directories", "grandchild directories", etc. can be created as required. The hierarchical structure resembles an "upside-down tree". There is actually a command called tree that displays a "directory tree diagram"!

Directory Pathnames

A pathname points to a file system location by following the directory tree hierarchy.

A pathname is used to specify a route to a file within the file system.

A pathname points to a file system location by following the directory tree hierarchy expressed in a string of characters in which path components, separated by a delimiting character, represent each directory. The delimiting character is most commonly the slash ("/").
Reference: https://en.wikipedia.org/wiki/Path_(computing)

The following table displays and defines commonly used directories
(listed by directory pathname) for for ALL Unix / Linux Filesystems:



Common directory pathnames
for for ALL Unix / Linux Filesystems.
Directory PathnamePurpose
/Root directory (ancestor to all directories)
/homeUsed to store users’ home directories
/home/usernameA specific User's Home Directory
/binCommon system binaries (commands)
/usr/binCommon utilities (commands) for users.
/usr/sbinCommon utilities for system administration
/etcSystem administration files (eg. passwd)
/varDynamic files (log and mail files)
/tmp , /var/tmpTemporary files for programs
/devDevice driver files (terminals, printers, etc.)


When you log into your Matrix account, you are automatically directed to your home directory.
This directory is where the user can store files, and create subdirectories to organize their files.

INVESTIGATION 1: CREATING & MANAGING DIRECTORIES

ATTENTION: The due date for successfully completing this tutorial (i.e. tutorial 2) is by Friday, November 5th by midnight (Week 8).

In this investigation, you will learn how to create, navigate, list directory contents and remove directories in your Matrix account.


Directory File Naming Rules

Before you learn how to create directories, it is important to understand what represents an appropriate directory filename.

Listed below are some common file-naming rules:

  • Unix/Linux characters are case sensitive. It is recommended to be consistent (e.g. use all lowercase letters)
  • Adopt a consistent directory naming scheme (this will help you to navigate within your directory structure)
  • Make your directory names meaningful
  • Avoid non-alphanumeric characters, as they may have a special meaning to the system that will make your work more difficult when changing to directories, etc.
  • Avoid using spaces for directory names (consider periods, hyphens, and underscores instead)


Part 1: Creating Directories

Tree Diagram of Directory Structure to Create in your Home Directory (displayed in blue text).

Creating subdirectories within your home directory makes it more efficient to
save and access files on your Linux server.

A comparison would be rooms in a house. If there were no rooms, just one large room in a 3,000 square foot house, it would be "messy" and difficult to locate items. Each room in a house is used to for a
specific purpose to be more productive to perform a task such as a kitchen, bedroom, bathroom, etc.

You will be creating the following directory structure within your home directory
(refer to diagram on the right side).


Perform the Following Steps:

NOTE: For several of the following commands, you will be using your Seneca username to replace the text:"your-seneca-id" in a pathname.

For example, if your Seneca user ID was mkhan then you would display
the pathname /home/your-seneca-id as: /home/mkhan

  1. Login to your matrix account (you should know how to do this from performing Tutorial 1 INVESTIGATION 1).

  2. Issue a command to confirm that you are located in your home directory
    (you should know how to do this from performing Tutorial 1 INVESTIGATION 2

  3. Issue the following Linux command: mkdir /home/your-seneca-id/osl640xx
    (remember: use your Seneca username instead of "your-seneca-id")

    NOTE: You should always confirm that you have created a directory.
    This can be done by issuing the ls command.

    Creating and Confirming the Creation of a Directory.
  4. Issue the following Linux command: ls /home/your-seneca-id/osl640xx

    There are no contents that are contained in this newly-created directory; therefore, no contents appear. A useful option -d can be used to confirm that the actual directory has been created as opposed to viewing the contents of the directory.

  5. Issue the following Linux command: ls -d /home/your-seneca-id/osl640xx

    You should now see just the directory listed. You can also combine the -d and -l options to provide more detail regarding the newly-created directory.

  6. Issue the following Linux command: ls -d -l /home/your-seneca-id/osl640xx

    How can you confirm from the output of this command that the file uli101xx is a directory?

  7. Issue the following Linux command: ls -ld /home/your-seneca-id/osl640xx

    Is the output from this command the same was the output from the previous command?
    If so, what does this say about how to use multiple options for Linux commands?

  8. Issue the following Linux command to create the osm620xx and nwk680xx directories:
    mkdir /home/your-seneca-id/osm620xx /home/your-seneca-id/nwk680xx

    NOTE: You can create multiple directories by issuing the mkdir command with multiple arguments.

  9. Issue the following Linux command to confirm that those directories have been created:
    ls -ld /home/your-seneca-id/osm620xx /home/your-seneca-id/nwk680xx

    Using a FULL pathname starting from the root directory (i.e. /) requires is a LOT of typing!.
    Since we are already located in our home directory, we don't have to start from
    the root directory called a "relative" pathname.


  10. We will now create the subdirectories that are contained in the osl640xx directory.
    Issue the following Linux command to move to the osl640xx directory using a relative pathname:
    cd osl640xx

  11. Issue a Linux command to confirm that your current location is in the osl640xx directory.
    You should know how to issue this command from a previous tutorial.

  12. Issue the following Linux command to create the directories called notes, tutorials and samples:
    mkdir notes tutorials samples

  13. Issue a Linux command to confirm that those directories have been created
    (you should already know how to do this).

    There are better ways to create the same directory structure without using the cd command.

    Issuing the cd command prior to issuing other commands indicates to others that you are a novice or "newbie".
    You can actually lose marks on quizzes or tests if you issue multiple Linux commands to performed tasks that can be performed
    using a single Linux command with pathnames.

  14. Issue the following Linux command to return to your home directory: cd

  15. Issue a Linux command to confirm that you are now located in your home directory.

    Although we will teach you how to remove directories and their contents in another investigation,
    let's remove the created directory structure' and recreate the same directory structure
    using just one command using pathnames.

  16. Issue the following Linux command to remove all of the directories that you have created:
    rm -r osl640xx osm620xx nwk680xx

  17. Issue a Linux command to confirm that the osl640xx, osm620xx and nwk680xx that been removed.

  18. Issue the following single Linux command to create the entire directory structure:
    <span style="color:blue;font-weight:bold;font-family:courier;"mkdir -p osl640xx/notes osl640xx/tutorials osl640xx/samples osm620xx nwk680xx</span>

    NOTE: The -p option allows "parent" directories to be automatically created first to then create their subdirectories.

  19. Issue the following Linux command to confirm that all of the directories have been created:
    ls -ld osl640xx/ osl640xx/notes osl640xx/tutorials osl640xx/samples osm620xx nwk680xx



Running a Shell Script to Check your Work

Although you are being asked to create the directory structure, you might have made some mistakes:
For Example:

  • Forgetting to create a directory.
  • Making syntax errors (eg. spelling or mixing up character case)
  • Not creating subdirectories within specified directories.

To check for mistakes, a shell script has been created to check your work. If the checking shell script detects an error, then it will provide feedback and offer constructive feedback on how to fix that problem so you can re-run the checking shell scripts until your work is correct. Errors (WARNINGS) are indicated in as WARNING and should be corrected. Steps correctly done are indicated as OK


Perform the Following Steps:

  1. Make certain that your current directory is your home directory.

  2. Issue the following Linux command to run a checking script:
    ~osl640/week2-check-1

  3. If you encounter errors, then view the feedback to make corrections, and then re-run the checking script. If you receive a congratulation message that there are no errors, then proceed to the next part.


Part 2: Viewing Directory Contents / Copying & Moving Directories

Now that you have learned how to efficiently create your directory structure, you will now learn how to issue Linux commands
to view directory contents as well as copy and move directories.


Perform the Following Steps:

Output of the tree command to display directory structure.
Output of the ls -R command to display directory structure.
  1. Issue the following Linux command: tree

    NOTE: You should see the directory structure that you created in the previous investigation. You can also issue the tree command using a directory pathname to display the directory structure for a specific scope.

    You can also use the -R option for the ls command to display all directories and subdirectories for a specified directory path (referred to as a recursive directory listing).

  2. Issue the following Linux command: ls -lR

    What directories do you see?

  3. Issue the following Linux command: ls -lR osl640xx

    Note the differences between both of these commands (i.e. using and not using a pathname).

    You can copy and move entire directories (and their contents) to other directories.

  1. Issue the following Linux command:
    cp -R osl640xx nwk680xx

  2. Issue the following Linux command to display the directory structure of your home directory to confirm you copied the osl640xx directory:
    tree





  3. Issue the following Linux command:
    mv osm620xx nwk680xx/osl640xx/tutorials/osm620xx

  4. Issue the following Linux command to display the directory structure of your home directory to confirm you moved the acp100xx directory:
    tree

    Let's run a checking script to see if you moved and copied the directories correctly.

  5. Make certain that your current directory is your home directory.

  6. Issue the following Linux command run checking script:
    ~osl640/week2-check-2

  7. If you encounter errors. make corrections and re-run the checking script until you receive a congratulations message, and proceed to the next investigation.

Part 3: Removing Directories

In this investigation, we will learn how to safely remove directories and their contents.


Perform the Following Steps:

  1. Confirm that you are located in your home directory.

    The rmdir command is used to remove empty directories.

  2. Issue the following Linux command to remove the empty directory called tutorials: rmdir uli101xx/tutorials

  3. Issue a command to confirm that you removed the tutorials directory.

  4. Issue the following Linux command to remove the empty directory called uli101xx: rmdir uli101xx

    NOTE: You should get an error message, since the uli101xx directory is NOT empty.

    To remove non-empty directories, you can use the rm -r command.
    The -r option stands for recursive, which can travel down the directory paths and their contents.

  5. Issue the following Linux command to remove the uli101xx directory and its contents: rm -r uli101xx

  6. Issue the tree command to confirm that the uli101xx directory (contained in your home directory) and its contents have been removed.

    NOTE: To safely remove non-empty directories, you can add the -i option which will prompt the user if they wish to remove contents as it your travel recursively down a directory to remove directories and their contents.

  7. Issue the following Linux command (entering "y" when prompted): rm -ri xyz100xx

    NOTE: You should have removed all directories that you have created.

    Let's run a checking script to confirm that you have correctly removed all of those directories.

  8. Issue the tree command to confirm that all of those recently created directories have been removed.

  9. Issue the following Linux command to run a checking script to confirm removal of those directories:
    ~uli101/week2-check-3

  10. If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message, and proceed to the next INVESTIGATION.

INVESTIGATION 2: MANAGING TEXT FILES

LINUX PRACTICE QUESTIONS