OPS145 Lab 2

From Littlesvr Wiki
Jump to navigation Jump to search

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.