OPS145 Lab 2: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Admon/important|DON'T USE THIS PAGE. | It is currently just a backup.}}
== Multi-user systems ==
=UNIX / LINUX FILE MANAGEMENT CONCEPTS=
Linux was intended from its beginnings to be a multi-user system. A multi-user system allows multiple people to use it at the same time. That concept is hard to understand if you think of a computer as having one screen, one keyboard, and one mouse.
===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===
But originally systems like '''Unix''' (the Linux predecessor) were used as workstations by multiple people at once. They got around the problem of sharing one monitor and one keyboard by using dumb terminals. A '''dumb terminal''' might look like a computer but it isn't. It's just a screen plus a keyboard plus a serial port connection (like a slow network connection) to the actual computer (sometimes called a '''mainframe''').


{|width="100%" cellspacing="0" cellpadding="10"
Other systems like Microsoft '''DOS''' (which eventually got a graphical interface and was then called '''Windows''') had no such capabilities, and were much simpler. That made them the perfect choice for this new thing called a Personal Computer ('''PC'''). You didn't need much sysadmin training to put DOS on a PC.


|- valign="top"
For decades the mainframe was powerful enough to serve the needs of all the employees, but cheap enough that it wasn't going to get replaced with personal computers for each employee.


|colspan="1" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;"|Course Notes<br>
Eventually PCs got much cheaper, and since it was no longer financially viable to stick to mainframes: Microsoft took over the computer world with its DOS and later Windows operating systems.


|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command/Shortcut Reference<br>
= Why terminals still exist =
It turns out that when you create an operating system intended to be used by one user on their personal computer: you don't consider security or even multi-tasking performance as priorities.


|colspan="1" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|YouTube Videos<br>
In the 1980s and 1990s the entire world was plagued by viruses, blue screens of death, and abysmal server performance because the demand for IT increased exponentially but Unix was expensive, Unix administrators were hard to find, so Windows was the only option for many.


|- valign="top" style="padding-left:15px;"
Luckily Linus Torvalds wrote an operating system kernel mostly compatible with Unix, and together with hundreds and thousands of other developers a new operating system was born which did not suffer from the MS DOS legacy problems: '''Linux'''.


|colspan="1" |'''Slides:'''<ul><li>Week 2 Lecture 1 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-2.1.pdf PDF] | [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-2.1.pptx PPTX]</li><li>Week 2 Lecture 2 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-2.2.pdf PDF] | [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-2.2.pptx PPTX]</li></ul>'''Tutorials:'''<ul><li>[http://www.cheat-sheets.org/saved-copy/Nano_Cheat_Sheet.pdf Nano Reference Sheet (PDF)]</li><li>[https://www.smashingmagazine.com/2010/05/vi-editor-linux-terminal-cheat-sheet-pdf Vi Reference Sheet (PDF)]</li></ul>
Dumb terminals have almost completely dissapeared. They're now only used by very large companies who like to pay IBM lots of money for basic IT services. I've recently seen a green screen being used by a bank teller, and smiled.


The mainframes of the 1970s have been replaced by PC-compatible servers and server farms. Two types of terminals are still used pervasively:


|  style="padding-left:15px;" |'''File Management:'''
* For regular users: a web browser is just a pretty version of a dumb terminal. All the content lives on the server, and all the actions performed by the website visitor are performed on the server.
*[http://man7.org/linux/man-pages/man1/pwd.1.html pwd]<br>
* The server administrators use almost identical terminals now as were used 50 years ago, except they're '''software terminals instead of hardware terminals'''. A graphical interface, while inviting with its apparent ease of use, is simply not efficient enough to allow complex administration tasks on remote servers. You are going to learn to use this sort of terminal in OPS145 and the rest of the OPS stream.
*[http://man7.org/linux/man-pages/man1/cd.1p.html cd]<br>
*[http://man7.org/linux/man-pages/man1/ls.1p.html ls]<br>
*[https://www.systutorials.com/docs/linux/man/1-tree/ tree]<br>
*[http://man7.org/linux/man-pages/man1/mkdir.1.html mkdir]<br>
*[http://man7.org/linux/man-pages/man1/rmdir.1.html rmdir]<br>
*[http://man7.org/linux/man-pages/man1/cp.1.html cp]<br>
*[http://man7.org/linux/man-pages/man1/mv.1p.html mv]<br>
*[http://man7.org/linux/man-pages/man1/rm.1.html rm] , [http://man7.org/linux/man-pages/man1/rm.1.html rm -r]<br>


|  style="padding-left:15px;"|'''Text Editors /<br>File Content:'''
= What can you do in a terminal =
*[http://man7.org/linux/man-pages/man1/vi.1p.html vi] , *[https://linux.die.net/man/1/nano nano]<br>
If you're a server administrator: just about everything that you can do can be done in a terminal. For example:
*[http://man7.org/linux/man-pages/man1/cat.1.html cat]<br>
*[http://man7.org/linux/man-pages/man1/more.1.html more] , [http://man7.org/linux/man-pages/man1/less.1.html less]<br>
* [http://man7.org/linux/man-pages/man1/head.1.html head] , [http://man7.org/linux/man-pages/man1/tail.1.html tail]
*[http://man7.org/linux/man-pages/man1/touch.1.html touch]<br>
*[http://man7.org/linux/man-pages/man1/file.1.html file]<br>
*[http://man7.org/linux/man-pages/man1/find.1.html find]<br>
*[http://man7.org/linux/man-pages/man1/diff.1.html diff]<br>
*[http://man7.org/linux/man-pages/man1/sort.1.html sort]<br>
*[http://man7.org/linux/man-pages/man1/uniq.1.html uniq]<br>
*[http://linuxcommand.org/lc3_man_pages/grep1.html grep]<br>


|colspan="1" style="padding-left:15px;" width="30%"|'''Brauer Instructional Videos:'''<ul><li>[https://www.youtube.com/watch?v=UfAFAipked0&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=3 Basic File Actions<br>(touch, ls -l, cp, cp -r, mv, mkdir, mkdir -p, rm, rmdir, rm -r)]</ul>
* Copying and moving files and directories.
* Creating users and controlling their access privileges.
* Configuring and controlling services (e.g. a web server).
* Monitoring server activity.
* Installing and uninstalling software.
* Configuring networking.
* Configuring other hardware (like hard drives).
* Starting and stopping machines on demand.
* Creating, deploying, and monitoring all sorts of automation.
Terminals are probably completely new to you, and though they're older than your grandparents: don't imagine that they're ancient garbage. Most of the exciting computer things created in your lifetime were built and are managed in terminals. Once you learn the basics: you'll impress yourself with what you can accomplish by typing commands rather than opening windows and clicking buttons. That's what OPS145 is for.


|}
= ls, pwd, cd =
One of the simplest things you can do is look at the contents of a directory (a.k.a. a folder).


= KEY CONCEPTS=
* First open a graphical file manager in your ops145 VM.
* In the '''View''' menu change the '''Sidebar''' type to '''Tree'''.
* Also in the '''View''' menu change the '''Toolbar''' type to '''Location Entry'''.


===Unix / Linux Directories===
This will encourage you to better understand paths over the long term. Note that it appears you have 8 empty subdirectories in your home directory.


[[Image:directory-structure-1.png|thumb|right|350px|In Unix / Linux (as opposed to MS Windows), there are no drive letters (such as '''C:''', or '''D:''').<br><br> All files and directories appear under a single ancestor directory called "the '''root''' directory".]]
* Open the terminal application and run the '''ls''' command. To run a command you type it at the prompt and press enter.
To better '''organize''' files (eg. text, images, documents, spreadsheets, programs) within your Matrix account, they should be stored in '''directories'''. To further organize <u>many</u> files, directories<br> may contain '''sub-directories'''.  


The Unix / Linux file system is '''hierarchical''', like other operating systems such as '''Windows''', '''macOS''', etc. In Unix / Linux (as opposed to Windows), there are no drive letters (such as '''C:''', or '''D:''').
The ls command (as most other commands) produces some text output and that output is printed in the terminal window where you executed the command.


All files and directories appear under a single ancestor directory called the "'''root directory'''".
* Run the '''clear''' command. This is helpful at times when there is too much old output in the terminal window which you don't care about and is distracting you. This doesn't undo the commands you ran previously, it simply erases their output from the screen.
* Try running ls using different capitalization, for example '''Ls''', '''LS''', or '''lS'''. Try to run some other made up commands and note that the error is the same: '''command not found'''.


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'').
[[File:BadLS.png|center]]


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''".
* Open the "Text Editor" graphical application. This works with plain text files.
* Write some text in it and save the file as '''first.txt''' in your Documents directory.
* Close the text editor, and open your first.txt again by finding it in your graphical file manager.
* Run ls in your terminal and note that the Documents directory is there, but the first.txt file does not show up. That's because by default the ls command shows the contents of the Present Working Directory (PWD), your PWD is /home/yourusername, but first.txt is in the /home/yourusername/Documents/ directory.
* Change your PWD by running '''cd Documents'''.
* And run ls again. You should now see first.txt. But it no longer shows Desktop, Downloads, etc. because those are subdirectories of /home/yourusername/ and your PWD is /home/yourusername/Documents/
* Run the '''pwd''' command. That simply shows you the PWD, which you can typically see anyway in the terminal prompt.


There is actually a command called <code>tree</code>  that displays a "'''directory tree diagram'''"!
== ls -l ==
You can simply run a command by typing in the name of the command and waiting for it to execute its default behaviour. But you can also change that default behaviour, by following the command with one or more arguments (a.k.a. options, a.k.a. parameters, a.k.a. switches). Each command has its own set of possible arguments. Sometimes they're similar between commands, most of the time they're not.


=== Directory Pathnames ===
For the '''ls''' command one such argument is '''-l''' (minus lower case L). This instructs '''ls''' to print extra details about each file and directory.
[[Image:path-name.png|thumb|right|180px|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)
* Run '''ls -l'''


The following table displays and defines '''commonly used directories''' (listed by directory pathname) for for ALL Unix / Linux Filesystems:
For now we can ignore most of that output, but do try to find the size of the file. It's the number just before the modification date. By default the size is in bytes.
[[File:Ls-l.png|center|812x812px]]


[[Image:directory-structure-2.png|thumb|right|500px|'''Common directory pathnames'''<br>for for ALL Unix / Linux Filesystems.]]<table cellpadding="5"><tr><th style="border-bottom: 1px solid black;">Directory Pathname</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr><td>'''/'''</td><td>Root directory (ancestor to all directories)</td></tr><tr><td>'''/home'''</td><td>Used to store users’ home directories
Since the text is written in english: each character is an ASCII character, which uses one byte of storage. Spaces and newlines (Enters) are also one-byte characters.
</td></tr><tr><td>'''/home/username'''</td><td>A '''<u>specific</u>''' User's Home Directory</td></tr><tr><td>'''/bin'''</td><td>Common system binaries (commands) </td></tr><tr><td>'''/usr/bin'''</td><td>Common utilities (commands) for users. </td></tr><tr><td>'''/usr/sbin'''</td><td>Common utilities for system administration  </td></tr><tr><td>'''/etc'''</td><td>System administration files (eg. passwd)</td></tr><tr><td>'''/var'''</td><td>Dynamic files (log and mail files)</td></tr><tr><td>'''/tmp , /var/tmp'''</td><td>Temporary files for programs</td></tr><tr><td>'''/dev'''</td><td>Device driver files (terminals, printers, etc.)
</td></tr></table>


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.
* Count the number of characters in your text file and confirm that it's the same as the size of the file.


=INVESTIGATION 1: CREATING &amp; MANAGING DIRECTORIES=
= cat =
{{Admon/caution|This weekly tutorial must be completed by Friday in Week 3 by 11:59pm to obtain a grade of 2% towards this course.|}}
The default behaviour of the '''cat''' command is to read the contents of a file and dump them out on the terminal. The terminal can only display text. Other types of data will likely show up as garbage.


In this investigation, you will learn how to '''create''', '''navigate''', '''list directory contents''' and '''remove''' directories in your Matrix account.
* Run '''cat first.txt''' to see the contents of first.txt The name of the command is cat and the first argument is first.txt


'''Directory File Naming Rules'''
[[File:CatFirstTxt.png|center|812x812px]]


Before you learn how to create directories, it is important to understand what represents an appropriate '''directory filename'''.
Commands and arguments are separated by spaces. The number of spaces doesn't matter.


Listed below are some common '''file-naming rules''':
* Try to run the '''ls''' and '''cat''' commands you ran earlier but add more spaces before the command, between the command and the argument, and at the end.


* Unix/Linux characters are '''case sensitive'''. It is recommended to be consistent (e.g. use all lowercase letters)
The '''ls -l''' command shows you information ''about'' the file. The '''cat''' command shows you the ''contents'' of the file.
* 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 ===
* Create a new text file in the graphical editor and save it with a name which has a space. Then try to use cat to see its contents:


[[Image:directory-structure-3.png|thumb|right|350px|'''Tree Diagram''' of Directory Structure to Create in your '''Home''' Directory (displayed in blue text).]]
[[File:catFileWithSpace.png|center|812x812px]]
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.
"'''No such file or directory'''" is one of the most common errors. Usually it's obvious what it means, but often (as in this case) you have to think a little.


You will be creating the following '''directory structure''' within your home directory (refer to diagram on the right side).
Since the command arguments are separated by spaces: the '''cat''' command has no way of guessing that '''second''' and '''file.txt''' are one filename, it assumes they are two separate files. Later in the course you'll learn to deal with spaces and other special characters in filenames.


'''Perform the Following Steps:'''
* Try to use '''cat''' to see the contents of '''First.txt''' (note the capital first letter). It won't work since First.txt and first.txt are as different from each other as first.txt and hirst.txt.
* Use the graphical text editor to create another text file, and save it in the Documents directory with the name '''First.txt''', then repeat your last '''cat''' command:
[[File:CatfirstFirst.png|center|812x812px]]


'''NOTE:''' For several of the following commands, you will be using '''<u>your</u> Seneca username''' to replace the text:"'''your-seneca-id'''" in a pathname.
* Note also that the sizes of the files are different. '''First.txt''' has fewer characters, therefore it's smaller.


For example, if your Seneca user ID was '''mkhan''' then you would display the pathname ''/home/your-seneca-id'' as: '''/home/mkhan'''
= ls -l -h =
First get some sample files to use for the rest of this lab.


# '''Login''' to your matrix account (you should know how to do this from performing [https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_1:_ACCESSING_YOUR_MATRIX_LINUX_ACCOUNT Tutorial 1 INVESTIGATION 1].<br><br>
*Download [http://ops345.ca/ops145/SampleFiles.tar.xz '''SampleFiles.tar.xz'''] (it's like a zip file) into your Downloads directory.
# Issue a command to '''confirm''' that you are located in your home directory<br>(you should know how to do this from performing [https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_2:_USING_THE_LINUX_SHELL Tutorial 1 INVESTIGATION 2]<br><br>
* Double-click on it in the graphical file manager, and extract its contents also into the Downloads directory. You can either use drag-and drop or click the appropriate buttons. Later in the course you'll learn to download and extract tarballs using only the command line.
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">mkdir /home/your-seneca-id/uli101xx</span><br>(remember: use <u>your</u> Seneca username instead of "your-seneca-id")<br><br>'''NOTE:''' You should always confirm that you have created a directory.<br>This can be done by issuing the '''ls''' command.<br><br>[[Image:directory-structure-4.png|thumb|right|350px|'''Creating''' and '''Confirming''' the Creation of a Directory.]]
* Open a new terminal window. You can close the original one if it confuses you.
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls /home/your-seneca-id/uli101xx</span><br><br>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 <u>directory</u> has been created as opposed to viewing the contents of the directory.<br><br>
* Use '''cd''' to change your PWD to the Downloads directory, then to the SampleFiles directory, and confirm using '''ls -l''' that your extracted files are there.
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -d /home/your-seneca-id/uli101xx</span><br><br>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.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -d -l /home/your-seneca-id/uli101xx</span><br><br>How can you confirm from the output of this command that the file uli101xx is a directory?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -ld /home/your-seneca-id/uli101xx</span><br><br>Is the output from this command the same was the output from the previous command?<br>If so, what does this say about how to use multiple options for Linux commands?<br><br>
# Issue the following Linux command to create the '''acp100xx''' and '''xyz100xx''' directories:<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx</span><br><br>'''NOTE:''' You can create multiple directories by issuing the '''mkdir''' command with <u>multiple arguments</u>.<br><br>
# Issue the following Linux command to confirm that those directories have been created:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -ld /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx</span><br><br>'''Using a FULL pathname starting from the root directory (i.e. /) requires is a LOT of typing!.<br>Since we are already located in our home directory, we don't have to start from<br>the root directory. This is called a "relative" pathname.'''<br><br>
# We will now create the subdirectories that are contained in the '''uli101xx''' directory.<br>Issue the following Linux command to move to the '''uli101xx''' directory using a '''relative''' pathname:<br><span style="color:blue;font-weight:bold;font-family:courier;">cd uli101xx</span><br><br>
# Issue a Linux command to confirm that your current location is in the ''uli101xx'' directory.<br>You should know how to issue this command from a previous tutorial.<br><br>
# Issue the following Linux command to create the directories called '''notes''', '''tutorials''' and '''samples''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir notes tutorials samples</span><br><br>
# Issue a Linux command to confirm that those directories have been created<br>(you should already know how to do this).<br><br>There are better ways to create the same directory structure without using the '''cd''' command.<br><br>Issuing the '''cd''' command '''<u>prior</u>''' to issuing other commands indicates to others that you are a novice or "'''newbie'''".<br>You can actually '''lose marks''' on quizzes or tests if you issue multiple Linux commands to performed tasks that can be performed<br>using a single Linux command with '''pathnames'''.<br><br>
# Issue the following Linux command to return to your home directory: <span style="color:blue;font-weight:bold;font-family:courier;">cd</span><br><br>
# Issue a Linux command to confirm that you are now located in your home directory.<br><br>Although we will teach you how to '''remove''' directories and their contents in another investigation,<br>let's remove the created directory structure' and '''recreate''' the same directory structure<br>using just <u>one</u> command using pathnames.<br><br>
# Issue the following Linux command to remove all of the directories that you have created:<br><span style="color:blue;font-weight:bold;font-family:courier;">rm -r uli101xx acp100xx xyz100xx</span><br><br>
# Issue a Linux command to confirm that the '''uli101xx''', '''acp100xx''' and '''xyz100xx''' directories have been removed.<br><br>
# Issue the following single Linux command to create the entire directory structure:<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir -p uli101xx/notes uli101xx/tutorials uli101xx/samples acp100xx xyz100xx</span><br><br>'''NOTE:''' The '''-p''' option allows "parent" directories to be automatically created first to then create their subdirectories.<br><br>
# Issue the following Linux command to confirm that all of the directories have been created:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -ld uli101xx/ uli101xx/notes uli101xx/tutorials uli101xx/samples acp100xx xyz100xx</span>
<br><br>
'''Running a Shell Script to Check your Work'''


Although you are being asked to create the directory structure, you might have made some mistakes:<br>For Example:<br>
Larger file sizes are harder to understand if they are expressed in bytes. You can get the '''ls''' command to show sizes in human-readable format by adding another argument to it:


* Forgetting to create a directory.
* Run '''ls -l -h''' and look at the sizes of the files:
* Making syntax errors (eg. spelling or mixing up character case)
[[File:Ls-h.png|center|812x812px]]
* Not creating subdirectories within specified directories.


= less =
Many commands in Linux are strangely named, for complicated reasons. For example the command '''less''' is simply called that because the older version of the same thing was called '''more'''.


<table align="right"><tr valign="top"><td>[[Image:script-check-1.png|thumb|right|250px|If all checks pass, then user performed task correctly and can continue.]]</td><td>[[Image:script-check-2.png|thumb|right|450px|If there is a warning, then feedback is provided to user to correct and re-run checking script.]]</td></table>
If you open 1984.txt in a graphical text editor: you get the cursor and scrollbar you're used to. Those things don't have exact equivalents in a terminal.
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.


*Run '''cat 1984.txt''' then '''cat AnimalFarm.txt''' (these are examples of really long text files).
*Try to scroll to the beginning of the AnimalFarm book. Or find Chapter 2 of 1984. You'll find it very difficult even though this terminal does have a scroll bar.
*Run '''less 1984.txt''' and use the up/down/PgUp/PgDown/Home/End keys to navigate through the file.
**Press the '''/''' (forward slash) key, then type in '''Chapter 2''', and press Enter.
**Press '''q''' to quit.
*Run '''less AnimalFarm.txt''' and navigate through it. Quit the less program.
You are unlikely to be reading books in a terminal but there are many other kinds of plain text files you'll have to open as part of your job as an administrator. Pretty much any text file which won't fit on one terminal screen is a good candidate for reading with less.


'''Perform the Following Steps:'''
= File types and extensions =
In Linux primarily the extension of a file has only cosmetic purposes - the files work the same way no matter what their name is, or what their extension is.


# Make certain that your current directory is '''your home directory'''.<br><br>
* Open the BigBugBunny.mp4, NotPlainText.odt, NotPlainText.pdf, and Snow.jpg files in the graphical file manager. Note that each file opens in the appropriate application.
# Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week2-check-1</span><br><br>
# 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.
<br>


===Part 2: Viewing Directory Contents / Copying & Moving Directories===
The NotPlainText.odt and NotPlainText.pdf files appear to only have text in them, but that's deceptive. There is in fact much more information in these files which is not being displayed by LibreOffice and Xreader.


Now that you have learned how to efficiently create your directory structure, you will now learn how to issue Linux commands<br>to '''view directory contents''' as well as '''copy''' and '''move''' directories.
* In your terminal try to run '''cat NotPlainText.odt''' or '''cat NotPlainText.pdf''' and see what happens.


The meaning of a file is determined by its contents, not by the filename. Renaming a pdf file to have a txt extension isn't going to change the contents of the pdf file. Renaming an .mp4 file to .jpg isn't going to turn the video into a still frame. Apporpriate software is needed to make such conversions.


'''Perform the Following Steps:'''
* Rename the sample files you have, removing the extensions completely.
<table align="right"><tr valign="top"><td>[[Image:directory-structure-5.png|thumb|right|250px|Output of the '''tree''' command to display directory structure.]]</td><td>[[Image:directory-structure-6.png|thumb|right|250px|Output of the '''ls -R''' command to display directory structure.]]</td></table>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tree</span><br><br>'''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.<br><br>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''').<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 uli101xx</span><br><br>Note the differences between both of these commands (i.e. using and <u>not</u> using a pathname).<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 '''uli101xx''' directory (and contents) to the '''xyz100xx''' directory.]]</td><td>[[Image:directory-structure-8.png|thumb|right|300px|Output of the '''tree''' command to confirm movement of '''acp100 directory''' directory (and contents) to the '''xyz100xx''' diredtory.]]</td></tr></table>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cp -R uli101xx xyz100xx</span><br><br>
#Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101xx directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree </span><br><br><br><br><br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv acp100xx  xyz100xx/uli101xx/tutorials</span><br><br>
#Issue the following Linux command to display the directory structure of your home directory to confirm you moved the acp100xx directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree </span><br><br>Let's run a checking script to see if you moved and copied the directories correctly.<br><br>
# Make certain that your current directory is '''your home directory'''.<br><br>
# Issue the following Linux command run checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week2-check-2</span><br><br>
# If you encounter errors. make corrections and re-run the checking script until you receive a congratulations message, and proceed to the next investigation.<br><br>


