Command Line Interface

The Command Line Interface (CLI) is the terminal that you use to interact with the system. Most of the system are Unix/Linux/BSD/OSX-based and knowing the basics will be useful to help you navigate the system.


Moving around

To find out where you are, use the print working directory command:

$ pwd

List the content of a directory:

$ ls

Other arguments such as -l can be included after the ls command to produce a different output:

$ ls -l

The available arguments can be viewed via the manual man command, for example:

$ man ls

To view the content of a different directory, an absolute path can be used like this:

$ ls -l /opt/local/bin

Alternatively, viewing the content of a directory can be done using a relative path:

$ ls -l ./Scratch

The ./ returns the current path that you are in while ../ returns the path of the parent directory:

$ ls -l ../

To move around, use the change directory command:

$ cd ./Scratch

Likewise, to move back to the parent directory:

$ cd ../

Playing with files and directories

Here are some simple commands that can be used to with files. If you see a file that you are unfamiliar with, the following command returns filetype:

$ file filename

The content of the file is printed on the terminal via the concatenate command:

$ cat filename

Similarly, the more powerful less command can be used that helps paginate the file:

$ less filename

Here are some frequently used command in less:

Commonly used less commands
Command Action
Up Move down one line
Down Move down one line
Page Up Move one page forward
Page Down Move one page backward
/ Search file for word
n Repeat search
h Display help
q Quit

Files can be duplicated using the copy command. This copies files in the same directory:

$ cp filename1 filename2

The following will do the same above but the file is copied into a directory:

$ cp filename1 ./directory/filename2

To move or rename files:

$ mv filename1 filename2

Similarly, moving files into a different directory:

$ mv filename1 ./directory/filename2

If you need to delete a file, make sure the is the right file as you cannot recover it after it is removed:

$ rm filename

Directories are treated like files. To make a directory use the following command:

$ mkdir directory

Directories can be moved or renamed:

$ mv directory1 directory2

copied using the -r option:

$ cp -r directory1 directory2

or removed using the -r option:

$ rm -r directory

Tab is your friend

Auto-completion in the command line helps reduce the amount of things you need to type. The Tab key auto-magically completes the commands or filenames that starts with the first few characters you typed. If you have a file that is named readme.txt:

$ cat rea

and press Tab will complete the remaining filename. Tab can also be used to auto-complete directory names.


Wildcards

There are ways in the CLI that allows you to simplify many repetitive commands. The * wildcard matches everything. So typing the command:

$ ls abc*

will list all filenames and directories in your current path that starts with abc.

The ? wildcard is more specific:

$ rm junk.???

deletes all filenames with junk. and that has 3 characters after that.

Another useful wildcard is [range]. For example:

$ ls abc[0-9][0-9]

will list all files and directories in your current path that starts with abc and has 2 digits after it.

The following lists all files and directories whose name starts with either ab or cd followed by a single character between f and h:

$ ls {ab,cd}[f-h]

The inverse ! command can be used to exclude names that you do not want:

$ rm *[!.txt]

deletes all files except those that ends with .txt.


Redirection

Outputs from any commands can be redirected to a file. For example, the following redirects the output of a directory listing into a file:

$ ls -l > dir_list.txt

However, each time the > command is used, it creates or replaces any files with the same filename. To append to the end of an existing file:

$ cat filename1 >> filename2

copies the contents of the first file into the end of the second file.


Piping

Piping is a powerful command that allows you to do several things all at once. For example, if the directory you are listing is very long (multiple pages), piping it with less will allow you to scroll the output on the screen:

$ ls | less

Other useful examples of piping is to search a certain word in a file using the grep command:

$ cat namelist.txt | grep John

You can also output the results of the grep search into a file:

$ cat namelist.txt | grap John > nameswithjohn.txt

Secure Shell

The Secure Shell (SSH) allows communication and data transfer between secure network services. It provides a service for users to log into remote systems.


Making a connection

To connect to a remote server via SSH:

$ ssh username@server

It will prompt you for your password. If everything works well, you will be able to access the CLI as if you are on your local system.


Logging out

To log out of the remote system, type the following command in an active SSH connection:

$ logout

Alternatively, you can press ctrl-d to exit the secure shell.


Executing programs remotely

You can compile and execute, for example, a C++ program on a remote server. However, if your program takes a while to finish, say it takes a day or two, closing the terminal or logging out of the CLI will automatically stop all the processes that are currently running. To prevent your program from stopping when you logout, you can execute the program in the background via the & command:

$ ./program_name &

Important You need to periodically check on your background running programs as they may encounter a bug and ended up not doing anything while hogging precious system resources.

The following command displays and updates sorted information about the current processes:

$ top

See man top to see how the information can be sorted.


Copying files

Files can be copied using the scp command in SSH:

$ scp source_file username@server:~/path/to/target_file

The above command works both ways for copying files from the local system to the remote server and vice-versa.

Introduction to Git

Git is a Version Control System (VCS) that records changes to a file or a set of fiels over time which allows you to recall a specific version at a later time. A VCS allows files to be reverted back to a previous state, compare changes over time and see what is modified. It also means that you can recover what is lost easily.

A good online Git resource is available on the Git's website


Creating a local repository

To create a local Git repository, you need to go to the projects's directory and initialise it:

$ cd project_name
$ git init

It creates a .git directory where it keeps a record of all the changes made to the repository

To start keep track of a file in your repository, you will need to add it to Git followed by a commit and a message to describe the changes. For example:

$ git add *.cpp
$ git add *.h
$ git commit -m 'Initial commit'

