QUICK START BASIC LINUX AND G COMMANDS

Transcription

QUICK STARTBASIC LINUX ANDG COMMANDSPrepared By:Pn. Azura Bt IshakFTSM UKMBANGI2009

Content1.0 About UBUNTU12.0 Terminal13.0 Basic Linux Commands34.0 G Commands23

1.0 ABOUT UBUNTUUbuntu is a community developed, Linux-based operating system that is perfect for laptops, desktops andservers. It contains all the applications you need - a web browser, presentation, document and spreadsheetsoftware, instant messaging and much more. (referred in http://www.ubuntu.com/)2.0 TERMINALLinux commands will be written through UBUNTU's inside the terminal.Step 1: Click Terminal Icon such as below.Click this Terminal iconFigure 1: User Interface of UBUNTUQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 1

Step 2: The window of terminal will display such as the figure 2 below.Figure 2: Terminal windowQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 2

3.0 BASIC LINUX COMMANDS1) pwd to find the name of working directory.The example below (/home/pelajar) shows that you're in pelajar directory which it is located insidethe home directoryCheck or view the currentdirectoryFigure 3: pwd commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 3

2) ls to list the files and directoriesYou can see the list of files and directories inside pelajar directory or folder. The files must befollowed by the format (eg. book.make, lab.cpp), meanwhile any directory or folder didn't have anyformat (eg. Desktop, Documents).List and check any files orother directories in thecurrent directoryFigure 4: ls commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 4

The same thing you can view directories and files in graphic based if you click places menu andclick icon Home Folder such as below. You can see the directories seem like in the booklet basedmeanwhile files seem like a single paper. This graphical based is more easier compare to commandbased BUT since this lesson is about commands, so we'll focus on that particular topic.Places MenuList and check any files orother directories in thecurrent directory using filebrowserFigure 5: File browser in UBUNTUQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 ismyLab. 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 youstill want to separate two wording together, you may put underscore such as my Lab.Creating a new directoryFigure 6: mkdir commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 6

Step 2: Now, let’s check either the directory of myLab is already inside pelajar directory by typingls command. In the following figure 7, you can see that myLab directory is already there. Itmeans your directory has been successful built.Check directory of myLabmyLab directoryFigure 7: Checking the directory using ls commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 thedirectory. It seems like if you want to put something in the drawer, for sure you should open firstthe drawer, the you can put anything inside the drawer. In the figure 8 below, we try to open thedirectory of myLab by using command cd. If you want to open the previous directory, you can justtyping cd command without following by the name of directory.Open myLab directoryFigure 8: cd commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 8

Step 2: Now, let’s check either this directory have any files or other directories by using commandls. Logically, since this is a new directory, so for sure it is empty right? You can see there isnothing display such as the figure 9 below.Open myLabdirectory.Check the files or otherdirectories inside myLabdirectory.No list files and directoriesproof that the directory ofmyLab is empty.Figure 9: Checking the contents of the directory using ls commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 gedittext editor because its more colorful text based compare to pico.Open myLabdirectory.Open gedit text editorfollowed with the filename of try.cppFigure 10: gedit commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 10

Step2: Gedit text editor will display such as the figure 11 below. You can type any text includingC programming in this gedit text editor.Figure 11: gedit text editorQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 11

Step 3: Now, let’s try typing any simple coding of C such as below. Click save once you finishtyping the coding and close the file for the moment.Figure 12: Simple C programming in gedit text editorQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 12

Step 4: Go back to the terminal, and check the file of try.cpp by using command ls. You can seethe file will display in the terminal such as figure 13.Open myLabdirectoryCheck the files or other directoriesinside myLab directory.Open gedit text editorCheck the file of try.cppFigure 13: Checking the file in the directory using ls commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 fileusing gedit text editor. But you may NOT edit the contents at all. The cat command just want tohelp user to identify, recheck or reconfirm the contents faster and easier of the specific file.Display the content insidethe file through the terminalFigure 14: cat commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 oftry2.cpp whether it's already existed or not by using ls command.Copy try.cpp to a new filename try2.cppCheck the new file nameof try2.cppFigure 15: cp command (copy file)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 anew directory of myLab2 using the previous command mkdir. Open the new directory of myLab2by using cd command and type ls command to check whether the file of try.cpp is already existed ornot.Create new directoryof myLab2Copy try.cpp intomyLab2Open myLab2Check the file of try.cpp inmyLab2 directoryFigure 16: cp command (copy file into directory)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 myLab2directory, make sure go back to the previous directory of pelajar by using cd command withoutfollowing any directory name. Then open back myLab directory by typing cd myLab.Check the current filename of try.cppCopy myLab2 to a newdirectory of myLab3Check the directoryof myLab3Figure 17: cp command (copy directory)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 renametry.cpp to a new file name of test.cpp.Check the current filename of try.cppChange the name file of try.cpp to test.cppCheck the new file name of test.cppFigure 18: mv command (rename file)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 twofiles test.cpp and try2.cpp into the directory of myLab3.Check test.cpp & try.cppare outside myLab3Move test.cpp & try.cpp into myLab3Open myLab3Check test.cpp & try.cppare inside myLab3Figure 19: mv command (move file into directory)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 movemyLab3 directory into the myLab2 directory.current directories;myLab2 & myLab3Move myLab3 into myLab2Open myLab2Check myLab3 is insidemyLab2Figure 20: mv command (move directory into another directory)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 ofthe files try.cpp and test.cpp in myLab3 directory.Open myLab3Delete try.cpp & test.cppCheck try.cpp and test.cpphave been deleted or not.Figure 21: rm command (delete file)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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’stry to delete myLab3 directory which it inside myLab2 directory. You can also delete more than onedirectory together.Open myLab2Check myLab3 inside myLab2Delete myLab3Check myLab3 has been deletedor notFigure 22: rm command (delete directory)Quick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 22

3.0 G COMMANDS : COMPILING AND EXECUTING FILE OF C PROGRAMMING1) 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.compileerrorFigure 23: g commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 editorQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 24

2) ./a.out to execute file of C programmingIf 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.compileexecuteoutputFigure 25: ./a.out commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 –fcommand 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 fileFigure 26: main.cpp contains main() and this file depends on statement.hQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 26

header fileFigure 27: statement.cpp implementation for class and this file depends on statement.hQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 27

Figure 28: statement.h header file for classQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 makeutility to choose the appropriate program files that are to be compiled and linked together. 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 thefigure 29. Make sure all the files including makefile must be put into the same directory or folder.Use ‘Tab’ keyUse ‘Tab’ keyUse ‘Tab’ keyUse ‘Tab’ keyFigure 29: makefile scriptQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 29

Step 3: Open the terminal, and start compiling (make –f makefile)Compiling makefileNo syntaxerrorsFigure 30: make –f commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 30

Step 4: Type ls command and you will see the 2 objects files (main.o and statement.o) and 1 executefile (try) will be automatic generated for you.Execute fileObject fileObject fileFigure 31: Checking the objects and execute files using ls commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 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 theoutput of “Welcome to UKM” will be display through the terminal.Execute fileExecute commandOutputFigure 32: Execute commandQuick Start Basic Linux and G Commands – Prepared by Pn. Azura IshakPage 32

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 . Quick Start Basic