Sunteți pe pagina 1din 11

LAB 03

Objective: Introduction to linux shell

1. Command Shell
A program that interprets commands Allows a user to execute commands by typing them manually at a terminal, or automatically in programs called shell scripts. A shell is not an operating system. It is a way to interface with the operating system and run commands.

2. Command Syntax
Commands can be run by themselves, or you can pass in additional arguments to make them do different things. Typical command syntax can look something like this:

command [-argument] [-argument] [--argument] [file]

3. 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 downusing the arrow keys. In order to exit from the man page and return to the command line type the letter q. Exercise2: Type man man to read information about the man command itself.

4. 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 bar: Dash Home Terminal. A window will open, with a command line prompt where you can type commands. i. 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 first part before the @ shows the user which is logged in at the moment. In this example it is the user murphy. The second part between the @ and the : shows the name of the computer you are working on. In this 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).

Exercise1: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?

5. Commands for Navigating the Linux Filesystems


The first thing you usually want to do when learning about the Linux filesystem is take some time to look around and see what's there! These next few commands will: a) Tell you where you are, b) Take you somewhere else, and c) Show you what's there.

i.

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 3:Type pwd and see the full path of the current directory.

ii.

Viewing the Files and Sub-directories of the Current Directory

In order to see the list of files 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 files and which are directories. In order to see more details about the files and directories, type ls -l murphy@murphy-laptop:~/tp$ ls -l

access rights drwxr-xr-x -rw-r--r--

Files 2 1

User name murphy murphy

Computer name murphy murphy

size 4096 0

date of last modification 2013-02-17 2013-02-17

time of last File name modification 23:42 laborator01 23:35 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 modification, owner, etc. What is the most important for the moment is the first letter in each line. If the first letter in a line is d, then the name on that line is a directory. If the first letter is - then the name on that line is a file.

Exercise 4: Type ls to see the content of the current directory. Then type ls -l. Identify the files and the directories based on the first letter in each line. Then determine which color shows directories in the short form of ls, and which colors show files.

iii.

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 specified, starting from the root directory: murphy@murphy-laptop:/$ cd /home/murphy/tp murphy@murphy-laptop:~/tp$ pwd /home/murphy/tp

Command cd /home

Description Change the current working directory to /home. The '/' indicates relative to root, and no matter what directory you are in when you execute this command, the directory will be changed to "/home". Move to the parent directory of the current directory. This command will make the current working directory "/home. Move to the user's home directory which is "/home/username". The '~' indicates the users home directory.

cd ..

cd ~

iv.

Creating an Empty File on Disk

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

Example Watch the date shown next to the test.c file in the files listing. After executing touch upon the file, the date will change: murphy@murphy-laptop:~/tp$ ls -l

access rights drwxr-xr-x -rw-r--r--

Files 2 1

User name murphy murphy

Computer name murphy murphy

size 4096 0

date of last modification 2013-02-17 2013-02-17

time of last File name modification 23:42 laborator01 23:35 test.c

murphy@murphy-laptop:~/tp$ touch test.c murphy@murphy-laptop:~/tp$ ls -l

access rights drwxr-xr-x -rw-r--r--

Files 2 1

User name murphy murphy

Computer name murphy murphy

size 4096 0

date of last modification 2013-02-17 2013-02-17

time of last File name modification 23:42 laborator01 00:00 test.c

Note the appearance of the new file exemplu.c after running the touch command: murphy@murphy-laptop:~/tp$ touch exemplu.c murphy@murphy-laptop:~/tp$ ls -l

access rights drwxr-xr-x -rw-r--r--rw-r--r--

Files 2 1 1

User name murphy murphy murphy

Computer name murphy murphy murphy

size 4096 0 0

date of last modification 2013-02-17 2013-02-17 2013-02-17

time of last modification 23:42 00:00 00:00

File name laborator01 test.c exampleu.c

Exercise 5: Create an empty file named file.txt in your home folder. Type ls -l and see the last modification time of the file. Wait a minute or so and run touch on the file. See again the last modification time and notice the change.

v.

Deleting a File on Disk

It is done through rm <file name> The letters come from remove files or directories. Example In order to delete the test.c file, we type: murphy@murphy-laptop:~/tp$ rm test.c murphy@murphy-laptop:~/tp$ ls -l

access rights drwxr-xr-x -rw-r--r--

Files 2 1

User name murphy murphy