===Part 3: Removing Directories ===
Usually the '''file''' command can guess the type of file based on its binary contents. Try it on the sample files:
[[File:FileCommand.png|center|812x812px]]


In this investigation, we will learn how to '''safely remove''' directories and their contents.
* Double-click on the extensionless files in the graphical file manager and note that they still open in the correct applicaton.
* After you're done experimenting: you probably want to rename the files again to include the original extensions.


 
= Submit evidence of your work =
'''Perform the Following Steps:'''
For this lab: choose whatever screenshots you feel show that you've completed the work, and submit them on Blackboard.
 
[[Category:OPS145]]
# Confirm that you are located in your home directory.<br><br>The '''rmdir''' command is used to remove <u>empty</u> directories.<br><br>
# Issue the following Linux command to remove the empty directory called '''tutorials''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101xx/tutorials</span><br><br>
# Issue a command to confirm that you removed the '''tutorials''' directory.<br><br>
# Issue the following Linux command to remove the empty directory called '''uli101xx''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101xx</span><br><br>NOTE: You should get an '''error message''', since the '''uli101xx''' directory is '''NOT''' empty.<br><br>To remove non-empty directories, you can use the '''rm -r''' command.<br>The '''-r''' option stands for recursive, which can travel down the directory paths and their contents.<br><br>
# Issue the following Linux command to remove the '''uli101xx''' directory and its contents: <span style="color:blue;font-weight:bold;font-family:courier;">rm -r uli101xx</span><br><br>
# Issue the '''tree''' command to confirm that the uli101xx directory (contained in your home directory) and its contents have been removed.<br><br>'''NOTE:''' To <u>safely</u> 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.<br><br>
# Issue the following Linux command (entering "y" when prompted): <span style="color:blue;font-weight:bold;font-family:courier;">rm -ri xyz100xx</span><br><br>'''NOTE:''' You should have removed all directories that you have created.<br><br>Let's run a checking script to confirm that you have correctly removed all of those directories.<br><br>
# Issue the '''tree''' command to confirm that all of those recently created directories have been removed.<br><br>
# Issue the following Linux command to run a checking script to confirm removal of those directories:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week2-check-3</span><br><br>
# If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message, and proceed to the next INVESTIGATION.<br><br>
 
