Difference between revisions of "Tutorial2: Unix / Linux File Management"
(→Part 2: Viewing Directory Contents / Copying & Moving Directories) |
(→Part 2: Viewing Directory Contents / Copying & Moving Directories) |
||
Line 178: | Line 178: | ||
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR</span><br><br>What directories do you see?<br><br> | # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR</span><br><br>What directories do you see?<br><br> | ||
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR uli101</span><br><br>Note the differences between both of these commands.<br><br>You can copy and move entire directories (and their contents) to other directories.<br><br><table align="right"><tr valign="top"><td>[[Image:directory-structure-7.png|thumb|right|250px|Output of the '''tree''' command to confirm copy of '''uli101''' directory (and contents) to the '''xyz''' directory.]]</td><td>[[Image:directory-structure-8.png|thumb|right|300px|Output of the '''tree''' command to confirm movement of '''apc100 directory''' directory (and contents) to the '''xyz''' diredtory.]]</td></tr></table> | # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR uli101</span><br><br>Note the differences between both of these commands.<br><br>You can copy and move entire directories (and their contents) to other directories.<br><br><table align="right"><tr valign="top"><td>[[Image:directory-structure-7.png|thumb|right|250px|Output of the '''tree''' command to confirm copy of '''uli101''' directory (and contents) to the '''xyz''' directory.]]</td><td>[[Image:directory-structure-8.png|thumb|right|300px|Output of the '''tree''' command to confirm movement of '''apc100 directory''' directory (and contents) to the '''xyz''' diredtory.]]</td></tr></table> | ||
− | # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp -R uli101 xyz100</span><br><br>Note the differences between both of these commands.<br><br> | + | # Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cp -R uli101 xyz100</span><br><br>Note the differences between both of these commands.<br><br> |
#Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101 directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree</span><br><br> | #Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101 directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree</span><br><br> | ||
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv apc100 xyz100/uli101/tutorials</span><br><br>Note the differences between both of these commands.<br><br> | # Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv apc100 xyz100/uli101/tutorials</span><br><br>Note the differences between both of these commands.<br><br> |
Revision as of 14:40, 8 January 2020
Contents
UNIX / LINUX FILE MANAGEMENT CONCEPTS
Main Objectives of this Practice Tutorial
- Understand the Purpose of the Unix / Linux Directory Structure.
- List Common Directories that are Contained in a Typical Unix / Linux Filesystem
- Use Common Unix / Linux Commands to Perform Directory Management Tasks.
- Use Common Unix / Linux Commands to Perform Text File Management Tasks.
- Using Text Editors to Create and Manipulate Text Files
- Using Text Editors to Create and Manipulate Text Files
Tutorial Reference Material
Course Notes |
Linux Command/Shortcut Reference |
YouTube Videos | ||
Course Notes:Tutorials:
|
Directory and File Management Commands | Text File Management Commands | Text Editors
|
Brauer Instructional Videos: |
Unix / Linux Directories
The Unix/Linux file system is hierarchical, similar to other operating systems such as
Windows, Mac OSX, etc.
Files are organized in directories. Directories may contain sub-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".
Learning how to issue Linux commands for navigating the Linux filesystem and manipulating directory and files are essential skills for Linux users and administrators.
In the Linux (Unix) OS, the "root directory" / is the starting directory, and other "child directories", "grandchild directories", etc. are created
The hierarchical structure resembles an "upside- down tree". There is actually a command called tree that can display a "tree diagram"!
Directory Pathnames
A path, the general form of the name of a file or directory, specifies a unique location in a file system. A path 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:
Please take a few moments to review these pathnames.
Directory Pathname | Purpose |
---|---|
/ | Root directory (ancestor to all directories) |
/home | Used to store users’ home directories |
/home/username | A Particular User's Home Directory |
/bin | Common system binaries (commands) |
/usr/bin | Common utilities (commands) for users. |
/usr/sbin | Common utilities for system administration |
/etc | System administration files (eg. passwd) |
/var | Dynamic files (log and mail files) |
/tmp , /var/tmp | Temporary files for programs |
/dev | Device driver files (terminals, printers, etc.) |
Every user when receiving an account has a “home” directory created (/home/userid). This is where the user keep subdirectories and personal files.
We will now learn to create and manage subdirectories within your own home directory.
INVESTIGATION 1: MANAGING DIRECTORIES
In this investigation, you will learn to manage directories including their creation, navigation, listing contents and removal.
Directory File Naming Rules
Before learning to create directories, it is important to understand what represents an appropriate directory filename.
Some of the basic file-naming rules are:
- 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 later
- 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
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 would like to create a directory structure within your home directory as displayed in the diagram on the right aide.
You should note from the previous section that the root directory is the
"starting point" in the Matrix file system, that the home directory is used to store all Matrix user accounts by their userid, and that your userid contained within the home directory represents YOUR home directory where you can create files (both directory files, text files, etc).
Perform the Following Steps:
- Login your matrix account.
- Issue a command to confirm you are located in your home directory. You should know how to do this from the previous tutorial.
- Issue the following Linux command: mkdir uli101
NOTE: You should always confirm that you have created a directory. This can be done by issuing the ls command. - Issue the following Linux command: ls uli101
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. - Issue the following Linux command: ls -d uli101
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. - Issue the following Linux command:ls -d -l uli101
How can you confirm from the output of this command that the file uli101 is a directory? - Issue the following Linux command:ls -ld uli101
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? - Issue the following Linux command to create the apc100 and xyz100 directories:mkdir apc100 xyz100
NOTE: You should now notice that you can create multiple directories by issuing the mkdir with multiple arguments. - Issue the following Linux command to confirm that those directories have been created: ls -ld apc100 xyz100
- We will now create the subdirectories that are contained in the uli101 directory. Issue the following Linux command to move to the uli101 directory: cd uli101
- Issue a command to confirm that your current location is in the uli101 directory. You should know how to issue this command from a previous tutorial.
- Issue the following Linux command to create the directories called notes, tutorials and examples: mkdir notes tutorials examples
- Issue a Linux command to confirm that those directories have been created. You should know how to do this...
- This technique is considered to be inefficient... There are options and ways to create the same directory structure without using the cd command and only issue a single Linux command.
Using this inefficient method tends to show that you are a novice or "newbie", and you may lose marks if you issue multiple Linux commands to performed tasks that can be performed using only a single Linux command.
Although we will teach you how to remove directories and their contents in a future section, let's change back to your home directory and issue a command to remove the directory structure that you just created so you can learn a more efficient method of creating the same directory structure. - Issue Linux commands to move to your home directory and confirm that your current directory is your home directory. You should know how to do this...
- Issue the following Linux command to remove all of the directories that you have created:
rm -r uli101 apc100 xyz100
NOTE: You will learn how to safely remove directories and their contents in another section. - Issue the following single Linux command to create the entire directory structure:
mkdir -p uli101/notes uli101/tutorials uli101/examples acp100 xyz100
NOTE: The -p option allows "parent" directories to be automatically created first to then create their subdirectories. - Issue the following Linux command to confirm that all of the directories have been created:
ls -ld uli101 uli101/notes uli101/tutorials uli101/examples apc100 xyz100
Downloading & Running a Shell Script to Check your Work
Although you are being asked to create the directory structure, this author has no idea that you have performed it correctly:
- Not creating directories or some directories.
- You are making mistakes in directory name syntax (eg. spelling or mixing up character case)
- Not creating subdirectories within specified directories.
To check that you haven't made mistakes so you won't encounter problems in the next section 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.
Perform the Following Steps:
- Make certain that your current directory is your home directory.
- Issue the following Linux command to download a shell script to your home directory that will check your work:
wget https://matrix.senecacollege.ca/~murray.saul/uli101/week1-check-1 - Run the checking script to check your work by issuing the following:
bash week1-check-1
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 contents contained in directories, as well as copy and move directories.
Perform the Following Steps:
- Issue the following Linux command: tree
NOTE: You should see the directory structure that you created in the previous section. 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"). - Issue the following Linux command: ls -lR
What directories do you see? - Issue the following Linux command: ls -lR uli101
Note the differences between both of these commands.
You can copy and move entire directories (and their contents) to other directories. - Issue the following Linux command:
cp -R uli101 xyz100
Note the differences between both of these commands. - Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101 directory:
tree - Issue the following Linux command:
mv apc100 xyz100/uli101/tutorials
Note the differences between both of these commands. - Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101 directory:
tree
Let's download and run a checking script to see if you moved and copied the directories correctly. - Make certain that your current directory is your home directory.
- Issue the following Linux command to download a shell script to your home directory that will check your work:
wget https://matrix.senecacollege.ca/~murray.saul/uli101/week1-check-2 - Run the checking script to check your work by issuing the following:
bash week1-check-2
Part 3: Removing Directories
In a previous section, you learned to remove empty and non-empty directories to learn how to create a directory structure more efficiently.
In this section, we will learn how to safely remove directories and their contents.
Perform the Following Steps:
- Confirm that you are located in your home directory.
The rmdir command is used to remove empty directories. - Issue the following Linux command to remove the empty directory called uli101/tutorials: rmdir uli101/tutorials
- Issue the following Linux command to remove the empty directory called uli101: rmdir uli101
NOTE: You should get an error message, since the uli101 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. - Issue the following Linux command to remove the uli101 directory and its contents: rm -r uli101
- Issue the tree command to confirm that the uli101 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. - Issue the following Linux command (entering "y" when prompted): rm -ri xyz100
NOTE: You should have removed all directories that you have created.
Let's download and run a checking script to see if you have removed all of the directories that you earlier created. - Make certain that your current directory is your home directory.
- Issue the following Linux command to download a shell script to your home directory that will check your work:
wget URL - Run the checking script to check your work by issuing the following:
bash filename
INVESTIGATION 2: MANAGING TEXT FILES
x
Part 1: Creating Text Files
x
Perform the Following Steps:
- x
Part 2: Viewing / Manipulating Text Files
x
Perform the Following Steps:
- x
Part 3: Copying / Moving Text Files
x
Perform the Following Steps:
- x
Part 4: Removing Text Files
x
Perform the Following Steps:
- x
LINUX PRACTICE QUESTIONS
The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.
Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:
https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week2_practice.docx
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
Review Questions:
- x
- x
- x
- x
- x
- x
- x
- x
- Create a table listing each Linux command, useful options that were mentioned in the online assignment #1 and command purpose for the following Linux commands: '