Introduction

Terminal / Shell

You might find it much simpler to work in a terminal (or shell) instead of a graphical user interface when working on code. While it takes away some convenient features, it gives you much more control of what is going on. There are various terminals available under Linux from the gnome terminal, to the Konsole Terminal for KDE and the xterm. Useful commands for the terminal:

  • 'man COMMAND' - displays detailed help text for each shell command. Use this to find out more about the commands below.

  •  'cd DIR' - change directory to DIR.

  • 'ls DIR'' - lists the contents of a directory. 'ls -la DIR' also lists further details about the files.

  • 'cp A B' - copies file A to file B. 'cp -r A B' copies directory A to B.

  • 'mv A B' - moves/renames files/directory from A to B.

  • 'ln -s A B' - create a symbolic link (due to -s) with the name B to the target A (it is not recommend to create hard links, i.e. dropping -s, in general).

  • 'rm A' - removes files A. 'rm -r A' removes directory A. Adding the -f option ignores nonexistent files and never prompts.

  • 'mkdir DIR' - creates directory DIR.

  • 'cat FILE' - displays contents of files.

  • 'less FILE' - paged display of file.

  • 'sort OUT' - sorts lines of IN alphabetically and stores results in OUT.

  • 'find DIR' - displays all files under the directory DIR. You can specify properties of files you wish to find, e.g. 'find DIR -name "*.java"' finds only java files.

  • 'grep PATTERN FILE...' - find occurances of PATTERN in the files listed.

  • 'sed' - a very useful stream editor for filtering and transforming text in files. E.g. 'sed -e 's,OLD,NEW,g' OUT' substitutes any occurance of OLD in file IN with NEW in file OUT. Read the manual and look online for various guides on how to use this efficiently to bulk-edit files.

  • 'awk' - a pattern scanning and processing engine for advanced editing of files (sometimes very useful to explore and edit text files).

  • All these commands can take input from STDIN and write to STDOUT. STDOUT can be redirected via > and STDIN via >. The output of one command can become the input of the other command using the pipe symbol |. So 'ls | grep *.java >java_files' stores all .java files in the current directory in the file java_files.

You may find the "Bash Guide for Beginners" and "Bash Programming - Introduction HOW-TO" useful to learn more about the shell.

Note that there is more than one type of shell under UNIX with different syntax. Most of you will probably be using bash, the Bourne-Again) Shell, but there is also, e.g., the C shell (tcsh).

Editors

There is a variety of editors available under Linux. For programming we recommend either
  • sublime
  • kate
  • emacs
  • vi / vim.

You may also want to use an IDE, even if most of these are too heavy for the tasks of this course. We recommend to use

  • netbeans
  • code::blocks
  • pycharm.

Build [Java]

To compile your java code under Linux simply run
  •  'javac FILE.java' and it will generate the class files, etc. To shortcut this you can also compile all .java files with * 'javac *.java'

To execute the file run

  •  'java CLASS'

You can automate the build process using a Makefile and the make command. For this generate a file called "Makefile" like the following:

# Makefile - Simple java makefile

# Define java compiler and flags and how to compile .java files to .class files
JFLAGS = -g
#JFLAGS = -g:none
JC = javac
.SUFFIXES: .java .class
.java.class:
  $(JC) $(JFLAGS) $*.java

# Define delete command
RM = rm -f

# A list of the relevant java sources to be compiled
CLASSES = \
  Foo.java \
  Bar.java

# Default make target
.DEFAULT_GOAL := default
default: classes # Defines classes target to compile files in CLASSES classes: $(CLASSES:.java=.class) # Cleanup files generated by compiler .PHONY: clean
clean: $(RM) *.class
Of course you have to replace the files listed under classes with your own files. Also note that the long spaces at the beginning should be a TAB! If this file is in the current directory you can compile the altered java files just by typing "make". You can remove the generated class files with "make clean".

Last modified: Friday, 30 October 2015, 4:02 PM