=INVESTIGATION 2: MANAGING TEXT FILES=
<table align="right"><tr valign="top"><td>[[Image:nano-text-editor-1.png|thumb|right|250px|The '''Nano Text Editor''' is Easy to Operate for Novice Users.]]</td><td>[[Image:vi-text-editor-1.png|thumb|right|300px|The '''vi Text Editor''', although taking longer to master, has outstanding features and allow the user to be more productive with editing text files.]]</td></tr></table>
Since IT students will be working in the Unix / Linux command line environment, it is useful to learn a least two common command-line '''text editors'''.
 
Although '''programming students''' can use '''graphical IDE's''' to code and compile programs, they can '''create source code''' using a text editor and '''compile their source code''' in the '''Matrix''' server to generate executable programs (without having to transfer them for compilation or execution).
 
'''Networking and Tech Support students''' can use a text editor to '''edit configuration files'''. These students in upper grades will become familiar with the process of installing, configuring, and running network services and text editors are an important tools to help setup but also "tweak" or make '''periodic changes in networking services configuration'''.
 
The two most readily-available command line text editors in Linux are '''Nano''' and '''vi'''. The Nano text editor would seem like an easier-to-use text editor, but vi (although taking longer to learn) has outstanding features and allow the user to be more productive with editing text files.
 
