Beruflich Dokumente
Kultur Dokumente
Prepared By:
Pn. Azura Bt Ishak
FTSM UKM
BANGI
2009
Content
2.0 Terminal 1
Ubuntu is a community developed, Linux-based operating system that is perfect for laptops, desktops and
servers. It contains all the applications you need - a web browser, presentation, document and spreadsheet
software, instant messaging and much more. (referred in http://www.ubuntu.com/)
2.0 TERMINAL
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 1
Step 2: The window of terminal will display such as the figure 2 below.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 2
3.0 BASIC LINUX COMMANDS
The example below (/home/pelajar) shows that you're in pelajar directory which it is located inside
the home directory
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 3
2) ls → to list the files and directories
You can see the list of files and directories inside pelajar directory or folder. The files must be
followed by the format (eg. book.make, lab.cpp), meanwhile any directory or folder didn't have any
format (eg. Desktop, Documents).
Figure 4: ls command
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 4
The same thing you can view directories and files in graphic based if you click places menu and
click icon Home Folder such as below. You can see the directories seem like in the booklet based
meanwhile files seem like a single paper. This graphical based is more easier compare to command
based BUT since this lesson is about commands, so we'll focus on that particular topic.
Places Menu
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 5
3) mkdir → to create a directory (mkdir Directory)
Step1: Let's try creating your own directory. In the following example, the name of directory is
myLab. You have to make sure the name of directory doesn’t have any format such as try.cpp,
pwd.png or others. And please AVOID any spacing of the directory name such as my Lab. If you
still want to separate two wording together, you may put underscore such as my_Lab.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 6
Step 2: Now, let’s check either the directory of myLab is already inside pelajar directory by typing
ls command. In the following figure 7, you can see that myLab directory is already there. It
means your directory has been successful built.
myLab directory
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 7
4) cd → to change your working directory (cd Directory)
Step 1: If you want to store any files or directories in your own directory, you should open first the
directory. It seems like if you want to put something in the drawer, for sure you should open first
the drawer, the you can put anything inside the drawer. In the figure 8 below, we try to open the
directory of myLab by using command cd. If you want to open the previous directory, you can just
typing cd command without following by the name of directory.
Figure 8: cd command
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 8
Step 2: Now, let’s check either this directory have any files or other directories by using command
ls. Logically, since this is a new directory, so for sure it is empty right??? You can see there is
nothing display such as the figure 9 below.
Open myLab
directory.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 9
5) gedit → text editor (gedit File.cpp)
Step1: Open any text editor such as gedit followed by the file name of try.cpp which the format of
*.cpp is a C++ format file. You can also use pico text editor. In this example, we will use gedit
text editor because its more colorful text based compare to pico.
Open myLab
directory.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 10
Step2: Gedit text editor will display such as the figure 11 below. You can type any text including
C++ programming in this gedit text editor.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 11
Step 3: Now, let’s try typing any simple coding of C++ such as below. Click save once you finish
typing the coding and close the file for the moment.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 12
Step 4: Go back to the terminal, and check the file of try.cpp by using command ls. You can see
the file will display in the terminal such as figure 13.
Open myLab
directory
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 13
6) cat → to view a file's content thorough the terminal (cat File.cpp)
You can see the file's content will display directly through the terminal without opening the file
using gedit text editor. But you may NOT edit the contents at all. The cat command just want to
help user to identify, recheck or reconfirm the contents faster and easier of the specific file.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 14
7) (a) cp→ to copy file (cp currentFile.cpp newFile.cpp)
You may copy from the current file try.cpp to a new file name try2.cpp. Then, let’s check the file of
try2.cpp whether it's already existed or not by using ls command.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 15
(b) cp→ to copy file into directory (cp File.cpp Directory)
You may copy file of try.cpp and put inside the specific directory myLab2. You should create first a
new directory of myLab2 using the previous command mkdir. Open the new directory of myLab2
by using cd command and type ls command to check whether the file of try.cpp is already existed or
not.
Open myLab2
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 16
(c) cp→ to copy directory (cp -R currentDirectory newDirectory)
You can copy from the current directory myLab2 to a new directory myLab3. If you're in myLab2
directory, make sure go back to the previous directory of pelajar by using cd command without
following any directory name. Then open back myLab directory by typing cd myLab.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 17
8) (a) mv→ to rename file (mv currentFile.cpp newFile.cpp)
You can move or rename any current file name to a new file name. In this example, try to rename
try.cpp to a new file name of test.cpp.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 18
(b) mv→ to move files into the directory (mv File.cpp Directory)
You can move more than one file into the specific directory. In this example, let’s move two
files test.cpp and try2.cpp into the directory of myLab3.
Open myLab3
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 19
(c)mv→ to move directory into another directory (mv Directory1 Directory2)
You also can move the directory into any specific directory. In this example, let’s try to move
myLab3 directory into the myLab2 directory.
current directories;
myLab2 & myLab3
Open myLab2
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 20
9) (a) rm → to delete files (rm File.cpp)
You can delete more than one file using rm command. In this example, let’s try to delete both of
the files try.cpp and test.cpp in myLab3 directory.
Open myLab3
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 21
10) (b) rm → to delete directories (rm -r Directory)
You also can delete more than one directories using rm -r command. In the example below, let’s
try to delete myLab3 directory which it inside myLab2 directory. You can also delete more than one
directory together.
Open myLab2
Delete myLab3
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 22
3.0 G++ COMMANDS : COMPILING AND EXECUTING FILE OF C++
PROGRAMMING
Step1: If the program has any syntax error, you have to identify the error. In the example below,
the program have error in line 6 which the expected symbol of ; was missing.
compile
error
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 23
Step 2: Open the file back in gedit text editor and make some correction on that particular error.
Save the file and compile again using g++ command in the terminal.
add symbol ;
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 24
2) ./a.out → to execute file of C++ programming
If your coding don't have any syntax error, you should execute the file using ./a.out command.
Thus, it will display the output of your program such as the figure 25 below.
compile
execute
output
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 25
3) make -f → to compile several source files of C++ programming (make -f makefile)
Step1: If you want to compile more than one source file of C++ programming, make –f
command is the better solution. In this example, let’s create 3 files in the same directory; main.cpp,
statement.cpp and statement.h such as in figure 26-28.
header file
Figure 26: main.cpp → contains main() and this file depends on statement.h
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 26
header file
Figure 27: statement.cpp → implementation for class and this file depends on statement.h
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 27
Figure 28: statement.h → header file for class
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 28
Step2: Before compiling, you have to create the file of makefile using gedit text editor such as below.
In this example, the name file of makefile is lab. A makefile is basically a script that guides the make
utility to choose the appropriate program files that are to be compiled and linked together. (referred:
http://searchenterpriselinux.techtarget.com/sDefinition/0,,sid39_gci283986,00.html).
To avoid any errors in makefile, please use ‘Tab’ key to space the compiling command such as in the
figure 29. Make sure all the files including makefile must be put into the same directory or folder.
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 29
Step 3: Open the terminal, and start compiling (make –f makefile)
Compiling makefile
No syntax
errors
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 30
Step 4: Type ls command and you will see the 2 objects files (main.o and statement.o) and 1 execute
file (try) will be automatic generated for you.
Execute file
Object file
Object file
Figure 31: Checking the objects and execute files using ls command
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 31
Step 5: Finally, execute the file. (./executeFile)
In the figure 32, the execute file name is try. So, the command should be ./try. You will see the
output of “Welcome to UKM” will be display through the terminal.
Execute file
Execute command
Output
Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 32