CS 241 Data Organization using C
Lab 1: Introduction to C and the Linux Command Line Environment


August 21, 2018

1 Brief Introduction to Command Line Linux

Before this lab, you will need to have a CS server account. If you do not have one, you will need to visit FEC 3550 with your UNM id to receive one. During lab, you will sit down at a machine and use your CS login information.

1.1 Pop a terminal window

The command Ctrl-Alt T is the keyboard shortcut for opening up a terminal.

Otherwise, you can click on the upper left hand Ubuntu icon and search for terminal.

1.2 Running some basic Linux Commands

Hopefully now you are logged in and have a terminal window open. This will look something like this:

vasek@b146-76:~$

This line is called the command prompt or simply prompt. In Linux, the equivalant to “My Documents” of Windows is called your home directory.

1.3 Common linux commands

You can look up many more commands on this helpful webpage: http://www.pixelbeat.org/cmdline.html.

1.4 Creating Directories

First you should create a cs241 directory which will contain all the files you create during this semester. Then create a lab directory inside this to contain all of the code you write during lab. Inside your lab directory, create a subdirectory named lab1. Make this folder your working directory. Here is how to do this:

At the end of this subsection, verify that you have all three directories and that they are properly nested. What command executed from what directory can verify this for you?

For each new lab, you will need to create a new lab folder under your lab directory.

1.5 Editing text files

You can use emacs or vim, whichever you prefer. (You can even use one of the other editors available in Linux, but those are the two big ones.)

Some helpful emacs commands

When you’re logged into a Linux machine, you can open up emacs in a similar way that you found a terminal window – by clicking on the ubuntu icon on the upper left hand toolbar, and then searching for emacs.

You can also open up emacs from a terminal window. Type the command emacs & which will open up a new window with an emacs buffer. What happens when you forget the &?

1.6 Copying, Renaming, Moving, and Removing Files

The Linux commands to copy, move and delete (remove) a file are:

For example: cp helloWorld.c myBackupCopy.c will create a copy of helloWorld.c with the name myBackupCopy.c.

2 Problem Specification

This lab is designed to help you familiarize you with C as well as the Linux environment.

2.1 Introduction

First, make sure that your current working directory is your lab1 folder. Next, you will need to copy the files from the server to your local machine. To do this, use a command wget – a program for downloading files. wget takes in one argument, the website or file to be downloaded. You can use the following command to do that.

wget https://www.cs.unm.edu/~vasek/cs241/lab/intro.c

Then, compile and run this program using the following commands:

gcc intro.c  
./a.out

What does this program do?

Edit the intro program with your own details, using either emacs or vim. Then save, compile, and rerun the program. Next, add a couple additional print statements. What happens if you remove the /n?

Redirect the output of your program to a textfile using the following command:

./a.out > output.txt

Create a copy of output.txt and name it output2.txt. Add a few spaces and some extra new lines to the file.

Check to see which lines you’ve changed using the linux command diff. diff compares two files and outputs the steps to turn one file into another. Read the man page for diff. (Remember that Linux command?) What diff command gives you the most feedback as to which lines have changed? How easy is it for you to tell which lines you changed and which lines are the same?

2.2 Testing your diff abilities

Download test1.txt and test2.txt from the lab folder on the course website.

Write a single linux comand to highlight all the changes in the two text files. Then, in English in a text file called diff.txt, include your command and describe how to turn test1.txt into test2.txt.

2.3 More time?

There are a couple useful command to find things: find and grep. find walks a file hierarchy whereas grep looks for content within a file.

In a textfile called find.txt, write the following commands using find and/or grep:

Be sure to create enough testing files for this.

3 Turning in your assignment

You are now done with your assignment. Please zip up your lab1 files, naming it yourcsusername.zip, and drop it into the CS 241 dropbox. Your lab1 files should contain your edited intro.c, diff.txt, and find.txt, if you had the extra time.

zip -r yourcsusername *.c *.txt