===Part 1: Creating Text Files Using The Nano Text Editor===
 
You will now learn basic editing skills using the '''Nano''' text editor including '''creating''', '''editing''', and '''saving''' text files.
 
'''Perform the Following Steps:'''
 
# Make certain that you are located in your home directory.<br><br>[[Image:directory-structure-10.png|right|150px|thumb|]]
# '''Create''' the following directory structure (displayed on the right side) by issuing a '''single Linux command'''<br>(You should know how to do this from the previous INVESTIGATION).<br><br>
# Issue a Linux command to check that you correctly created those directories.<br><br>
# Issue the following Linux command to edit a text file called '''mytext.txt''' in the '''linux/practice''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">nano textedit/practice/mytext.txt</span><br><br>'''NOTE:''' When using the '''Nano''' text editor, you are placed in '''INPUT''' mode, so you can enter text immediately.<br><br>[[Image:nano-text-editor-2.png|right|400px|thumb|Enter the following text that appears in this diagram.]]
# Enter the lines shown in the other diagram of the nano text editor on the right side.<br><br>'''NOTE:''' Refer to the table below for a list of common '''Nano''' navigation &amp; editing commands:<br><br><table cellpadding="5"><tr><th style="border-bottom: 1px solid black;">Key(s)</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr valign="top"><td style="font-family:courier">'''&lt;ctrl&gt;&lt;SPACEBAR&gt;''',<br>'''&lt;esc&gt;&lt;6&gt;'''</td><td>Move forward/backward one word</td></tr><tr valign="top"><td style="font-family:courier">'''&lt;ctrl&gt;&lt;a&gt;''',<br> '''&lt;ctrl&gt;&lt;e&gt;'''</td><td>Move to beginning/end of line</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;k&gt;'''</td><td>Cut line</td></tr><tr><td style="font-family:courier">'''&lt;esc&gt;6'''</td><td>Copy Line</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;u&gt;'''</td><td>Paste Cut/Copied Text</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;g&gt;'''</td><td>Display help screen (ctrl-x to exit help screen)</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;x&gt;'''</td><td>Save editing changes and exit</td></tr></table><br>
# Referring to the table above, practice navigating and editing your entered lines for practice.<br><br>
# To '''save''' your editing session, press: <span style="color:blue;font-weight:bold;font-family:courier;">&lt;ctrl&gt;x</span><br><br>
# You will be prompted to modify your file: type the letter <span style="font-family:courier;color:blue;font-weight:bold">y</span> for '''yes'''.<br><br>
# The name of the file will be displayed, Press <span style="color:blue;font-weight:bold;font-family:courier;">ENTER</span>.to save editing changes for that file name.<br><br>'''NOTE:''' This prompt for file name allows you to change the name of the file if you wish.<br>By pressing '''ENTER''', it will accept the default filename.<br><br>
# Perform some more editing operations based on the editing chart above.<br><br>
# '''Save''' your editing session and '''exit''' your text editor.<br><br>
 
