Sie sind auf Seite 1von 7

Introduction to Linux. Writing and Compiling C Programs in Linux.

Contents
1 File 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 Management in Linux The Command Line Prompt . . . . . . . . . . . . . . . . . . . . Getting Help in Linux . . . . . . . . . . . . . . . . . . . . . . . Viewing the Current Directory . . . . . . . . . . . . . . . . . . Viewing the Files and Sub-directories of the Current Directory Changing the Current Directory . . . . . . . . . . . . . . . . . Creating an Empty File on Disk . . . . . . . . . . . . . . . . . Deleting a File on Disk . . . . . . . . . . . . . . . . . . . . . . . Creating a Directory on Disk . . . . . . . . . . . . . . . . . . . Deleting a Directory . . . . . . . . . . . . . . . . . . . . . . . . The Midnight Commander Navigation Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 2 2 2 3 4 4 4 5 5 5 6

2 Editing and Viewing Text Files 3 Compiling and Running C Programs 4 Exercises

File Management in Linux

The commands that will be described are meant to be executed from a command line terminal. In order to open a terminal, access from the menu: Applications Accessories Terminal. You will see a window opening, with a command line prompt where you can type commands.

1.1

The Command Line Prompt

The command line prompt that shows up in the terminal window contains useful information. It has the form: murphy@murphy-laptop:~/tp$ There are three parts in the command line prompt, separated by @ : and $ murphy @ murphy-laptop : ~/tp $

The rst part before the @ shows the user which is logged in at the moment. In our example it is the user murphy. The second part between the @ and the : shows the name of the computer you are working on. In our example it is murphy-laptop. The third part, between the : and the $ shows the current directory you are working in. In our example it is ~/tp (~ is a shortcut for the users home directory; ~/tp is equivalent to /home/murphy/tp). When you will change the current directory, you will see the third part of the command line prompt changing. 1

Exercise Identify the three parts of the command prompt on the computer you are working on. What is the name of the logged in user? What is the name of the computer? Which is the current directory immediately after opening up the terminal window?

1.2

Getting Help in Linux

Whenever you need help about commands in Linux, you can read the man pages, which contain detailed information about commands and their options. To see the man page about a command, type man <command name> Information about the command will appear on the screen. You can move up and down using the arrow keys. In order to exit from the man page and return to the command line type the letter q. Exercise Type man man to read information about the man command itself.

1.3

Viewing the Current Directory

At any time, in order to see the current directory you can type pwd The letters come from print name of current/working directory. Example murphy@murphy-laptop:~/tp$ pwd /home/murphy/tp Exercise Type pwd and see the full path of the current directory.

1.4

Viewing the Files and Sub-directories of the Current Directory

In order to see the list of les and directories that are found in the current directory, type ls The name of the command comes from list directory contents. Example murphy@murphy-laptop:~/tp$ ls laborator01 test.c Two names are listed: laborator01 and test.c. Usually they are printed with colors, and based on the color we can tell which are les and which are directories. In order to see more details about the les and directories, type ls -l murphy@murphy-laptop:~/tp$ ls -l total 4 drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01 -rw-r--r-- 1 murphy murphy 0 2009-02-17 23:42 test.c

The same two entries are listed, but with more information next to each of them. We can see access rights, time of last modication, owner, etc. What is the most important for the moment is the rst letter in each line. If the rst letter in a line is d, then the name on that line is a directory. If the rst letter is - then the name on that line is a le. Exercise Type ls to see the content of the current directory. Then type ls -l. Identify the les and the directories based on the rst letter in each line. Then determine which color shows directories in the short form of ls, and which colors show les.

1.5

Changing the Current Directory

In order to change the current directory, type cd <new path on disk> The letters come from change the current directory. You must specify the path of the new current directory. Example For changing the current directory to the sub-directory laborator01 you type

murphy@murphy-laptop:~/tp$ cd laborator01 murphy@murphy-laptop:~/tp/laborator01$ pwd /home/murphy/tp/laborator01 For going one level up, to the parent directory, you type murphy@murphy-laptop:~/tp/laborator01$ cd .. murphy@murphy-laptop:~/tp$ pwd /home/murphy/tp The root directory is / murphy@murphy-laptop:~/tp$ cd / murphy@murphy-laptop:/$ pwd / An absolute path can also be specied, starting from the root directory: murphy@murphy-laptop:/$ cd /home/murphy/tp murphy@murphy-laptop:~/tp$ pwd /home/murphy/tp

1.6

Creating an Empty File on Disk

In general the text editors are able to create empty les on disk. However, sometimes it is useful to create an empty le directly from the command line. In order to do it, type: touch <file name> An empty le with the specied name will be created. If a le with that name already exists on disk, it will remain there and moment of last update is changed in the properties of the le.

Example Watch the date shown next to the test.c le in the les listing. After executing touch upon the le, the date will change: murphy@murphy-laptop:~/tp$ total 4 drwxr-xr-x 2 murphy murphy -rw-r--r-- 1 murphy murphy murphy@murphy-laptop:~/tp$ murphy@murphy-laptop:~/tp$ total 4 drwxr-xr-x 2 murphy murphy -rw-r--r-- 1 murphy murphy ls -l 4096 2009-02-17 23:42 laborator01 0 2009-02-17 23:42 test.c touch test.c ls -l 4096 2009-02-17 23:42 laborator01 0 2009-02-18 00:00 test.c