Subsequent changes to the project can be added to the local Git repository by using the git add and git commit -m 'message' command.


Cloning an existing repository

If you are working on a project that already has a Git repository, you can get a copy of it by cloning it. If the project with the Git repository is available locally:

$ git clone path/to/project_name

If it is available on the server and you are the owner of the Git repository:

$ git clone ssh://username@server/~/Repository/location/to/project_name.git

however, if you are cloning it from someone else:

$ git clone ssh://username@server/~/../username/Repository/location/to/project_name.git

Simple Git workflow

To check the status of the Git repository to see if you have a file that is staged (tracked and modified) or one that is untracked by the repository:

$ git status

Untracked files just means that Git will not include it in the repository. To stage or track a file:

$ git add filename

Most of the time, you like to track and stage all of the files. They can be added by using:

$ git add .

When you are ready to commit:

$ git commit -m 'message'

Alternatively, you can combine both of the above git add and git commit command using:

$ git commit -a -m 'message'

Removing files

It is sometimes necessary to delete a file and you do not want Git to track it anymore. First simply remove the file and then stage the file's removal:

$ rm filename
$ git rm filename

If you do not wish to delete the file but just do want it to be tracked by Git:

$ git rm --cached filename

Moving or renaming files

If you want to rename or move a file in Git, you can do the following commands:

$ mv filename1 filename2
$ git rm filename
$ git add filename2

Alternatively, Git does all of the above via:

$ git mv filename1 filename2

Viewing commit histories

After several commits, you may want to look back at a project's history:

$ git log

A useful way to display what has changed between the different versions, the -p command is used together with -2, which limits the output to only the last 2 commits:

$ git log -p -2

If you want to view the commits for the last 2 weeks, for example:

$ git log --since=2.weeks

Changing the latest commit

One of the common mistake that often happens is when you commit too early and have either left out files you wanted to add or may wish to rewrite the commit message:

$ git commit --amend

reverts back to your previous commit message and:

$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend

allows you to add your file as if it was done on the first commit.

Git repository

The remote Git repository server allows anyone within the group to share their work and collaborate with others. The centrally hosted repository readily enables anyone to clone their work onto whichever server or workstation that they are working on.


On the server

You can set up an empty repository on the server by first logging in and creating a git project directory:

$ ssh username@server
$ cd ./Repository
$ mkdir project_name.git

Use git init to create a new repository with the --bareoption, which initialises the repository without a working directory:

$ cd project_name.git
$ git init --bare

Give a description of the project that you are placing in the central repository by modifying the description file.

$ echo "Write something short to describes the project" > description

Any project that you create in the Repository directory will be displayed in the Git repository.


On the client

Info Make sure you have created a GIT repository on your local system beforehand.

On your local system, locate the Git project that you like to push onto the server. If you have not already add the address of the server into the project, do the following to check:

$ cd project_name
$ git remote --verbose

If nothing comes up, do the following:

$ git remote add origin ssh://username@server/~/Repository/path/to/project_name.git

When the above address is set, to push changes from the local checked-out branch to the origin remote master branch:

git push origin master

To push a new branch to the server:

git push origin branch_name

Note that if you do not explicitly push your local branches, they stay in your repository and are invisible to others. git push by default pushes only those branches that already exist on the server. This is why you have to explicitly git push origin master the first time you push to an empty central repository. In contrast, git pull will create local copies of any new branches on the server.

To pull:

git pull origin

will pull changes from the origin remote to the local checked-out branch.


Renaming and removing remotes

The name of the remote can be changed via:

git remote rename name1 name2

while if you wish to remove it, perhaps because the name of the server has changed, do the following:

git remote remove name

C++ Compiler

Each of the server has a set of compilers that you can use to compile your source code. Most of the servers will have support for C, C++, Objective-C, Fortran and Java.


Compile

To compile your source code, call the appropriate compiler:

C++ compilers for the following servers/workstations
Server Command
Blossom
$ clang++ -o executable_filename source_filename
$ g++-mp-4.8 -o executable_filename source_filename

Info The g++ compiler points to clang++, i.e. they are the same.

Buttercup
$
Bubbles
$
Scooby
$ gcc -o executable_filename source_filename'
$ scl enable devtoolset-1.1 'g++ -o executable_filename source_filename'

Info The default g++ compiler is version 4.4.6 while the one contained in devtoolset-1.1 is version 4.7.2.

If you are using the Makefile from the Git repository, ensure that the CC and LD variables are set to use the correct compiler and linker. For example set either:

CC = g++
LD = g++

or

CC = g++-mp-4.8
LD = g++-mp-4.8

or

CC = scl enable devtoolset-1.1 'g++'
LD = scl enable devtoolset-1.1 'g++'

to use the appropriate g++ compiler suitable for your needs.


Running your program

To run your program:

$ ./exectable_filename

Intel ® Thread Building Blocks

Intel ® Thread Building Blocks is a C++ template library for task parallelism.


Source code

Ensure that the appropriate TBB libraries are called in #include. For example:

#include "tbb/parallel_for.h"
#include "tbb/concurrent_vector.h"

Environment settings

Before you compile your source code for TBB, you need to source the TBB environment:

Environment settings commands for the following servers
Server Command
Blossom
$ . /opt/local/bin/tbbvars.sh
Buttercup
$
Bubbles
$
Scooby
$

Check if the TBB environment has been loaded:

$ env

Compile

Make sure your Makefile includes the -ltbb linker flag. If you are using the Makefile from the Git repository, ensure that:

LDFLAGS = -ltbb

is present.