Академический Документы
Профессиональный Документы
Культура Документы
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
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 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.
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.
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
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
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.
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.
-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