Note the appearance of the new le exemplu.c after running the touch command: murphy@murphy-laptop:~/tp$ touch exemplu.c murphy@murphy-laptop:~/tp$ ls -l total 4 -rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 exemplu.c drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01 -rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 test.c Exercise Create an empty le named file.txt in your home folder. Type ls -l and see the last modication time of the le. Wait a minute or so and run touch on the le. See again the last modication time and notice the change.

1.7

Deleting a File on Disk

It is done through rm <file name> The letters come from remove les or directories. Example In order to delete the test.c le, we type:

murphy@murphy-laptop:~/tp$ rm test.c murphy@murphy-laptop:~/tp$ ls -l total 4 -rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 exemplu.c drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01 Exercise Delete the le test.txt created earlier.

1.8

Creating a Directory on Disk

A new directory is created with the command mkdir <directory name> The letters come from make directories.

Example

In order to create the directory laborator02 we type: mkdir laborator02 ls -l 0 2009-02-18 00:00 exemplu.c 4096 2009-02-17 23:42 laborator01 4096 2009-02-18 00:07 laborator02

murphy@murphy-laptop:~/tp$ murphy@murphy-laptop:~/tp$ total 8 -rw-r--r-- 1 murphy murphy drwxr-xr-x 2 murphy murphy drwxr-xr-x 2 murphy murphy

Exercise Create a directory named test.dir in your home folder. Change the current directory to it. Type pwd to view the current directory.

1.9

Deleting a Directory

In order to delete a directory, type rmdir <directory name> or rm -r <directory name> The rst command deleted only empty directories. If the directory contains any le or sub-directory, a warning will be displayed and the directory will not be deleted. The second command deletes the folder, recursively, together with its les and sub-directories. Use with care! Example In the directory laborator02 we have created other les and sub-directories. First we try to delete the directory laborator02 using rmdir, then using rm -r. Notice the warning message issued after we try the rst command. murphy@murphy-laptop:~/tp$ rmdir laborator02 rmdir: failed to remove laborator02: Directory not empty murphy@murphy-laptop:~/tp$ rm -r laborator02 murphy@murphy-laptop:~/tp$ ls -l total 4 -rw-r--r-- 1 murphy murphy 0 2009-02-18 00:00 exemplu.c drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01

1.10

The Midnight Commander Navigation Program

Its a similar program with Total Commander from Windows, but with text interface. You can start it by typing: mc It allows you to visually navigate through the hierarchy of directories and les, to create and delete directories and les, etc. It is a more user friendly alternative to the command line.

Editing and Viewing Text Files

Linux oers several programs for editing and viewing text les. There are text editors with plain text interface, or with graphical interface. On the computers in the laboratory, you can nd installed: emacs, vi, nano, gedit, and mcedit, the editor of mc. In order to view the content of a le, you can either open the le in one of the editors, or you can type in a terminal one of the commands:

cat <file name> or less <file name> The rst command prints the content of the le on the screen, and then returns the control to the command line. It is useful for small les. The second command prints the content of the le and allows you to navigate up and down using the arrow keys. It is useful for large les. In order to quit and return to the command line, type the letter q.

Compiling and Running C Programs

We will compile C programs from the command line. We will use the gcc compiler. You can nd details about it on the website http://gcc.gnu.org/ or in its man page (man gcc). For starters we will use the compiler in a simple form: gcc -Wall -o <output executable file> <input C file> The result of running the command is that the input C le will be compiled and linked, and the output binary le will have the name specied by us. The -Wall option instructs the compiler to print out all warnings about our source code. Example Suppose we have a program stored in a le test.c:

#include <stdio.h> int main(void) { printf("Will it compile?\n"); return 0; } The content of the current directory is: murphy@murphy-laptop:~/tp$ ls -l total 8 drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01 -rw-r--r-- 1 murphy murphy 85 2009-02-18 00:22 test.c We compile the le: murphy@murphy-laptop:~/tp$ gcc -Wall -o test test.c If the le would have errors, they would be printed on the screen. If nothing is printed, it means the le was compiled and linked successfully. Now the current directory contains: murphy@murphy-laptop:~/tp$ ls -l total 16 drwxr-xr-x 2 murphy murphy 4096 2009-02-17 23:42 laborator01 -rwxr-xr-x 1 murphy murphy 6369 2009-02-18 00:24 test -rw-r--r-- 1 murphy murphy 85 2009-02-18 00:22 test.c The test le appeared, which is our output executable le. In Linux executable programs dont require the .exe extension. In order to run the program we have just compiled, we type:

murphy@murphy-laptop:~/tp$ ./test Will it compile? The ./ characters are important when we are running programs from the current directory. We can see that the program ran successfully, and our message got printed.

Exercises
1. Create the following hierarchy of directories in your home directory: ~/test/test1/test11 ~/test/test1/test12 ~/test/test2/test21 ~/test/test2/test22 (a) without using the cd command (b) using the cd command 2. Delete the directory test21, having as current directory test (a) using a relative path (b) using an absolute path 3. List the contents of the directory test and its subdirectories. 4. Delete the directory test with all its subdirectories. 5. Perform the above operations from mc. 6. Write a C program that tells the user to think about a number between 1 and 100 and then tries to guess the number by asking questions of the form Is your number greater than ...? and Is your number smaller than ...?. Save the program in a le called guess.c. Compile and link the le with gcc. Run the program.

Das könnte Ihnen auch gefallen