Top Banner
QUICK START BASIC LINUX AND G++ COMMANDS Prepared By: Pn. Azura Bt Ishak FTSM UKM BANGI 2009
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ubuntu Manual

QUICK START

BASIC LINUX AND G++ COMMANDS

Prepared By: Pn. Azura Bt Ishak

FTSM UKM

BANGI 2009

Page 2: Ubuntu Manual

Content

1.0 About UBUNTU 1

2.0 Terminal 1

3.0 Basic Linux Commands 3

4.0 G++ Commands 23

Page 3: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 1

1.0 ABOUT UBUNTU 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 Linux commands will be written through UBUNTU's inside the terminal. Step 1: Click Terminal Icon such as below.

Figure 1: User Interface of UBUNTU

Click this Terminal icon

Page 4: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 2

Step 2: The window of terminal will display such as the figure 2 below.

Figure 2: Terminal window

Page 5: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 3

3.0 BASIC LINUX COMMANDS

1) pwd → to find the name of working directory. The example below (/home/pelajar) shows that you're in pelajar directory which it is located inside the home directory

Figure 3: pwd command

Check or view the current directory

Page 6: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 4

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

List and check any files or other directories in the

current directory

Page 7: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 5

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.

Figure 5: File browser in UBUNTU

List and check any files or other directories in the

current directory using file browser

Places Menu

Page 8: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 6

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.

Figure 6: mkdir command

Creating a new directory

Page 9: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 7

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.

Figure 7: Checking the directory using ls command

myLab directory

Check directory of myLab

Page 10: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 8

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

Open myLab directory

Page 11: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 9

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.

Figure 9: Checking the contents of the directory using ls command

Check the files or other directories inside myLab

directory.

No list files and directories proof that the directory of

myLab is empty.

Open myLab directory.

Page 12: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 10

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.

Figure 10: gedit command

Open gedit text editor followed with the file

name of try.cpp

Open myLab directory.

Page 13: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 11

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.

Figure 11: gedit text editor

Page 14: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 12

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.

Figure 12: Simple C++ programming in gedit text editor

Page 15: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 13

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.

Figure 13: Checking the file in the directory using ls command

Check the file of try.cpp

Open myLab directory

Check the files or other directories inside myLab directory.

Open gedit text editor

Page 16: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 14

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.

Figure 14: cat command

Display the content inside the file through the terminal

Page 17: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 15

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.

Figure 15: cp command (copy file)

Copy try.cpp to a new file name try2.cpp

Check the new file name of try2.cpp

Page 18: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 16

(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.

Figure 16: cp command (copy file into directory)

Create new directory of myLab2

Copy try.cpp into myLab2

Open myLab2

Check the file of try.cpp in myLab2 directory

Page 19: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 17

(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.

Figure 17: cp command (copy directory)

Copy myLab2 to a new directory of myLab3

Check the current file name of try.cpp

Check the directory of myLab3

Page 20: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 18

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.

Figure 18: mv command (rename file)

Check the current file name of try.cpp

Change the name file of try.cpp to test.cpp

Check the new file name of test.cpp

Page 21: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 19

(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.

Figure 19: mv command (move file into directory)

Move test.cpp & try.cpp into myLab3

Open myLab3

Check test.cpp & try.cpp are inside myLab3

Check test.cpp & try.cpp are outside myLab3

Page 22: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 20

(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.

Figure 20: mv command (move directory into another directory)

Move myLab3 into myLab2

Open myLab2

Check myLab3 is inside myLab2

current directories; myLab2 & myLab3

Page 23: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 21

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.

Figure 21: rm command (delete file)

Delete try.cpp & test.cpp

Open myLab3

Check try.cpp and test.cpp have been deleted or not.

Page 24: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 22

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.

Figure 22: rm command (delete directory)

Delete myLab3

Open myLab2

Check myLab3 inside myLab2

Check myLab3 has been deleted or not

Page 25: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 23

3.0 G++ COMMANDS : COMPILING AND EXECUTING FILE OF C++

PROGRAMMING

1) g++ → to compile C++ programming file (g++ File.cpp) 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.

Figure 23: g++ command

compile

error

Page 26: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 24

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.

Figure 24: Edit the C++ programming in gedit text editor

add symbol ;

Page 27: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 25

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.

Figure 25: ./a.out command

execute

compile

output

Page 28: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 26

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.

Figure 26: main.cpp → contains main() and this file depends on statement.h

header file

Page 29: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 27

Figure 27: statement.cpp → implementation for class and this file depends on statement.h

header file

Page 30: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 28

Figure 28: statement.h → header file for class

Page 31: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 29

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.

Figure 29: makefile script

Use ‘Tab’ key

Use ‘Tab’ key

Use ‘Tab’ key

Use ‘Tab’ key

Page 32: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 30

Step 3: Open the terminal, and start compiling (make –f makefile)

Figure 30: make –f command

Compiling makefile

No syntax

errors

Page 33: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 31

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.

Figure 31: Checking the objects and execute files using ls command

Execute file

Object file

Object file

Page 34: Ubuntu Manual

Quick Start Basic Linux and G++Commands – Prepared by Pn. Azura Ishak Page 32

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.

Figure 32: Execute command

Output

Execute command

Execute file