Unix Command Line / Vi Text Editor

Tutorial 1, © Ron Hills

Learning Objective: gain a working knowledge of using basic UNIX and VI commands listed on the page: [Command Reference]. Create and edit text files, manage files and organize directories, and use command syntax structure on the command-line interface.


Key Terms: passed command arguments, command flags; bit/byte, binary encoding, ASCII text; file I/O; relative/absolute pathnames; access permissions; output redirection; Vi editor.


Homework:

  • Discussion Questions are assigned that ask you to interpret the output of commands you are typing at key points in the tutorial. In your lab report, explain the context of each question and major problems you were trying to address in a complete paragraph.

  • Write a 3-paragraph executive summary of your results. Explain the key terms listed below, any problems you may have encountered on your computer, and/or specific techniques/workarounds that you found useful.

Focus Question: What is the Unix command line useful for? What are common pitfalls associated with the command syntax?

Part 1a. Setup for Mac Users (requires 0 Mb of free space)

Go to your Applications folder and then the Utilities subfolder.

Open Terminal.app

After a second and your terminal will display the Unix prompt symbol ($, %, > etc.) to show it's ready to accept commands to be input. Some computers will show your computer name: current folder or file path in front of the $/% symbol prompt. Enter the commands in bold below to get started. The pwd command will tell you what folder your terminal starts in. Remember to hit the return key to submit each command line..

pwd return prints working directory (starts in your user home folder).

ls ls command lists the directory contents.

mkdir day1 mkdir makes a new directory (folder).

cd day1 cd changes the current working directory of your terminal down to the nested folder.

pwd confirm that you changed folders

echo Hi the echo command prints the supplied characters to the screen.

echo hello > file.txt the > sign redirects the output into a file.

ls if the previous command worked, file.txt will be created.

cat file.txt the cat command print the contents of the specified file to the screen.

open . type a space before the period (.)

On macs, the open command can be used to open common files. Here the dot (.) is a shortcut to your current directory, telling the Finder file browser application to open your folder. You can open Finder at any time by clicking on your desktop and then holding down Command-N.

Why do macs have a Unix terminal? When Steve Jobs was rehired by Apple in 1997, he brought Unix to the version 10.0 Mac operating system and named it Mac OS X. Ever since, the mac operating system (OS) has been a variant of Unix. You're already familiar with clicking on files in your browser. Instead of using a mouse, in this course we will be entering commands into the terminal to interact with text files and programs. Invented in 1969 by AT&T Bell Labs, the original Unix operating system and client–server program model were essential elements in the development of the Internet and the reshaping of computing as centered in networks rather than individual computers. The master control program in Unix is the kernel, which manages communication between software applications and the actual data processing performed at the hardware level. On the front end, the Unix environment consists of the Unix shell: a command-line interpreter that gives the user access to a large set of free programs that come with most variants of Unix or Linux (Red Hat, Ubuntu, etc.).

Programmers who write their own programs will also install an add-on called a Developer Tool Kit. If you are a registered Apple Developer you can install the kit, named Xcode, which gives you access to the GNU compiler collection (GCC). A compiler is a program that is needed to compile your own code into a binary executable that can actually be run on a computer. Because it incorporates iOS for iPhone apps, Xcode takes up 10 Gb of disk space, so we'll skip it. The Gnu C compiler ships with Linux but not Macs and PCs, so you will have to log in remotely to my machine to use it.


Part 1b. Cygwin installation for PCs (requires >400 Mb of free space)


Identify your computer as an older 32-bit or a modern 64-bit processor by opening This PC in File Explorer and click on System properties. Identify the free space on your hard disk by hovering the mouse over the C: drive (or right-click and select properties).

Go to the cygwin.com website and download the 64-bit installer for your system: setup-x86_64.exe

On your desktop, double-click the setup.exe installer file and click Run. Read the notes while clicking the following sequence of buttons: Next, Install from Internet, C:\cygwin64, Next, Next, and Next.


Choose a Download Site

The installation program shows a list of web servers (http addresses) to download from. Click in the white box to highlight a mirror site entry. Use which ever one starts right up, but the .edu sites in the U.S. are more reliable (e.g. gatech.edu). Click Next and it should quickly download the list of all current packages available. Note: if this step is slow, go back one step and choose a different mirror.

Select Packages

Groups of packages should now be listed alphabetically by category. We need to add a few subpackages to the ones that are selected by default (default install is only 130 Mb).

To go inside the following packages by clicking on the plus [+] next to each category to expand the list of subpackages. When you find your program, click on the the drop down arrow to the right and select the first program version number you are installing. An X should appear in the binary executable (bin) column but not the source code (src) column, meaning you'll be able to execute but not edit the program.

Single-click on the [+] sign to expand each package (starting with category Devel) and select the following subpackages (listed alphabetically in the Package column):

[+] Devel: Default

Scroll down or search 'gcc' to find the Package entry beginning with: 'gcc-g++'. For this entry, under the New column, click on the drop down menu and select the OLDEST version (the new version often has bugs). Make sure the same version is selected for gcc-core. You can now go up to [-] Devel and click the minus sign to collapse the Developer package directory.

