7/29/2019 Lab1 chassis
1/11
Birla Institute of Technology & Science - Pilani, K K Birla Goa CampusComputer Programming (CS F111)
Second Semester 2011-2012Lab-1
------------------------------------------------------------------------------------------------------------Objectives:
o Introduction (Computing Machine, Operating System, LINUX)
o Getting started with LINUX
1. Opening a Terminal Window2. 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
------------------------------------------------------------------------------------------------------------
IntroductionA 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
7/29/2019 Lab1 chassis
2/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
7/29/2019 Lab1 chassis
3/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
7/29/2019 Lab1 chassis
4/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 /h ome /CPSEC1/
4/11
7/29/2019 Lab1 chassis
5/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 ome/ CPSEC1/ is called the pathname.
4. List the contents of the current directory (ls command)
Command Description
ls List names of all files in current directory
ls filename List only the names files
ls -t List in time order, most recent first
ls -l List long
ls -u List by time last used
ls -r List in reverse order
ls -a 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
What is Home Directory???When you open a terminal window, your current working directory is your home
director . Your home director has the same name as our user-name.
7/29/2019 Lab1 chassis
6/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
7/29/2019 Lab1 chassis
7/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
7/29/2019 Lab1 chassis
8/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] $
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
catcommand is short for concatenate files.
Let us create a file Poem using the command cat. Type the command at the commandprompt
[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, UNIXsaves the contents typed so far and saves in the file named poem1. To see the contents ofthis file, you can use cat command
[CPSEC1@localhost ~] $cat Poem1
Syntax of displaying content of a file:cat We can concatenate the contents of two files using cat command. You need to create onemore file Poem2 using cat with the following content.
8/11
Observe the two prompts above.o Before you enter the command you can see a ~ symbol towards the end of the
prompto 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/29/2019 Lab1 chassis
9/11
Let us concatenate the contents of the two files (Poem1 and Poem2) and store the result inthe file named Poem. The cat command is issued as follows.
[CPSEC1@localhost ~] $ cat Poem1 Poem2 > Poem
7-b) Copying FilesWe can use the copy (cp) command to copy the contents of one file into another file or Copyfiles from one location to another.
Syntax
cp [OPTION1] [OPTION2] ... SOURCE DESTINATION
OPTIONS are optional
OPTIONS PURPOSE
-f if an existing destination file cannot be opened, remove it and try again
-i prompt before overwrite
-l link files instead of copying-u copy only when the SOURCE file is newer than the destination file or
when the destination file is missing
-R or r copy directories recursively
For instance, to copy the contents of the file firstwill into the file finalwill, we use the cpcommand as follows.
cp firstwill finalwill
After issuing the command, you may see a new file finalwill created in the current directorywith 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 thefile exist and used as the destination of cp command, the contents of this file will beoverwritten.The syntax of cp command is as follows
cp source destination
source refers to the source filename, and destination refers to the destination filename ordestination directory. If the destination is a directory, the source file is copied into thedestination directory with the same name as source. Thesource and destination can refer toabsolute 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 thenew location.Syntax
mv[OPTION1] [OPTION2] ... SOURCE DESTINATION
OPTIONS are optional
OPTIONS PURPOSE
-f This option replaces the file / directory if it exists already in the destinationwithout prompting to the user. Note that this is the default if the standard input
9/11
7/29/2019 Lab1 chassis
10/11
is not a terminal.
-i This option prompts us, if we are trying to replace a file/directory in thedestination.
Examplesmv finalwill finalwish
Moves file named finalwill to finalwish. The file named finalwill will not beavailable in the system after this operation.
mv myfile exercises/
Moves the file named myfile to the directory exercises. The file named myfile will beavailable 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 firstfrom the current directory and writes into thecurrent 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 fspecified in the command will replace the existing file third with the new one. But, thecommand
mv i first third
will notify you before it attempts to replace the existing file, and will prompt you if you aresure 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 directoriesTo remove a file we use the rm command.
Syntax
rm [OPTION1] [OPTION2] ... [filenames | directory]
OPTIONS PURPOSE
-f Remove all files (whether write-protected or not) in a directory withoutprompting the user. In a write-protected directory, however, files are neverremoved (whatever their permissions are), but no messages are displayed. Ifthe removal of a write-protected directory is attempted, this option will notsuppress an error message.
-i Interactive. With this option, rm prompts for confirmation before removingany files. It over- rides the -f option and remains in effect even if the standardinput is not a terminal.
10/11
7/29/2019 Lab1 chassis
11/11
-r or -R Recursively remove directories and subdirectories in the argument list. Thedirectory will be emptied of files and removed. The user is normally promptedfor removal of any write-protected files which the directory contains. Thewrite-protected files are removed without prompting, however, if the -f optionis 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. Ifthe removal of a non-empty, write-protected directory is attempted, the utilitywill always fail (even if the -f option is used), resulting in an error message.
Examples
To remove a file named help we use the commandrm help
To remove a directory namedexerciseswe use the commandrmdir exercises
To avoid inadvertently deleting a file, always use the rm command together with its -i option.
rm -i filenameThis 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 thedirectory 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 rmdircommand. If the directoryto 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
Top Related