Linux and Vi

If you search the web for ‘linux cheat sheet‘ you find numerous one-page documents that list the most commonly used Linux commands.  I recommend that you print or bookmark one to help you learn them.

Navigating the File System

Some of the most common commands that we’ll use are for listing the files within a directory and moving around the file system.

  • ls
  • ls -l
  • ls -al
  • pwd
  • cd /
  • cd $HOME
  • cd ..
  • cd [dir] (absolute or relative path)

UNIX directory structure

Standard-unix-filesystem-hierarchy

Manipulating Directories and Files

  • mkdir [dir]
  • rmdir [dir]
  • mv [src] [dest]
  • touch [file]
  • cp [orig] [new]
  • cp -r [src_dir] [dest_dir]
  • rm [file]
  • rm -r [dir]    (BE CAREFUL!)
  • chmod [mode] [file]

Displaying File, Manual, Environment and System Information

  • cat [file]
  • man [command]
  • which [command]
  • whereis [command]
  • env
  • whoami
  • hostname
  • date
  • clear

Viewing and Killing Processes

  • ps
  • ps -ax
  • kill [pid]
  • kill -9 [pid]

Pipes

  • [command] | [command]
  • cat filename | less

Vi – The Editor of Champions

  • Insert Mode (i)
    • arrow keys
    • delete
  • Command Mode (esc)
    • x – delete char
    • r [char] – replace char
    • dd – delete line
    • # dd – delete multiple lines
    • u – undo last command
    • yy – copy to clipboard
    • # yy – copy multiple lines
    • p – paste contents of clip board
    • $ – go to the end of a line
    • 0 – go to the beginning of a line
    • shift + G – go to the end of the file
    • shift + H – go to the beginning of the file
    • :# – goto line a particular line
    • :/search_term – search for a particular term
    • :%s/old/new/g – find and replace
    • :w – write the file
    • :q – quit vi
    • :q! – quit without writing
    • shift+z+z – write and quit

Kattis

Kattis is an online service that provides students with programming problems to solve and a system to check if their solutions are correct.  Anyone can create an account, pick a problem, write a solution and submit it to see if it is correct.  The service will respond with a judgment usually within 1 minute.

The service is also used by programing contests and by college faculty for their courses.  I’ve created a Kattis course that includes a set of problems for you to solve during this course.

Registration

The first thing you need to do is create a Kattis account.  You can do so at the following URL:  https://bridgewater.kattis.com/register

Next, you need to register as a student in my CSCI-105 Kattis course by going to https://bridgewater.kattis.com/courses/CSCI-105/Fall2016/register  and entering the Registration Key “CSCI-105”.

Problem Sets

You can view the list of problems for the course here: https://bridgewater.kattis.com/courses/CSCI-105/Fall2016/problems.

You will be given a separate handout specifying when each of the problem groups are due. 

Writing a Solution

When you are working on a solution to a Kattis problem, keep the Kattis Java guidelines in mind.  The guidelines can be found here:  https://open.kattis.com/help/java.

Some things to remember are:

  1. Your program should read its input from standard input and produce output on standard output.
  2. Kattis will inspect the exit code of your program. If it is non-zero, she will judge your submission as Run Time Error. So don’t call exit() with a positive number as the argument.  Always return 0.
  3. Uncaught java exceptions will almost always be judged as Run Time Errors with some extra information.

Submitting a Solution Using the Kattis Submit Script

If you are developing software on cs.bridgewater.edu, you can use the Kattis script, named kattis.py, to submit a solution.  It’s already installed on the machine in a directory that is on everyone’s path.  All you need to do is upload your personal configuration file into your home directory.

Uploading Your Personal Configuration File

Log into Kattis and download your personal configuration file onto your local machine by going to https://bridgewater.kattis.com/download/kattisrc.  Be sure to remember where you saved the file.

Then, using a SFTP client like FileZilla, copy the file from your local machine to your home directory on cs.bridgewater.edu.

Once the file is on cs.bridgewater.edu close your SFTP client and log into cs.bridgewater.edu with your SSH client (e.g. Putty).  Once logged on, change the name of the configuration file to “.kattisrc” using the linux mv command.

mv old-file-name .kattisrc

Your personal configuration file contains a private token.  You must not share this with anyone.  Your home directories have permissions set to 700 which means that you have read, write and execute permissions on your directory and everyone else has no rights what-so-ever.  I insist you keep it that way.

Submitting Your Kattis Solutions

Now you’re ready to use the submit script.  In order to use the script you need to

  1. Be in the directory that contains the source code that you want to submit.
  2. Know the Kattis problem identifier for the problem you are working on.
  3. Know which file your main() function is in.

The Kattis problem id can be found in the upper right corner of the problem’s Kattis web page as shown below.

When you’re ready to submit, simply type the following

kattis.py -p problem-id -m class-name -l Java files

where problem-id is the Kattis problem identifier and class-name is the name of the class that contains your main() function and files is a space separated list of your source code files.

For example, to hand in the problem named different we might enter the following:

kattis.py -p different -m Different -l Java Different.java

After you enter the submit command, you should see a prompt similar to the one below.

Problem: different
Language: Java
Files: Different.java
Mainclass: Different
Submit(y/N)?

If all of the information is correct, type a lower case y and hit ‘Enter’.  You should see a confirmation like the following.

Submission received. Submission ID: 1247718.

Within 1 minute of your submission, you should receive an email with the results.

Submitting a Solution via the Kattis Website

A tutorial on how to submit a solution to Kattis is found here: https://bridgewater.kattis.com/help/submit.

One way to submit a solution is via the Kattis website.  To do so, navigate to the problem description webpage for the problem you are working on.  On that page you will see a submit button in the upper right corner.

When you click on Submit you be directed to a Submit Solution page like the one below.

Here you select the files you wish to upload and specify the name of the class that has your main() function.  After you drag and drop your files and enter your Main class you Submit Solution page should look like the image below.  When done, press Submit.

When you press submit you’ll get a screen that looks like the following.

The “? New” status means that your solution is currently being judged.  In about a minute, you should receive an email from the system with a judgment like the following one.