You are on page 1of 6



Linux commands

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 4GB USB-stick, including OpenFOAM and some other OpenSource software. See further instructions. Another alternative is to install Linux on a part of your computer, keeping your previous operating system intact. Further instructions will be available here. 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 ssh -XY ssh -XY ssh -XY ssh -XY 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 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.


Linux commands


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!) List the files in a directory with extended information List all files, including hidden files (files with names starting with a dot, for example .bashrc) Make a directory named linuxTutorial. Note that Linux is case-sensitive, i.e. 'linuxTutorials' is different from 'linuxtutorials'. Move into the directory named linuxTutorial Print "Hello World" in the terminal window 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. 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. Copy the file (check with ls!) remove the original file (check with ls!) Rename the new file to the name of the original file (check with ls!) Make a directory named aSecondDirectory inside the linuxTutorial directory (check with ls!) Move inside aSecondDirectory Move up one directory, in this case to the directory named linuxTutorial Remove the empty directory named aSecondDirectory

ls -l ls -a

mkdir linuxTutorial

cd linuxTutorial echo "Hello World!" echo "A first text line in my file" > myFile.txt

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

cp myFile.txt copyOfMyFime.txt rm myFile.txt mv copyOfMyFime.txt myFile.txt mkdir aSecondDirectory

cd aSecondDirectory cd .. rmdir aSecondDirectory

mkdir -p aSecondDirectory/aThirdDirectory/aFourthDirectory Create this directory structure inside the linuxTutorial directory 2/6


Linux commands

cp myFile.txt aSecondDirectory/aThirdDirectory/aFourthDirectory ls aSecondDirectory/aThirdDirectory/aFourthDirectory cd aSecondDirectory/aThirdDirectory/aFourthDirectory pwd cd ../../..

Copy the file to the directory named aFourthDirectory List the files in the directory named aFourthDirectory Go to the directory named aFourthDirectory Show your current directory path Move up three directory levels (you should now be in the linuxTutorial directory (check with pwd) Print out directory structure in the terminal window Print out only two levels of the directory structure Print out only the directories in the directory structure View the contents of myFile.txt. If the file is large you exit with 'q', and move down in the file by pressing 'Enter' View the beginning myFile.txt (in this case it shows all of the file since it is small). View the end of myFile.txt (in this case it shows all of the file since it is small). View the end of a file and update when lines are added to the file. Exit with 'CTRL-c' Same as 'tail -f', but can be faster. Exit with 'CTRL-c' Search for the string "second text line" in myFile.txt, and print out all lines that match Search for the string "first text line" recursively in the directory named aSecondDirectory 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. Substitute the string "second text line" in myFile.txt with "modified text". Use your Linux skills to check that it was done! The

tree -L 3 tree -L 2 tree -d more myFile.txt

head myFile.txt tail myFile.txt tail -f myFile.txt

tailf myFile.txt grep "second text line" myFile.txt grep -r "first text line" aSecondDirectory

find aSecondDirectory -iname "*yfi*"

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


Linux commands

sed-commands can also be used in the 'vi' editor which will be discussed later. mkdir newDirectory Make a new directory and copy the whole file structure of myFile.txt into that cp --parents directory. Use your Linux knowledge to aSecondDirectory/aThirdDirectory/aFourthDirectory/myFile.txt check out the directory structure, and then newDirectory 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) 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. Create a file without opening it, and without adding anything in it. Removes the linuxTutorial directory. Note that we here do two commands at the same line, first a cd, and then an rm! close connection (terminal window)

ln -s myFile.txt softLinkToMyFile.txt

touch touchedFile.txt cd ; rm -r linuxTutorial


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. List background jobs in the current terminal window. 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

jobs ps -ef | grep xlogo


Linux commands

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 CTRL-c xlogo CTRL-z bg top Put a process in foreground, in this case the xlogo process Kill a process in foreground, in this case the xlogo process 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. 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 a job in background. Find the process id (PID) from the ps command, or the top command. If the 'kill' command doesn't work, try to add the -9 flag, which should force the kill. 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.

kill <PID> Kill -9 <PID> which xlogo

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: 5/6


Linux commands

Other alternaties are: emacs gedit nedit

Linux Filesystem Hierarchy Standard

If you are interested in why the file system looks as it does, see: