Tutorial 1) Part 2: Testing and Setting Up Unix Terminal

Unix terminals can run a variety of shell languages (bash is a common Linux default). Regardless of whatever login shell loads when you start Terminal, we can run a subshell within it so that we are all entering commands using the same command syntax, which determines how command language, symbols and punctuation are interpreted. For the duration of this course we will be using the C shell language.

tcsh enter this to change the shell each time you open a new terminal window (this opens a subshell within the same terminal. The default login shell is usually bash, which serves as your parent shell).

ps list running processes to confirm that the C subshell was started:

PID TTY TIME CMD

27228 ttys000 0:00.03 -tcsh

Note: Tcsh is an advanced version of csh that allows you to auto-complete commands and filenames by hitting the tab key. If tcsh does't work, you may need to type csh instead.

echo $$ if there are multiple shells running, this tells you the process ID (PID) of the current shell.

27228

Note how the Unix prompt changes format when you change the shell. It typically displays your username, computer name, and current folder (tilde: ~ denotes your home directory). If your Unix prompt is not telling you what folder you are in, you might try:

set prompt = "me:%~ % " (two spaces are enclosed in quotes)


For the remainder of the tutorial we will be trying out various Unix functions to see how each command is used and gain familiarity with the terminal.

set history=9999 keep a record of today's input using this shell environment variable (limit 9999 commands).

Next, in the upper left of your Desktop, select Terminal, Preferences...> Profiles> Window> Limit number of rows to: 10,000. Your eyes may also benefit from changing to a light or dark background color. Cygwin: right-click on the gray menu bar of the terminal (tap touchpad with two fingers) and select Options.. change Cursor to Block and turn off Blinking.


The cal command can take 0, 1 or 2 arguments:

cal

cal 2021

cal 2 2021

Command history: the history command keeps a log of your previously entered commands. Simply cycle through the entries if you need to re-enter or modify a command.

history

!4 runs the 4th command

!-3 runs the third last command

!p runs last command starting with 'p'

!! run last command

<up/down arrow> cycles through previous commands (ctrl-p and ctrl-n should do this as well).


Try using the left/right arrow keys to edit a prior command and re-enter it ( ^a and ^e skip to the begin or end of the line).


The date command returns the current date and time in an output format you can specify:

date

While the string of characters you entered are the input, the text on the next line is the output generated from that command. For your write-up, record the input and output, known as I/O, of various commands.

QUESTION 2: Compare with a classmate and describe the format of the date command output on a Mac and PC. We'll see tomorrow that the format of this data will determine how we construct a program to read the data back in.


Commands can be combined in Unix by letting the output of one command be the input for the next. Consider the echo command, which prints arguments to standard output:

echo he llo echo is given two arguments to print: "he" and "llo"

echo " space s matter" a character string enclosed in quotes stores spaces

We can combine echo with date using the backquote (above the tab key). Backquotes tell the terminal process, or shell, to execute the date program first and use its output as an argument, or input, to echo...

echo Today is 'date' single quotes interpret the text literally

echo today is `date` backquote is in the upper left of the keyboard

Note: Unix is case-sensitive and sometimes space sensitive.

QUESTION 3: Explain the difference between single quote, double quote, and backquote to the shell interpreter.

Make sure your font is large enough so that you can see punctuation and spaces (highlighted in gray). Hold down the ctrl key (apple/command key for Mac) and hit the plus (+) key a few times to enlarge a window's font/view. Hitting ctrl minus (-) will reduce a window's font size.


Filename Completion

We're going to be doing a lot of typing, and Unix has many customizable features to make us more efficient. One is auto-completion, a system setting that you can store in a custom settings file. In cygwin, auto-completion should already be working, but you may need to type (each time you open a terminal and Tcsh):

set filec autocompletes filenames (default in tcsh) using tab key.

set autoexpand autocompletes command names with tab key.

set autolist lists multiple entries starting with same letter(s) (hit tab key a second time).

set notify prints update messages in real time as opposed to the next time you enter a command.

set nobeep you may wish to turn off the annoying beep during auto-completion

