QUICK START BASIC LINUX AND G++ COMMANDS Prepared By: Pn. Azura Bt Ishak FTSM UKM BANGI 2009
Content 1.0 About UBUNTU 1 2.0 Terminal 1 3.0 Basic Linux Commands 3 4.0 G++ Commands 23
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. Click this Terminal icon Figure 1: User Interface of UBUNTU 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. Figure 2: Terminal window Quick Start Basic Linux and G++Commands Prepared by Pn. Azura Ishak Page 2
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 Check or view the current directory Figure 3: pwd command 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). List and check any files or other directories in the current directory 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 List and check any files or other directories in the current directory using file browser Figure 5: File browser in UBUNTU 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. Creating a new directory Figure 6: mkdir command 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. Check directory of mylab mylab directory Figure 7: Checking the directory using ls command 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. Open mylab 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. Check the files or other directories inside mylab directory. No list files and directories proof that the directory of mylab is empty. Figure 9: Checking the contents of the directory using ls command 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. Open gedit text editor followed with the file name of try.cpp Figure 10: gedit command 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. Figure 11: 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. Figure 12: Simple C++ programming in gedit text editor 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 Check the files or other directories inside mylab directory. Open gedit text editor Check the file of try.cpp Figure 13: Checking the file in the directory using ls command 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. Display the content inside the file through the terminal Figure 14: cat command 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. Copy try.cpp to a new file name try2.cpp Check the new file name of try2.cpp Figure 15: cp command (copy file) 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. Create new directory of mylab2 Copy try.cpp into mylab2 Open mylab2 Check the file of try.cpp in mylab2 directory Figure 16: cp command (copy file into directory) 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. Check the current file name of try.cpp Copy mylab2 to a new directory of mylab3 Check the directory of mylab3 Figure 17: cp command (copy directory) 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. 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 Figure 18: mv command (rename file) 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. Check test.cpp & try.cpp are outside mylab3 Move test.cpp & try.cpp into mylab3 Open mylab3 Check test.cpp & try.cpp are inside mylab3 Figure 19: mv command (move file into directory) 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 Move mylab3 into mylab2 Open mylab2 Check mylab3 is inside mylab2 Figure 20: mv command (move directory into another directory) 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 Delete try.cpp & test.cpp Check try.cpp and test.cpp have been deleted or not. Figure 21: rm command (delete file) 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 Check mylab3 inside mylab2 Delete mylab3 Check mylab3 has been deleted or not Figure 22: rm command (delete directory) 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 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. compile error Figure 23: g++ command 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 ; Figure 24: Edit the C++ programming in gedit text editor 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 output execute Figure 25:./a.out command 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. Use Tab key Use Tab key Use Tab key Use Tab key Figure 29: makefile script 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 Figure 30: make f command 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. Object file Execute 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 Output Execute command Figure 32: Execute command Quick Start Basic Linux and G++Commands Prepared by Pn. Azura Ishak Page 32