A short Linux tutorial for this course

Getting access to Linux

In this course we will use the Linux operating system. You will have access to Linux computers at Chalmers. If you want to work elsewhere, and if you do not have a Linux computer available, you can use a Linux distribution that fits on a 2GB USB-stick, including OpenFOAM-1.5-dev and some other OpenSource software. See further instructions below on how to run OpenFOAM using the SLAX USB. Another alternative is to install Linux on a part on your computer, keeping your previous operating system intact. The Ubuntu Linux distribution (http://www.ubuntu.com/) is one popular example, which includes automatic partitioning of your disk.

If you have access to the student computers at Chalmers (which you will be at the start of the course if you are registered to the course) you can log in to the Linux computers in MT13 or by doing an ssh to one of the following:

ssh -XY remote1.student.chalmers.se

ssh -XY remote2.student.chalmers.se

ssh -XY remote3.student.chalmers.se

ssh -XY remote4.student.chalmers.se

ssh -XY remote5.student.chalmers.se

If you want to do an ssh from a Windows computer, a simple way to do so is to install the OpenSource software Putty at http://www.putty.org/. Graphics through Putty might however be problematic, but that is only needed for post-processing.

Working in linux

In Linux you have the option to work graphically or text based. In this course we will work text based, since that way we have better control of what we are doing (and I am used to working that way).

To start with, you need to open a terminal window. You do that either by right-clicking on the desktop and clicking on 'Terminal' or 'Console', or you should be able to find a button looking like a computer monitor to click on somewhere. Once you have a terminal window open you need to know some useful Linux commands.

Manage files and directories

Here are some useful Linux commands. If you do all the commands in the same order as they are presented below you will go through a small tutorial on how to use these commands:

cd

(or: cd ~)

Move to your home directory. You should be there already if you just opened the terminal.

ls

List the files and directories in a directory (use this command at any time during the tutorial to see what files and directories you have in the current directory!)

ls -l

List the files in a directory with extended information

ls -a

List all files, including hidden files (files with names starting with a dot, for example .bashrc)

mkdir linuxTutorial

Make a directory named linuxTutorial. Note that Linux is case-sensitive, i.e. 'linuxTutorials' is different from 'linuxtutorials'.

cd linuxTutorial

Move into the directory named linuxTutorial

echo "Hello World!"

Print "Hello World" in the terminal window

echo "A first text line in my file" > myFile.txt

Create a file named myFile.txt, and add a line to it saying: "A first text line in my file". The '>' re-directs the output from the echo command to a file instead of to the terminal window.

echo "A second text line in my file" >> myFile.txt

Append the line "A second text line in my file" to myFile.txt. The '>>' appends the output of the echo command to the same file as before. If we had used the '>' we would have overwritten the file instead.

cp myFile.txt copyOfMyFime.txt

Copy the file (check with ls!)

rm myFile.txt

remove the original file (check with ls!)

mv copyOfMyFime.txt myFile.txt

Rename the new file to the name of the original file (check with ls!)

mkdir aSecondDirectory

Make a directory named aSecondDirectory inside the linuxTutorial directory (check with ls!)

cd aSecondDirectory

Move inside aSecondDirectory

cd ..

Move up one directory, in this case to the directory named linuxTutorial

rmdir aSecondDirectory

Remove the empty directory named aSecondDirectory

mkdir -p aSecondDirectory/aThirdDirectory/aFourthDirectory

Create this directory structure inside the linuxTutorial directory

cp myFile.txt aSecondDirectory/aThirdDirectory/aFourthDirectory

Copy the file to the directory named aFourthDirectory

ls aSecondDirectory/aThirdDirectory/aFourthDirectory

List the files in the directory named aFourthDirectory

cd aSecondDirectory/aThirdDirectory/aFourthDirectory

Go to the directory named aFourthDirectory

pwd

Show your current directory path

cd ../../..

Move up three directory levels (you should now be in the linuxTutorial directory (check with pwd)

tree -L 3

Print out directory structure in the terminal window

tree -L 2

Print out only two levels of the directory structure

tree -d

Print out only the directories in the directory structure

more myFile.txt

View the contents of myFile.txt. If the file is large you exit with 'q', and move down in the file by pressing 'Enter'

head myFile.txt

View the beginning myFile.txt (in this case it shows all of the file since it is small).

tail myFile.txt

View the end of myFile.txt (in this case it shows all of the file since it is small).

tail -f myFile.txt

View the end of a file and update when lines are added to the file. Exit with 'CTRL-c'

tailf myFile.txt

Same as 'tail -f', but can be faster. Exit with 'CTRL-c'

grep "second text line" myFile.txt

Search for the string "second text line" in myFile.txt, and print out all lines that match

grep -r "first text line" aSecondDirectory

Search for the string "first text line" recursively in the directory named aSecondDirectory

find aSecondDirectory -iname "*yfi*"

Find all files that have the string 'yfi' in their file name. The string is not case sensitive because we use the flag -iname. The '*' means 'any string'. The command is recursive.

sed -i s/"second text line"/"modified text line"/g myFile.txt

Substitute the string "second text line" in myFile.txt with "modified text". Use your Linux skills to check that it was done! The sed-commands can also be used in the 'vi' editor which will be discussed later.

mkdir newDirectory

cp --parents aSecondDirectory/aThirdDirectory/aFourthDirectory/myFile.txt newDirectory

Make a new directory and copy the whole file structure of myFile.txt into that directory. Use your Linux knowledge to check out the directory structure, and then remove newDirectory.

rm -r aSecondDirectory

remove directory aSecondDirectory and all files and directories in it (rmdir does not work if there are files or directories in a directory)

ln -s myFile.txt softLinkToMyFile.txt

Make a soft link to myFile.txt. The original file will NOT be copied, but the name softLinkToMyFile.txt will point at myFile.txt. If you edit softLinkToMyFile.txt tou will actually edit myFile.txt. However, removing softLinkToMyFile.txt will only remove the link, and not myFile.txt. You can see links using the 'ls -l' command, showing the sign '->', meaning that it points at another file.

touch touchedFile.txt

Create a file without opening it, and without adding anything in it.

cd ; rm -r linuxTutorial

Removes the linuxTutorial directory. Note that we here do two commands at the same line, first a cd, and then an rm!

exit

close connection (terminal window)



Managing processes

Here are some useful Linux commands. If you do all the commands in the same order as they are presented below you will go through a small tutorial on how to use these commands:

xlogo &

Put a job in background when starting it. 'xlogo' is the job we run in this case, and '&' puts it in background, so that you can continue working in the terminal window. Of course, any other process than xlogo can be used.

jobs

List background jobs in the current terminal window.

ps -ef | grep xlogo

List all the processes on the computer containing the string 'xlogo'. The 'ps -ef' command lists all the processes on the computer. The '|' sign sends that output to the 'grep xlogo' command, which makes sure that only lines containing the string 'xlogo' are shown. The first number on each line is the process id (PID).

fg

Put a process in foreground, in this case the xlogo process

CTRL-c

Kill a process in foreground, in this case the xlogo process

xlogo

CTRL-z
bg

Start a job in foreground (i.e., don't use the '&' sign), which will lock the terminal window from further work. The process can be stopped using CTRL-z, but the process will be paused until it is put in background with the 'bg' command.

top

Show the activity of all the processes on the computer. The first number on each line is the process id (PID). Exit by typing 'q'.

kill <PID>

Kill a job in background. Find the process id (PID) from the ps command, or the top command.

Kill -9 <PID>

If the 'kill' command doesn't work, try to add the -9 flag, which should force the kill.

which xlogo

Prints the full path to the xlogo executable, so that you can check that you are running the file you think that you are running.

Other commands

Find other useful Linux commands by doing the following:
info coreutils
info coreutils ls
info coreutils nohup
(exit by typing 'q')
Once you know the name of a command, learn how to use it by:
man command
(exit by typing 'q')

Editing files

For editing files interactively you use a text editor. I commonly use 'vi', which is a VERY simple text editor. The benefits of vi is that it is quick to open files, and it does not open any new window. You do the editing directly in the terminal window. Several of the vi commands can also be used in the man pages and in sed commands. On the downside, there is no graphical user interface. Search the internet for short introductions to vi, for example: http://www.cs.colostate.edu/helpdocs/vi.html

Other alternaties are:
emacs
gedit
nedit

Linux Filesystem Hierarchy Standard

If you are interested in why the file system looks as it does, see:
http://www.pathname.com

The SLAX USB

The SLAX USB includes a full Linux installation, OpenFOAM-1.5-dev, and some more OpenSource software. You can boot directly from a USB stick or DVD so that you do not have to install Linux.

Go to http://sourceforge.net/projects/openfoam-extend/files/, download the OpenFOAM_SLAX.tgz file, and follow the instructions at http://sourceforge.net/project/shownotes.php?release_id=681989. There is also an iso for burning a CD if your computer does not allow booting from USB.