Вы находитесь на странице: 1из 11

Birla Institute of Technology & Science - Pilani, K K Birla Goa Campus Computer Programming (CS F111) Second Semester

2011-2012 Lab-1 -----------------------------------------------------------------------------------------------------------Objectives: o Introduction (Computing Machine, Operating System, LINUX) o Getting started with LINUX 1. Opening a Terminal Window 2. LINUX treats everything as Files!!! 3. Print Working Directory (pwd command) 4. List the contents of current directory (ls command) 5. Creating a Directory (mkdir command) 6. Changing to a different Directory (cd command) 7. Other Linux commands o Exercises ------------------------------------------------------------------------------------------------------------

Introduction
A computer is a programmable machine. It allows the user to store all sorts of information and then process that information, or data, or carry out actions with the information, such as calculating numbers or organising words. Computers are made up of two parts: the hardware and the software. Hardware is the physical equipment required to create, use, manipulate and store electronic data whereas software is used to provide instructions to computer to carry out processing. All computers require the following components or resources: Central Processing Unit (CPU) Memory Storage device : Main Memory and Secondary Memory Input devices: Keyboard, mouse, scanner, etc. Output devices: Monitor, printer, etc.

The above resources are to be efficiently utilized and the user has to be provided with the interface with which it can interact with the computer in easy manner. Operating system is software which acts as a resource manager and provides the user interface. Current operating system offers two different kind of user interface i.e. Command line interface and Graphical User Interface(GUI). For Computer Programming lab we will be using LINUX operating system. In this lab sheet, LINUX basic commands related with files and directory operations are provided.

1/11

Getting started with LINUX


1. Opening a Terminal Window
After logging in, you will get a desktop screen similar to the one shown in Figure 1.

Figure 1: Desktop of a Linux system after logging in Figure 2 shows the steps to open a terminal window. The steps are as follows. Step 1: Click on the Applications tab in the top panel (top left corner of the desktop). The Applications will provide a dropdown list as shown in Figure 2. Step 2: Select (Click on) System Tools option in the dropdown list. This will provide a dropdown list as shown in Figure 2. Step 3: Select (Click on) Terminal option in the dropdown list. This will open a new window as shown in Figure 3.

2/11

Figure 2: Steps to open a Terminal Window In terminal Window you will find a prompt like this (as shown in Figure 3): [CPSEC1@localhost ~] $

Figure 3: A terminal Window 3/11

2. LINUX treats everything as Files!!!


Everything in LINUX is either a file or a process (dont bother to understand the term process at this stage, not a big deal anyway ). A file is a collection of data. They are created by users using text editors, running compilers etc. Examples of files: a. A document (report, essay etc.) b. The text of a program written in some high-level programming language (C, Pascal, Java, etc) c. File containing a photo, etc. All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree as shown in Figure 4. The top of the hierarchy is traditionally called root (written as a slash / )

Figure 4: Example of a typical Linux File System Generally each user has a personal or home directory, sometimes called login directory, that contains only the files that belong to him or her. When you login, you are in your home directory named by your user-name.

3. Print Working Directory (pwd command)


pwd command prints the name of the directory you are currently in. At the prompt, say, [CPSEC1@localhost ~] $ type pwd and press enter key i.e., [CPSEC1@localhost ~] $ pwd /home/CPSEC1/

4/11

This means that you are currently in the directory CPSEC1, in the directory home which in turn is in the root directory /. The name / h o m e / C P S E C 1 / is called the pathname. What is Home Directory??? When you open a terminal window, your current working directory is your home directory. Your home directory has the same name as your user-name.

4. List the contents of the current directory (ls command)


Command ls ls filename ls -t ls -l ls -u ls -r ls -a Description List names of all files in current directory List only the names files List in time order, most recent first List long List by time last used List in reverse order List all the files including hidden files

Assume that the command prompt is, [CPSEC1@localhost ~] $ Type ls at the prompt [CPSEC1@localhost ~] $ ls You will get the display similar to figure 5. You will immediately see the prompt back [CPSEC1@localhost ~] $

5/11

Figure 5: Output of ls command As you can see from Figure 5, home directory has only the system defined directories. ls a command is used to list all the files including hidden files. Hidden files starts with dot(.). The output of [CPSEC1@localhost ~] $ ls a is shown in Figure 6. The output of the ls a is shown in Figure 6. observe the first two results: a single dot (.) and a double consecutive dots (..). In LINUX, (.) means the current directory and (..) means the parent of the current directory.

6/11

Figure 6: Output of ls -a command

5. Creating a Directory (mkdir command)


We will create a subdirectory in our home directory to hold the files we will be creating and using during the lab. We name the new directory as exercises. The command is as follows: [CPSEC1@localhost ~] $ mkdir exercises You will immediately get back the prompt, without any other messages, as [CPSEC1@localhost ~] $ But, how do we make sure that if the directory named exercises is created or not??? We have ls command [CPSEC1@localhost ~] $ ls You can find the listing of your directory using ls command. How do we go to the directory created??? Read next section.

6. Changing to a different directory (cd command)


The command cd directory means change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current 7/11

position in the file-system tree. We want to change the current working directory to exercises. We do it as follows [CPSEC1@localhost ~] $ cd exercises [CPSEC1@localhost /exercises] $ Observe the two prompts above. o Before you enter the command you can see a ~ symbol towards the end of the prompt o Once you entered the command you can find the ~ symbol is replaced by exercises. exercises is the present working directory. Then what does the ~ symbol mean? o ~ symbol stands for your home directory

