Computer lab introduction/extras
- 1 With GUI
- 2 Without GUI
- 3 Plain-text editors
- 4 cygwin tips
- 5 Advanced topics (unfinished)
- 6 More about text editors
- 7 Online resources to learn UNIX
- Linux: gedit (GNOME default), Kate/KWrite (KDE default)
- Mac: TextEdit (default), TextWrangler
- Windows: Notepad (default), Notepad++
Programs that you might be used to using for manipulating text (e.g., Microsoft Word) do all sorts of complicated things to your files, including inserting lots of weird codes for specific formatting, special software-specific characters, page margin instructions, etc. The software that we are going to use frequently are controlled by text files (that's where their instructions are stored), and they are easily confused--they need a simple file with just their instructions, and none of that other stuff. So we need a plain-text editor to make and manipulate these files.
Note: different operating systems code line-break or end-of-line characters in different ways, and this can cause problems (Unix: LF '\n', Mac: CR '\r', Windows: CR+LF '\r\n').
The root folder of the directory tree is symbolized by a forward slash "/". Path names through the directory tree are formed by separating directory names with the "/" character. For example in the path /usr/local, local is a subdirectory of usr, which in turn is a subdirectory of the root folder (/). Further, usr is called the parent directory of local (whose parent is /). Users with an account on the computer have a so called home directory for their own files and folders, and where the operating system keeps user-specific settings. The operating system uses other locations in the directory tree to store system-wide files, like some executables, system-wide settings, etc.
In Windows, each disk partition has its own, separate root (without a common root for all partitions). However, each drive still has a most elemental directory, e.g. denoted C:\ for the partition named C. Absolute paths are therefore based on the partition root. Folder names in paths are separated by the backslash character "\".
Current working directory
The current working directory (or working directory) may be defined as the directory we are working in at the moment. Path names that do not start with / are interpreted as relative to the current working directory.
Calling a program
First, let's make a file using the command-line editor "nano." If you use a pre-existing file as nano's target, it will open it for editing, and if you use a non-existent file name, nano will create it for you (a new blank file). Let's do the latter:
This command both opens nano, and creates a file called "firstprogram.sh". In the nano window, enter the following two lines:
#!/bin/bash echo "hello mbl"
This file is called a shell script. It is a file that contains lists of unix commands like echo, ls, etc.
To move around your nano window, use the arrow keys, not your mouse. Control+x will exit, and nano will ask you if you want to save changes. Say yes.
Shell scripts almost always have the suffix ".sh". They are run by typing "sh" before the filename.
We can run this program by typing
This should print 'hello mbl' to the screen.
- Cygwin (unix interface for windows)
- NOTE: when installing Cygwin, be sure to install
- Python (whole thing)
Command line editing =
The following features of most command line interpreters often come in handy:
- Up and down arrow keys: cycle through previously issued commands
- Tab completion
- 'CTRL+a' moves cursor to beginning of the line
- 'CTRL+e' moves cursor to end of the line
Advanced topics (unfinished)
Adding a directory to the path
sed, grep, and awk
- sed (UNIX stream editor)
- Regular expressions cheat-sheet: http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
More about text editors
Looking inside text files
There are different commands that can be used to look inside files from within the command line interface. Two of these are cat in Linux/Mac, type in MS-DOS, and less in Linux/Mac, more in MS-DOS. The command more is also available in Linux/Mac, and in fact more is less, but less has more features than more.
- cat: cat stands for concatenate. This command is useful for peeking at short files; using cat to view a long file results in the top lines scrolling off before one can even read them. The simplest use of cat is with
- less: This command is useful to read long text files because a page of text (i.e., a command line window filled from top to bottom) is displayed one at a time. With less is easy to move forward and backward by lines, pages and even between two or more files. less is a program in itself, so when it is invoked, a prompt appears at the bottom of the page awaiting for a new less command. The prompt in less is a colon (:). The simplest use of less is with
The following table contains a list of useful less commands
|spacebar||display next page|
|return||display next line|
|n f||move forward n lines|
|b||move backward one page|
|n b||move backward n lines|
|/ word||search forward for word|
|? word||search backward for word|
Online resources to learn UNIX
|pwd||chdir||print working directory||pwd|
|ls||dir||list directory contents||ls|
|history||doskey /history||display command history||history|
|cd||cd||change directory||cd directory_name|
|mkdir||mkdir||make directory||mkdir directory_name|
|cp||copy||copy files||cp original_filename copied_filename|
|mv||move||move files (the same as rename files)||mv original_filename moved_filename|
|rm||del||remove file(s)||rm filename|
|clear||cls||clear the screen||clear|
|exit||exit||quit command line||exit|
Suggestions for next year (2013)
- Explaining the concept that on the server and on their laptop are different environments, as they look the same on terminal
- Not mentioning GUIs at intro but perhaps mention a few days later for scp operations (cyberduck etc)
- Find a better solution for windows than putty
- Have students learn basic commands by moving/copying/downloading/unzipping files for a REAL application. For example, have them download something for a lab, generate a dummy results.txt (using nano), and then have them move it back onto their local desktop.
- Quickly make a distinction between using './' and being able to execute things that are saved in bash profiles.