Computer name murphy murphy

size 4096 0

date of last modification 2013-02-17 2013-02-17

time of last File name modification 23:42 laborator01 00:00 exampleu.c

Exercise6: Delete the file test.txt created earlier.

vi.

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: murphy@murphy-laptop:~/tp$ mkdir laborator02 murphy@murphy-laptop:~/tp$ ls -l

access rights drwxr-xr-x drwxr-xr-x -rw-r--r--

Files 2 2 1

User name murphy murphy murphy

Computer name murphy murphy murphy

size 4096 4096 0

date of last modification 2013-02-17 2013-02-18 2013-02-17

time of last modification 23:42 00:07 00:00

File name laborator01 laborator02 exampleu.c

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

vii.

Deleting a Directory

In order to delete a directory, type rmdir <directory name> or rm -r <directory name> The first command deleted only empty directories. If the directory contains any file 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 files and sub-directories. Example In the directory laborator02 we have created other files 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 first 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

access rights drwxr-xr-x -rw-r--r--

Files 2 1

User name murphy murphy

Computer name murphy murphy

size 4096 0

date of last modification 2013-02-17 2013-02-17

time of last File name modification 23:42 laborator01 00:00 exampleu.c

viii.

Copying a file

In order tocopy a file cp <sourcefile><destinationfile> Command cp myfile yourfile cp -i myfile yourfile Description Copy the files "myfile" to the file "yourfile" in the current working directory. This command will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it exists. With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.

cp -i /data/myfile . cp -dpr srcdir destdir

Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt before overwriting the file. Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-p option), file attributes (-p option), and copy recursively (-r option). With these options, a directory and all its contents can be copied to another directory.

ix.

Moving files In order to remove the files mv <myfile><yourfile>

Command mv -i myfile yourfile mv -i /data/myfile

Description Move the file from "myfile" to "yourfile". This effectively changes the name of "myfile" to "yourfile". Move the file from "myfile" from the directory "/data" to the current working directory.

6. Editing and Viewing Text Files


Linux offers several programs for editing and viewing text files. There are text editors with plain text interface, or with graphical interface. In order to view the content of a file, you can either open the file in one of the editors, or you can type in a terminal one of the commands: cat <file name> gedit <file name> The command prints the content of the file on the screen, and then returns the control to the command line. It is useful for small files. The second command prints the content of the file and allows you to navigate up and down using the arrow keys. It is useful for large files.

7. Compiling and Running C Programs


We will compile C programs from the command line. We will use the gcc compiler. You can find 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 -c<input C file>

gcc -o<output executable file><input C file> ./<output executable file>

The result of running the command is that the input C file will be compiled and linked, and the output binary file will have the name specified by us. The option instructs the compiler to print out all warnings about our source code. Example Suppose we have a program stored in a file test.c: #include <stdio.h> int main(void) { printf("Will it compile?\n"); return 0; } murphy@murphy-laptop:~/tp$ gcc -c test.c murphy@murphy-laptop:~/tp$ gcc o test test.c murphy@murphy-laptop:~/tp$ ./test 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.

Lab Tasks:
1. Verify that you are in your home directory. 2. Make the directory adir using the following command: 3. List the files in the current directory to verify that the directory adir has been made correctly. 4. Change directories to adir. 5. Verify that you have succeeded in moving to the adir directory. 6. Create a file testfile.txt 7. Verify that the file testfile exists. 8. List the contents of the file testfile to the screen. 9. Make a copy of the file testfile under the name secondfile. 10. Verify that the files testfile and secondfile both exist. 11. List the contents of both testfile and secondfile to the monitor screen.

12. Delete the file testfile. 13. Verify that testfile has been deleted. 14. Clear the window. 15. Rename secondfile to thefile. 16. Copy thefile to your home directory. 17. Remove thefile from the current directory. 18. Verify that thefile has been removed. 19. Copy thefile from your home directory to the current directory. 20. Verify that thefile has been copied from your home directory to the currentdirectory. 21. Change directories to your home directory. 22. Verify that you are in your home directory. 23. Verify that a copy of thefile is in your home directory. 24. Remove thefile from your home directory. 25. Remove thefile from the directory adir. 26. Remove the directory adir from your home directory with the following command. 27. Verify that thefile and adir are gone from your home directory. 28. 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 file called guess.c. Compile and link the file with gcc. Run the program.

S-ar putea să vă placă și