===Part 2: Creating Text Files Using The vi Text Editor===
[[Image:vi-screen.png|right|300px|thumb|Using the '''vi''' text editor.]]
You will now learn basic editing skills using the '''vi''' ('''vim''') text editor including '''creating''', '''editing''', and '''saving''' text files.
 
The '''vi''' ('''vim''') text editor (although taking longer to learn) has outstanding features to increase coding productivity.
 
The major different between nano and vi is that '''vi starts in COMMAND LINE mode'''.  You need to issue letter commands to enter text. Also you can press colon  “: ” in COMMAND mode to enter more complex commands.
 
'''Perform the Following Steps:'''
 
# Make certain that you are located in your home directory.<br><br>
# Issue the following Linux command to edit a text file called '''othertext.txt''' in the '''linux/practice''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">vi textedit/practice/othertext.txt</span><br><br>'''NOTE:''' When using the '''vi''' text editor, you are placed in '''COMMAND''' mode,<br>so you need to issue a command to switch to ''INPUT'' mode.<br><br>[[Image:vi-text-editor-a.png|right|200px|thumb|Enter the following text that appears in this diagram.]]
# Type the following key to enter ''INPUT'' mode: <span style="color:blue;font-weight:bold;font-family:courier;">i</span><br><br>You should notice you are in ''INSERT'' mode by seeing the notification '''-- INSERT --'''<br>at the bottom left-hand side of the application screen.<br><br>
# Enter the line shown in the other diagram of the vi text editor on the right side.<br><br>
# While on the first line, press the following key to enter ''COMMAND'' mode: <span style="color:blue;font-weight:bold;font-family:courier;">ESC</span><br><br>You should see the ''-- INSERT --'' notification disappear indicating that you are in ''COMMAND'' mode.<br><br>'''NOTE:''' Refer to the table below for a list of<br>the most common '''vi''' ('''vim''') navigation &amp; editing commands:<br><br><table cellpadding="5"><tr><th style="border-bottom: 1px solid black;">Key(s)</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr><td style="font-family:courier">'''i'''</td><td>Enter '''INPUT''' mode</td></tr><tr><td style="font-family:courier">'''&lt;esc&gt;'''</td><td>Return to '''COMMAND''' MODE</td></tr><tr><td style="font-family:courier">'''x'''</td><td>Delete text to the right in COMMAND mode</td></tr><tr><td style="font-family:courier">'''W'''</td><td>Move forward one word in COMMAND mode</td></tr><tr><td style="font-family:courier">'''B'''</td><td>Move back one word in COMMAND mode</td></tr><tr><td style="font-family:courier">'''dd'''</td><td>Cut line in COMMAND mode</td></tr><tr><td style="font-family:courier">'''yy'''</td><td>Copy Line in COMMAND mode</td></tr><tr><td style="font-family:courier">'''p / P'''</td><td>Paste Cut/Copied Text below/above line in COMMAND mode</td></tr><tr><td style="font-family:courier">'''u'''</td><td>Undo previous editing command</td></tr><tr><td style="font-family:courier">''':help'''</td><td>Display help screen in COMMAND mode</td></tr><tr><td style="font-family:courier">''':x'''</td><td>Save editing changes and exit (in COMMAND mode)</td></tr><tr><td style="font-family:courier">''':w name'''</td><td>Save editing changes to "name" in COMMAND mode</td></tr><tr><td style="font-family:courier">''':q!'''</td><td>Abort editing session and exit (in COMMAND mode)</td></tr></table><br>Once you can get used to working in INPUT and COMMAND mode, it is easier to perform<br>text editing operations in '''vi''' via ''COMMAND'' mode as opposed to nano!<br><br>
# Type the following keys to <u>copy</u> the current line: <span style="color:blue;font-weight:bold;font-family:courier;">yy</span><br><br>
# Type the following key to <u>paste</u> the copied line: <span style="color:blue;font-weight:bold;font-family:courier;">p</span><br><br>What did you notice?<br><br>
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">u</span><br><br>What did you notice?<br><br>
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">3p</span><br><br>What did you notice?<br><br>[[Image:vi-text-editor-b.png|right|200px|thumb|Use a combination of '''arrow''' keys and the '''w''' and '''b''' keys while in COMMAND mode to change the correct words for the '''third''' and '''fourth''' lines.]]
# Use the up or down '''arrow''' keys to move to the second line.<br><br>
# Type either the '''w''' and/or '''b''' keys to move the cursor to the beginning of the word: '''first'''.<br><br>
# Type the '''x''' keys to remove the word called '''first'''.<br><br>
# Type the '''i''' key to enter INSERT mode.<br><br>
# Type the word: '''second'''<br><br>
# Press the '''ESC''' key to enter COMMAND mode.<br><br>
# Repeat steps '''11''' to '''16''' to change the words for line number for lines '''3''' and '''4'''.<br><br>
# Move to the 4th line (i.e. last line).<br><br>
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">dd</span><br><br>What did you notice?<br><br>Let's save editing changes and exit the vi text editor.<br><br>
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">:x</span> and press '''ENTER'''<br><br>What did you notice?<br><br>
# Issue the same command that you performed in '''Step #2''' to confirm that you had properly edited that file.<br><br>
# '''Save''' and '''exit''' your vi editing session.<br><br>[[Image:vi-text-editor-2.png|right|250px|thumb|The online vi-tutorial provides users "hands-on" experience of using the vi text editor.]]An '''online tutorial''' has been created to give you "hands-on" experience on how to use vi text editor. It is <u>recommended</u> that you run this online tutorial in your Matrix account to learn how to create and edit text files with the vi text editor.<br><br>
# Issue the following to run the vi online tutorial:<br><span style="color:blue;font-weight:bold;font-family:courier;">/home/jason.carman/vi-tutorial<br><br>
# In the tutorial menu, select the first menu item labelled "'''USING THE VI TEXT EDITOR'''"<br><br>
#Read and follow the instructions in the tutorial. Eventually, it will display a '''simulated''' vi environment<br>and will provide you with "'''hands-on'''" practice using the vi text editor. As far as this author is aware,<br>there is NO "hands-on" tutorial for the nano text editor in this particular format.<br><br>
# When you have completed that section, you will be returned to the main menu.<br>If you want to get extra practice, you can select the menu item labelled "'''REVIEW EXERCISE'''".<br><br>
# When you want to exit the tutorial, select the menu option to exit the tutorial.<br><br>
 