[+] Editors: Default

Click the plus next to Editors and scroll down to the package entry beginning with a "v": '1466k vim: Vi IMproved - enhanced vi editor'

For the "vim" row, click the drop down arrow and select the most recent numerical version/subversion.

[+] Python: Default

Scroll down or search the entry 'python39: Py3K language interpreter' and select version 3.9.10. We prefer to use python 3, which has become the industry standard, but our code is also compatible with the much older python 2. Also select the python packages python39-ipython and python39-numpy.

[+] Net: Default

Scroll to 'openssh' and select the newest version to install the binary.

[+] Shells: Default

Scroll to 'tcsh' and select the newest version to install the binary.


Once your subpackages above have been selected, click Next to install them. The installer now shows a list of other packages that the programs you have selected depend on, called dependencies. Click next again to install these by default.

Wait for all green bars to complete the download and then install each individual package. If you selected the right packages it should take ~15 minutes to install. Once the installation has finished choose create icons on your Desktop by checking the two boxes and select Finish.

Open a File Explorer and hold your mouse over the C:\cygwin64 folder. It should show a sizable chunk of disk space being used (>400 megabytes, MB). Double-click the cygwin64 folder to open it. If the download created a 100 Mb temporary folder named 'http...', click to highlight the http... folder and hit the delete key on your keyboard (not backspace). Do the same for the http.. folder in your Downloads folder. Lastly, find where you stored the original setup.exe installer file (C:\Downloads). Drag it into C:\cygwin64. You will use setup.exe any time you need to re-install or uninstall a package.


Notes:

  • If installation takes more than two hours you may have installed all packages by mistake (10 Gb!).

  • You might get an error message saying one of the packages has a "dependency", meaning it requires another program to be installed. Take note of the error message in case it causes a problem later. Then just click OK so that the packages finish installing.


Part 1c. Verify Installation (Macs and PCs)


Double click the Cygwin/Terminal icon on your Desktop to open a new Unix terminal window. Like your file browser, you can open more than one terminal and each starts by opening your home directory (Mac: /Users/username; Cygwin: /home/user). We will check your install to test whether individual commands (programs) were installed. Once the Unix prompt (symbol varies between Linux systems: $, %, >) loads in your terminal, type which followed by the program name. Always hit the enter key (return key on Macs) to submit a command:

which vim enter

The which Unix command is a program that searches your system folders for any program files with the name you gave as a command argument. Most Unix commands can accept one or more command arguments on the command line (separated by spaces). An argument passed to a command or program can be a file, text string, or number.

If the Vim program was installed on your computer you will see an output like:

/usr/bin/vim

This means that the Vim program is stored inside your system's bin folder for binary executable programs. The bin directory is itself nested inside the system usr folder. On a PC, cygwin emulates the UNIX filesystem by actually placing these folders inside your C:\cygwin\home\username folder. Any files we create in Terminal will be available in your File Browser.


Check that the other programs were installed:

which ssh csh python awk tar g++ {Question 1}


g++ is Gnu's C++ compiler. It used to be much more difficult to compile in Windows. We can check for it in Cygwin by entering:

(PC only) g++


The g++ compiler expects a code file supplied as an argument, so if g++ runs it will complain with the error:

g++: fatal error: no input files

The g++: signifies that an instance of the program was run, called a process. fatal error: signifies that the program encountered an error which caused this process to terminate.


If you have a Mac or forgot to select the g++ package, the Unix shell would complain that it was not able to find the program:

g++: Command not found

Select Not now if it ever asks you to install Xcode.

The PC users still need to try out some commands at the Unix prompt:

mkdir day1 makes (creates) an empty directory, or file folder

cd day1 changes the current working directory down one level, inside the day1 folder

echo Hello World the echo command prints the supplied arguments to the screen

echo Hello > file.txt the > symbol redirects the output into a (new) file

ls the list (ls) command displays the files in your current directory (the terminal starts out in your home folder)

cat file.txt the concatenate (cat) command prints the contents of the specified file(s) to the screen

Now, let's open file.txt in a word processor or text editor. Double-click on the following folders and then the file:

PC: My Computer > C: > cygwin64 > home > file

Inside C:/cygwin/home a folder should be showing with your username. Right-click on the folder (hold the control key and click or tap your touchpad with two fingers) and select 'Create shortcut'. Drag the shortcut to your Desktop.

Mac: Macintosh HD > Users > username > day1 > file.txt

PC/Mac: Now we can open file.txt by double-clicking this folder and then double-clicking: file (the .txt file extension may be hidden from your folder view). It should open in your default text viewer.

DISCUSSION QUESTION 1 for lab write-ups: Explain the output of which vim ssh csh python awk tar g++ and the location of your cygwin or Mac home/user directory in which day1 resides:

pwd prints the full path for the current working directory


Add a shortcut to your home directory in Finder by selecting from the menu bar Finder > Preferences... Sidebar and checking the Favorites box with your username. If you don't see a bar like pictured (right), hit option key-command-s.

Proceed to: Part 2