7. Other Linux commands 7-a) Creating and displaying the contents of a file
The cat command is used to print the file contents to the standard output (normally on your PC screen), cat also can be use to create as well as concatenate files on Linux system. The cat command is short for concatenate files. Let us create a file Poem using the command cat. Type the command at the command prompt [CPSEC1@localhost ~] $ cat > Poem1 and press the enter key. The cursor will be in the next line and waiting for the file contents. Now type

After typing the paragraph above, press Ctrl-d. Once this key combination is typed, UNIX saves the contents typed so far and saves in the file named poem1. To see the contents of this file, you can use cat command [CPSEC1@localhost ~] $ cat Poem1 Syntax of displaying content of a file: cat <filename> We can concatenate the contents of two files using cat command. You need to create one more file Poem2 using cat with the following content.

8/11

Let us concatenate the contents of the two files (Poem1 and Poem2) and store the result in the file named Poem. The cat command is issued as follows. [CPSEC1@localhost ~] $ cat Poem1 Poem2 > Poem

7-b) Copying Files


We can use the copy (cp) command to copy the contents of one file into another file or Copy files from one location to another. Syntax cp [OPTION1] [OPTION2] ... SOURCE DESTINATION OPTIONS are optional OPTIONS -f -i -l -u -R or r PURPOSE if an existing destination file cannot be opened, remove it and try again prompt before overwrite link files instead of copying copy only when the SOURCE file is newer than the destination file or when the destination file is missing copy directories recursively

For instance, to copy the contents of the file firstwill into the file finalwill, we use the cp command as follows. cp firstwill finalwill After issuing the command, you may see a new file finalwill created in the current directory with the content of firstwill. Here the file firstwill is the source file, and the file finalwill is the destination file. In the above usage, we assumed the file finalwill doesnt exist. But if the file exist and used as the destination of cp command, the contents of this file will be overwritten. The syntax of cp command is as follows cp source destination source refers to the source filename, and destination refers to the destination filename or destination directory. If the destination is a directory, the source file is copied into the destination directory with the same name as source. The source and destination can refer to absolute or relative path specification of the file location.

7.b Moving (and Renaming) File / Directory


Moving a file / directory means removing it from its current location and copying it into the new location. Syntax mv [OPTION1] [OPTION2] ... SOURCE DESTINATION OPTIONS are optional OPTIONS -f PURPOSE This option replaces the file / directory if it exists already in the destination without prompting to the user. Note that this is the default if the standard input 9/11

is not a terminal. -i This option prompts us, if we are trying to replace a file/directory in the destination.

Examples mv finalwill finalwish Moves file named finalwill to finalwish. The file named finalwill will not be available in the system after this operation. mv myfile exercises/ Moves the file named myfile to the directory exercises. The file named myfile will be available inside exercise directory (not in current location). mv file2 ../ Moves the file named file2 to the parent directory (if write permission is available). mv f first third Note that we are trying to move the file first to third. Both file are in the same directory (location). Hence, it essentially removes first from the current directory and writes into the current directory as third. This command has the effect of renaming the file first to third. What will happen, if a file named third is already exists in the current directory? The f specified in the command will replace the existing file third with the new one. But, the command mv i first third will notify you before it attempts to replace the existing file, and will prompt you if you are sure in replacing the existing one. If you type n, it would skip replacing. This command can be issued without f or i options. In this case, the UNIX system assumes the option is f. That is, we say the option f is default option. Try to use this command to moving your file contents.

7.c Removing Files and directories


To remove a file we use the rm command. Syntax rm [OPTION1] [OPTION2] ... [filenames | directory] OPTIONS -f PURPOSE Remove all files (whether write-protected or not) in a directory without prompting the user. In a write-protected directory, however, files are never removed (whatever their permissions are), but no messages are displayed. If the removal of a write-protected directory is attempted, this option will not suppress an error message. Interactive. With this option, rm prompts for confirmation before removing any files. It over- rides the -f option and remains in effect even if the standard input is not a terminal. 10/11

-i

-r or -R

Recursively remove directories and subdirectories in the argument list. The directory will be emptied of files and removed. The user is normally prompted for removal of any write-protected files which the directory contains. The write-protected files are removed without prompting, however, if the -f option is used, or if the standard input is not a terminal and the -i option is not used. Symbolic links that are encountered with this option will not be traversed. If the removal of a non-empty, write-protected directory is attempted, the utility will always fail (even if the -f option is used), resulting in an error message.

Examples To remove a file named help we use the command rm help To remove a directory named exercises we use the command rmdir exercises To avoid inadvertently deleting a file, always use the rm command together with its i option. rm -i filename This will prompt you to confirm that you want to remove a file from the current directory. Answering y will delete the file. The file is not deleted if any other response is given. To remove a directory, we may still use the rm command, but we specify the name of the directory to be deleted instead. rm -r directory_name This deletes all the contents of the directory including any subdirectories. To avoid inadvertently removing a directory, always use the rm command together with the i option. rm -ir directory_name But if the directory we want to delete is empty, we may use rmdir command. If the directory to be deleted is exercises, we may type

rmdir exercises
Try typing the command, [CPSEC1@localhost ~] $ ls

[CPSEC1@localhost ~] $ ls .. What is your observation from the result of the above two commands? The ls . gives the same result as ls (displays all the files and directories in the current directory) The ls .. displays the directories and files in the parent directory as shown in Figure 7.

11/11

Вам также может понравиться