: It is recommended to try both text editors, and choose the text editor that you feel that is easier to use.<br><br>
 
===Part 3: Manage &amp; Manipulate Text File Content===
 
We conclude this tutorial by learning to '''manage''', '''view''' or '''manipulate the <u>display</u>''' of text files. This is HIGHLY ADVISED in case you only want to view contents and '''NOT''' edit text file contents which can cause accidental erasure of data.
 
'''Perform the Following Steps:'''
 
# Make certain that you are located in your home directory.<br><br>
# Refer to the following table of '''Text File Management Commands:'''<br><br><table cellpadding="5"><tr><th style="border-bottom: 1px solid black;"  width="20%">Linux Command</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr><td style="font-family:courier">'''touch'''</td><td>Create empty file(s) / Updates Existing File's Date/Time Stamp</td></tr><tr><td style="font-family:courier">'''cat'''</td><td>Display text file's contents without editing (small files)</td></tr><tr><td style="font-family:courier">'''more , less'''</td><td>Display / Navigate within large text files without editing</td></tr><tr><td style="font-family:courier">'''cp'''</td><td>Copy text file(s)</td></tr><tr><td style="font-family:courier">'''mv'''</td><td>Move / Rename text files</td></tr><tr><td style="font-family:courier">'''rm'''</td><td>Remove text file(s)</td></tr><tr valign="top"><td style="font-family:courier">'''sort'''</td><td>Sorts (rearranges) order of file contents when displayed. Content is sorted alphabetically by default. The '''-n'''''Italic text'' option sorts numerically, '''-r''' performs a reverse sort</td></tr><tr valign="top"><td style="font-family:courier">'''head''' , '''tail'''</td><td>Displays the first / last 10 lines of a text file by default. An option using a value will display the number of lines (e.g. <span style="font-family:courier">head -5 filename</span> will display first 5 lines, <span style="font-family:courier">tail -5 filename</span> will display last 5 lines).</td></tr><tr valign="top"><td style="font-family:courier">'''grep'''</td><td>Displays file contents that match a pattern</td></tr><tr valign="top"><td style="font-family:courier">'''uniq'''</td><td>Displays identical consecutive lines only once</td></tr><tr valign="top"><td style="font-family:courier">'''diff''' file1 file2</td><td>Displays differences between 2 files</td></tr><tr><td style="font-family:courier">'''file'''</td><td>Gives info about the contents of the file (e.g. file with no extention)</td></tr><tr><td style="font-family:courier">'''find'''</td><td>To find files matching specified characteristics: <!-- <table><tr><td width="30%">'''<span style="font-family:courier">find . -name "file*"</span>'''</td><td>List pathname of any filenames beginning with "file",<br>from the current directory and any subdirectories</td></tr><tr valign="top"><td>'''<span style="font-family:courier">find . -size +50k</span>'''</td><td>List pathname of any files larger than 50 kb, from the current directory and any subdirectories</td></tr><tr valign="top"><td>'''<span style="font-family:courier">find . -mmin -5</span>''' </td><td>List files modified less than 5 minutes ago</td></tr><tr> valign="top"><td>'''<span style="font-family:courier">find -P .</span>'''</td><td>Lists file pathnames in the current directory</td></tr></table> --> </td></tr></table><br>
# Issue the following Linux command to create three <u>empty</u> text files in your current directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">touch a.txt b.txt c.txt</span><br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -l a.txt b.txt c.txt</span><br><br>Check the size in the detailed listing to confirm that these newly-created files are empty.<br><br>[[Image:vi-screen-40.png|right|300px|thumb|Nano text editor containing numbers '''1 to 40'''<br>on separate lines.]]
# Use the '''nano''' text editor to edit the empty file called '''a.txt'''. <br><br>Type the number "'''1'''" and press '''ENTER'''. On the second line, type the number "'''2'''" and press '''ENTER'''.<br>Continue entering increasing number values until you reach the number '''40''' on line 40<br>(refer to the diagram on the right).<br><br>
# '''Save''' and '''exit''' your editing session.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat a.txt</span><br><br>Can you see all of the contents?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">more a.txt</span><br><br>'''NOTE:''' The '''more''' command uses the same navigation keys as with the '''man''' command<br>(refer to week 1 notes for reference). Try using keys that you used to navigate the man pages.<br><br>What is the advantage of using the more command?<br><br>
# Type the letter "<span style="color:blue;font-weight:bold;font-family:courier;">q</span>" to exit the ''more'' command.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">less a.txt</span><br><br>Is there any difference between the '''more''' and '''less''' commands?<br>(again press '''q''' to quit)<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort a.txt</span><br><br>Why does the output not look what you expected? Why?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort -n a.txt</span><br><br>Try the same command using both the '''-n''' and '''-r''' options to see what happens.<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">head a.txt</span><br><br>What is the output from this command display?<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">head -7 a.txt</span><br><br>What is the output from this command display?<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tail a.txt</span><br><br>What is the output from this command display?<br>How would you issue this command to display only the '''last line''' contained in that file?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">grep 2 a.txt</span><br><br>What type of output appear? Why did these lines appear (what do they all have in common)?<br><br>[[Image:end-of-line.png|right|200px|thumb|Edit the '''a.txt''' file and add to the bottom 5 new lines each consisting of the <u>same</u> text: "'''end of line'''".]]
# Edit the '''a.txt''' file and add to the bottom 5 new lines each consisting<br>of the <u>same</u> text: "'''end of line'''" (refer to diagram on right).<br><br>
# '''Save''' your editing session and '''exit''' your text editor.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">uniq a.txt</span><br><br>What do you notice happened to those newly created lines?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp a.txt a.txt.bk</span><br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp a.txt b.txt</span><br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">mv a.txt aa.txt</span><br><br>
# Issue a Linux command to view the directory contents.<br><br>What happened to the file called '''a.txt'''? Why?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">file b.txt</span><br><br>What sort of information did it provide?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">diff aa.txt b.txt</span><br><br>Was there any output? If not, why?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">diff aa.txt c.txt</span><br><br> What do you think is the purpose of this output?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">find -P . </span><br><br>What is the output of this command?<br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">rm aa.txt b.txt a.txt.bk c.txt</span><br><br>
# Issue the '''ls''' command to verify that these files have been removed.<br><br>
# Complete the '''LINUX PRACTICE QUESTIONS''' to get additional practice.
 