Let's test autocomplete. Type da and hit tab. You should see a list of all program commands beginning with the letters da. Add the letter t and hit tab. By supplying 'dat' it should autocomplete to 'date ' and then you can hit enter. Autocomplete will become very useful for working with files and folders. By now you've probably mistyped a command and gotten an error. Using autocomplete is a way to ensure you got the command or file name correct, and generated the correct output, without constantly having to list the new folder contents.

Just as in a Finder or Windows Explorer file browser for viewing folders in your OS, when you open a terminal you start in your default home directory for your account. In Windows you change to a folder by clicking on it. In Unix one changes the current folder using the cd command to change directories. Unless you specify otherwise using the full directory path, Unix looks for programs and files only in the current directory. You can check your current folder at any time by entering:

pwd print working directory

Unix prompts will often show you the current directory for convenience.

ls list files and folders at the level of your current directory

Another common way to navigate directories is to go up one level to what is called the parent directory (the folder in which your current directory resides). On your Folder Browser this may be called "Back".

cd .. changes up one level to the parent directory (..) of your current folder

pwd

ls

cd ~ the tilde key is in the upper left of your keyboard

cd day1 goes down one subdirectory, inside day1

cd typing cd without an argument brings you to your user home directory. Alternatively we could have typed:

cd ~ tilde is the Unix shortcut to/for your home directory

pwd


Directories can be strung together using forward slash (/). This is known as the path.

cd ../.. goes up two directory levels

pwd

ls find the name of your home directory (usually your username) and enter:

cd username/day1 goes down two directory levels

pwd


Now let's try to find the master directory of the Unix file system, called root (/). {Question 4}

Mac: cd / Mac's have a traditional Linux root folder at the top level of all directories, simply: /

PC: cd /cygdrive/c Cygwin emulates the Unix filesystem by mounting your C:\ drive at /cygdrive. In Linux, loading a CD or USB drive similarly involves mounting the external device to a folder, called a mount point. Mac drives mount to /Volumes. All files for which you have been given read access permissions will be accessible from within your terminal.

pwd

ls list files and folders at the level of your current directory (in this case the top level of the hard drive)

Let's examine the contents of root. The ls command is more useful if we specify options for the output. Options for Unix commands are specified using one or more command flags, which begin with a dash:

ls -l lists contents in long format (detailed columns). List and its flag use lowercase L, not the number one (1).

drwxr-xr-x 8 root admin 2048 Jan 6 2014 Users

drwxrwxrwx 3 root admin 2048 Jan 3 19:11 Volumes

drwxr-xr-x 2 root wheel 374 Jul 6 2014 usr


Note: on newer machines with tcsh, you may be able to just enter l as a shortcut for ls and it should color the directories in blue.


The 3rd column shows the owner of each file or directory tree, who in this case is the root administrator. File/folder names are listed in the last column. The 5th field displays the size in bytes of files, but is meaningless for directories. Fields 6-8 display the last time the file was modified.


You'll notice the columns all line up nicely--this is because Unix terminals are in monospaced font (each character is the same width). Monospaced fonts are useful for writing programs because blank spaces are important in interpreting the command syntax.


Since you don't have write access to the master root directory, go back to your default user home directory by simply entering cd with no arguments:

cd switches to your home directory

pwd

ls -l

ls -l day1 subdirectories can also be supplied as an argument to the ls command

There are many -flags that can be supplied to a given command. The man page program displays them for a given command:

man ls displays manual page for command

Hit the spacebar a few times to scroll down thru the options. When finished, tap q to quit the man page program and return to the Unix prompt.

Flags can be combined together, e.g.:

cd /

ls -lr -r flag reverses the sort order

ls -lrt displays most recently modified files last

ls -a -a displays all hidden system files (names begin with dot (.)

ls -a ~ tilde (~ key in the far upper left of the keyboard) is a shortcut to/for your home directory

You can make shortcuts for commands you use often:

alias l 'ls -lFo' the flags Fo or FG (Mac) should color the files/directories (ls -C in cygwin)

Try it out:

l (lowercase L)

QUESTION 4: List the contents of your master root folder (/) and describe how to navigate from root to your home directory.

Proceed to: Part 3