= 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://wiki.cdot.senecacollege.ca/uli101/files/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:'''
 
For each of the following questions, use a pathname starting from the '''root''' directory (i.e. “/”).
 
[[Image:tree-diagram.png|right|350px|thumb|]]
# Write a single Linux command to create the directory structure starting from your home directory from the diagram displayed on the right.
# Write a Linux command to display a detailed listing of '''history''' directory.<br>How would this command differ if you wanted to also view hidden files as well?
# Write a Linux command to change to the '''project''' directory.<br>What command would you issue to return to your home directory?
# Write a Linux command to copy the '''project''' directory and its contents to the '''history''' directory.
# Write a Linux command to move the directory called '''directories''' to the '''history''' directory.
# Write a Linux command to remove both directories named '''1''' and '''2'''.
# Write a Linux command to remove the '''concepts''' directory and its contents.
# Write a Linux command to remove the '''concepts''' directory and prompt the user if they want to remove this directory’s contents.
# Write a single Linux command to create the following empty files in the concepts directory:
#* '''myfile.txt'''
#* '''yourfile.txt'''
#* '''thefile.txt'''
# Write a Linux command to view the contents of the myfile.txt text file to prove it is empty. What is the difference between the commands: '''cat''', '''more''' and '''less'''?
# Write a Linux command to sort the contents of a file called '''practice/customers.txt'''
# Write a Linux command to display the first 4 lines of a file called '''practice/customers.txt'''
# Write a Linux command to display the last line of a file called '''practice/customers.txt'''
# Write a Linux command to match a line containing the pattern '''Linux''' in a file called '''practice/customers.txt'''
# Write a Linux command to display unique occurrences of consecutive lines in a file called '''practice/customers.txt'''
# Create a '''table''' listing each Linux command, useful options that are displayed near the top of this tutorial labelled: '''Tutorial Reference Material'''
 
_________________________________________________________________________________
 
Author:  Murray Saul
 
License: LGPL version 3
Link:    https://www.gnu.org/licenses/lgpl.html
 
_________________________________________________________________________________
 
[[Category:ULI101]]

Latest revision as of 09:58, 17 January 2024

Multi-user systems

Linux was intended from its beginnings to be a multi-user system. A multi-user system allows multiple people to use it at the same time. That concept is hard to understand if you think of a computer as having one screen, one keyboard, and one mouse.

But originally systems like Unix (the Linux predecessor) were used as workstations by multiple people at once. They got around the problem of sharing one monitor and one keyboard by using dumb terminals. A dumb terminal might look like a computer but it isn't. It's just a screen plus a keyboard plus a serial port connection (like a slow network connection) to the actual computer (sometimes called a mainframe).

Other systems like Microsoft DOS (which eventually got a graphical interface and was then called Windows) had no such capabilities, and were much simpler. That made them the perfect choice for this new thing called a Personal Computer (PC). You didn't need much sysadmin training to put DOS on a PC.

For decades the mainframe was powerful enough to serve the needs of all the employees, but cheap enough that it wasn't going to get replaced with personal computers for each employee.

Eventually PCs got much cheaper, and since it was no longer financially viable to stick to mainframes: Microsoft took over the computer world with its DOS and later Windows operating systems.

Why terminals still exist

It turns out that when you create an operating system intended to be used by one user on their personal computer: you don't consider security or even multi-tasking performance as priorities.

In the 1980s and 1990s the entire world was plagued by viruses, blue screens of death, and abysmal server performance because the demand for IT increased exponentially but Unix was expensive, Unix administrators were hard to find, so Windows was the only option for many.

Luckily Linus Torvalds wrote an operating system kernel mostly compatible with Unix, and together with hundreds and thousands of other developers a new operating system was born which did not suffer from the MS DOS legacy problems: Linux.

Dumb terminals have almost completely dissapeared. They're now only used by very large companies who like to pay IBM lots of money for basic IT services. I've recently seen a green screen being used by a bank teller, and smiled.

The mainframes of the 1970s have been replaced by PC-compatible servers and server farms. Two types of terminals are still used pervasively:

  • For regular users: a web browser is just a pretty version of a dumb terminal. All the content lives on the server, and all the actions performed by the website visitor are performed on the server.
  • The server administrators use almost identical terminals now as were used 50 years ago, except they're software terminals instead of hardware terminals. A graphical interface, while inviting with its apparent ease of use, is simply not efficient enough to allow complex administration tasks on remote servers. You are going to learn to use this sort of terminal in OPS145 and the rest of the OPS stream.

What can you do in a terminal

If you're a server administrator: just about everything that you can do can be done in a terminal. For example:

  • Copying and moving files and directories.
  • Creating users and controlling their access privileges.
  • Configuring and controlling services (e.g. a web server).
  • Monitoring server activity.
  • Installing and uninstalling software.
  • Configuring networking.
  • Configuring other hardware (like hard drives).
  • Starting and stopping machines on demand.
  • Creating, deploying, and monitoring all sorts of automation.

Terminals are probably completely new to you, and though they're older than your grandparents: don't imagine that they're ancient garbage. Most of the exciting computer things created in your lifetime were built and are managed in terminals. Once you learn the basics: you'll impress yourself with what you can accomplish by typing commands rather than opening windows and clicking buttons. That's what OPS145 is for.

ls, pwd, cd

One of the simplest things you can do is look at the contents of a directory (a.k.a. a folder).

  • First open a graphical file manager in your ops145 VM.
  • In the View menu change the Sidebar type to Tree.
  • Also in the View menu change the Toolbar type to Location Entry.

This will encourage you to better understand paths over the long term. Note that it appears you have 8 empty subdirectories in your home directory.

  • Open the terminal application and run the ls command. To run a command you type it at the prompt and press enter.

The ls command (as most other commands) produces some text output and that output is printed in the terminal window where you executed the command.

  • Run the clear command. This is helpful at times when there is too much old output in the terminal window which you don't care about and is distracting you. This doesn't undo the commands you ran previously, it simply erases their output from the screen.
  • Try running ls using different capitalization, for example Ls, LS, or lS. Try to run some other made up commands and note that the error is the same: command not found.
BadLS.png
  • Open the "Text Editor" graphical application. This works with plain text files.
  • Write some text in it and save the file as first.txt in your Documents directory.
  • Close the text editor, and open your first.txt again by finding it in your graphical file manager.
  • Run ls in your terminal and note that the Documents directory is there, but the first.txt file does not show up. That's because by default the ls command shows the contents of the Present Working Directory (PWD), your PWD is /home/yourusername, but first.txt is in the /home/yourusername/Documents/ directory.
  • Change your PWD by running cd Documents.
  • And run ls again. You should now see first.txt. But it no longer shows Desktop, Downloads, etc. because those are subdirectories of /home/yourusername/ and your PWD is /home/yourusername/Documents/
  • Run the pwd command. That simply shows you the PWD, which you can typically see anyway in the terminal prompt.

ls -l

You can simply run a command by typing in the name of the command and waiting for it to execute its default behaviour. But you can also change that default behaviour, by following the command with one or more arguments (a.k.a. options, a.k.a. parameters, a.k.a. switches). Each command has its own set of possible arguments. Sometimes they're similar between commands, most of the time they're not.

For the ls command one such argument is -l (minus lower case L). This instructs ls to print extra details about each file and directory.

  • Run ls -l

For now we can ignore most of that output, but do try to find the size of the file. It's the number just before the modification date. By default the size is in bytes.

Ls-l.png

Since the text is written in english: each character is an ASCII character, which uses one byte of storage. Spaces and newlines (Enters) are also one-byte characters.

  • Count the number of characters in your text file and confirm that it's the same as the size of the file.

cat

The default behaviour of the cat command is to read the contents of a file and dump them out on the terminal. The terminal can only display text. Other types of data will likely show up as garbage.

  • Run cat first.txt to see the contents of first.txt The name of the command is cat and the first argument is first.txt
CatFirstTxt.png

Commands and arguments are separated by spaces. The number of spaces doesn't matter.

  • Try to run the ls and cat commands you ran earlier but add more spaces before the command, between the command and the argument, and at the end.

The ls -l command shows you information about the file. The cat command shows you the contents of the file.

  • Create a new text file in the graphical editor and save it with a name which has a space. Then try to use cat to see its contents:
CatFileWithSpace.png

"No such file or directory" is one of the most common errors. Usually it's obvious what it means, but often (as in this case) you have to think a little.

Since the command arguments are separated by spaces: the cat command has no way of guessing that second and file.txt are one filename, it assumes they are two separate files. Later in the course you'll learn to deal with spaces and other special characters in filenames.

  • Try to use cat to see the contents of First.txt (note the capital first letter). It won't work since First.txt and first.txt are as different from each other as first.txt and hirst.txt.
  • Use the graphical text editor to create another text file, and save it in the Documents directory with the name First.txt, then repeat your last cat command:
CatfirstFirst.png
  • Note also that the sizes of the files are different. First.txt has fewer characters, therefore it's smaller.

ls -l -h

First get some sample files to use for the rest of this lab.

  • Download SampleFiles.tar.xz (it's like a zip file) into your Downloads directory.
  • Double-click on it in the graphical file manager, and extract its contents also into the Downloads directory. You can either use drag-and drop or click the appropriate buttons. Later in the course you'll learn to download and extract tarballs using only the command line.
  • Open a new terminal window. You can close the original one if it confuses you.
  • Use cd to change your PWD to the Downloads directory, then to the SampleFiles directory, and confirm using ls -l that your extracted files are there.

Larger file sizes are harder to understand if they are expressed in bytes. You can get the ls command to show sizes in human-readable format by adding another argument to it:

  • Run ls -l -h and look at the sizes of the files:
Ls-h.png

less

Many commands in Linux are strangely named, for complicated reasons. For example the command less is simply called that because the older version of the same thing was called more.

If you open 1984.txt in a graphical text editor: you get the cursor and scrollbar you're used to. Those things don't have exact equivalents in a terminal.

  • Run cat 1984.txt then cat AnimalFarm.txt (these are examples of really long text files).
  • Try to scroll to the beginning of the AnimalFarm book. Or find Chapter 2 of 1984. You'll find it very difficult even though this terminal does have a scroll bar.
  • Run less 1984.txt and use the up/down/PgUp/PgDown/Home/End keys to navigate through the file.
    • Press the / (forward slash) key, then type in Chapter 2, and press Enter.
    • Press q to quit.
  • Run less AnimalFarm.txt and navigate through it. Quit the less program.

You are unlikely to be reading books in a terminal but there are many other kinds of plain text files you'll have to open as part of your job as an administrator. Pretty much any text file which won't fit on one terminal screen is a good candidate for reading with less.

File types and extensions

In Linux primarily the extension of a file has only cosmetic purposes - the files work the same way no matter what their name is, or what their extension is.

  • Open the BigBugBunny.mp4, NotPlainText.odt, NotPlainText.pdf, and Snow.jpg files in the graphical file manager. Note that each file opens in the appropriate application.

The NotPlainText.odt and NotPlainText.pdf files appear to only have text in them, but that's deceptive. There is in fact much more information in these files which is not being displayed by LibreOffice and Xreader.

  • In your terminal try to run cat NotPlainText.odt or cat NotPlainText.pdf and see what happens.

The meaning of a file is determined by its contents, not by the filename. Renaming a pdf file to have a txt extension isn't going to change the contents of the pdf file. Renaming an .mp4 file to .jpg isn't going to turn the video into a still frame. Apporpriate software is needed to make such conversions.

  • Rename the sample files you have, removing the extensions completely.

Usually the file command can guess the type of file based on its binary contents. Try it on the sample files:

FileCommand.png
  • Double-click on the extensionless files in the graphical file manager and note that they still open in the correct applicaton.
  • After you're done experimenting: you probably want to rename the files again to include the original extensions.

Submit evidence of your work

For this lab: choose whatever screenshots you feel show that you've completed the work, and submit them on Blackboard.