Sie sind auf Seite 1von 41

Microsoft VB.

Net
and ASP.Net

1
Practical No. 01
Enumeration : Enumeration is a related set of constants. They are used when
working with many constants of the same type. It's declared with the Enum keyword.
Output of above code is the image below:

Imports System.Console
Module Module1
Enum Seasons
Summer = 1
Winter = 2
Spring = 3
Autumn = 4
End Enum
Sub Main()
Write("Summer is the" & Seasons.Summer & "season")
End Sub
End Module

2
Practical No. 02

If....Else statement

If conditional expression is one of the most useful control structures which allows us
to execute a expression if a condition is true and execute a different expression if it is
False. The syntax looks like this:

If condition Then
[statements]
Else If condition Then
[statements]
-
-
Else
[statements]
End If

If the condition is true, the statements following the Then keyword will be executed,
else, the statements following the ElseIf will be checked and if true, will be executed,
else, the statements in the else part will be executed.

Imports System.Console
Module Module1
Sub Main()
Dim i As Integer
WriteLine("Enter an integer, 1 or 2 or 3")
i = Val(ReadLine())
'ReadLine() method is used to read from console

If i = 1 Then
WriteLine("One")
ElseIf i = 2 Then
WriteLine("Two")
ElseIf i = 3 Then
WriteLine("Three")
Else
WriteLine("Number not 1,2,3")
End If
End Sub

End Module

3
Practical No. 03

Select....Case Statement

The Select Case statement executes one of several groups of statements depending
on the value of an expression. If your code has the capability to handle different
values of a particular variable then you can use a Select Case statement. You use
Select Case to test an expression, determine which of the given cases it matches and
execute the code in that matched case.

The syntax of the Select Case statement looks like this:

Select Case testexpression


[Case expressionlist-n
[statements-n]] . . .
[Case Else elsestatements]
End Select

Imports System.Console
Module Module1

Sub Main()
Dim keyIn As Integer
WriteLine("Enter a number between 1 and 4")
keyIn = Val(ReadLine())

Select Case keyIn


Case 1
WriteLine("You entered 1")
Case 2
WriteLine("You entered 2")
Case 3
WriteLine("You entered 3")
Case 4
WriteLine("You entered 4")
End Select

End Sub

End Module

4
Practical No. 04
For Loop

The For loop is the most popular loop. For loops enable us to execute a series of
expressions multiple numbers of times. The For loop in VB .NET needs a loop index
which counts the number of loop iterations as the loop executes. The syntax for the
For loop looks like this:

For index=start to end[Step step]


[statements]
[Exit For]
[statements]
Next[index]

The index variable is set to start automatically when the loop starts. Each time in the
loop, index is incremented by step and when index equals end, the loop ends.

Module Module1

Sub Main()
Dim d As Integer
For d = 0 To 2
System.Console.WriteLine("In the For Loop")
Next d
End Sub
End Module

5
Practical No. 05

Arrays
Arrays are programming constructs that store data and allow us to access them by
numeric index or subscript. Arrays helps us create shorter and simpler code in many
situations. Arrays in Visual Basic .NET inherit from the Array class in the System
namespace. All arrays in VB as zero based, meaning, the index of the first element is
zero and they are numbered sequentially.

The following code demonstrates arrays.

Imports System.Console
Module Module1
Sub Main()
Dim sport(5) As String
'declaring an array
sport(0) = "Soccer"
sport(1) = "Cricket"
sport(2) = "Rugby"
sport(3) = "Aussie Rules"
sport(4) = "BasketBall"
sport(5) = "Hockey"
'storing values in the array
WriteLine("Name of the Sport in the third location" & " " & sport(2))
'displaying value from array
End Sub
End Module

6
Practical No. 06
Classes and Objects

Classes are types and Objects are instances of the Class. Classes and Objects are
very much related to each other. Without objects you can't use a class. In Visual
Basic we create a class with the Class statement and end it with End Class. The
Syntax for a Class looks as follows:

Public Class Test

----- Variables
-----Methods
-----Properties
-----Events

End Class

The above syntax created a class named Test. To create a object for this class we use
the new keyword and that looks like this: Dim obj as new Test(). The following
code shows how to create a Class and access the class with an Object. Open a
Console Application and place the following code in it.

Module Module1
Imports System.Console

Sub Main()
Dim obj As New Test()
'creating a object obj for Test class
obj.disp()
'calling the disp method using obj
Read()
End Sub

End Module

Public Class Test


'creating a class named Test
Sub disp()
'a method named disp in the class
Write("Welcome to OOP")
End Sub
End Class

Output of above code is the image below.

7
Practical No. 07

Inheritance

A key feature of OOP is reusability. It's always time saving and useful if we can reuse
something that already exists rather than trying to create the same thing again and
again. Reusing the class that is tested, debugged and used many times can save us
time and effort of developing and testing it again. Once a class has been written and
tested, it can be used by other programs to suit the program's requirement. This is
done by creating a new class from an existing class. The process of deriving a new
class from an existing class is called Inheritance. The old class is called the base class
and the new class is called derived class. The derived class inherits some or
everything of the base class. In Visual Basic we use the Inherits keyword to inherit
one class from other. The general form of deriving a new class from an existing class
looks as follows:

Public Class One


---
End Class

Public Class Two


Inherits One
---
---
End Class

Using Inheritance we can use the variables, methods, properties, etc, from the base
class and add more functionality to it in the derived class. The following code
demonstrates the process of Inheritance in Visual Basic.

8
Imports System.Console
Module Module1

Sub Main()
Dim ss As New Two()
WriteLine(ss.sum())
Read()
End Sub

End Module

Public Class One


'base class
Public i As Integer = 10
Public j As Integer = 20

Public Function add() As Integer


Return i + j
End Function

End Class

Public Class Two


&nbspInherits One
'derived class. class two inherited from class one
Public k As Integer = 100

Public Function sum() As Integer


'using the variables, function from base class and adding more
functionality
Return i + j + k
End Function

End Class

9
Practical No. 08
Math Functions

Visual Basic provides support for handling Mathematical calculations. Math functions
are stored in System.Math namespace. We need to import this namespace when we
work with Math functions. The functions built into Math class helps us calculate the
Trigonometry values, Square roots, logarithm values, etc.

The following code puts some Math functions to work.

Imports System.Console
Imports System.Math
Module Module1

Sub Main()
WriteLine("Sine 90 is" & " " & Sin(90))
'display Sine90 value
WriteLine("Square root of 81 is " & " " & Sqrt(81))
'displays square root of 81
WriteLine("Log value of 12 is" & " " & Log(12))
'displays the logarithm value of 12
Read()
End Sub

End Module

The image below displays output from above code.

10
Practical No. 09

TextBox Control

Windows users should be familiar with textboxes. This control looks like a box and
accepts input from the user. The TextBox is based on the TextBoxBase class which is
based on the Control class. TextBoxes are used to accept input from the user or used
to display text. By default we can enter up to 2048 characters in a TextBox but if the
Multiline property is set to True we can enter up to 32KB of text. The image below
displays a Textbox. The default event of the TextBox is the TextChanged Event.

RichTextBox

RichTextBoxes are similar to TextBoxes but they provide some advanced features
over the standard TextBox. RichTextBox allows formatting the text, say adding
colors, displaying particular font types and so on. The RichTextBox, like the TextBox
is based on the TextBoxBase class which is based on the Control class. These
RichTextBoxes came into existence because many word processors these days allow
us to save text in a rich text format. With RichTextBoxes we can also create our own
word processors. We have two options when accessing text in a RichTextBox, text
and rtf (rich text format). Text holds text in normal text and rtf holds text in rich text
format. Image of a RichTextBox is shown below.

6. Label

Labels are those controls that are used to display text in other parts of the
application. They are based on the Control class. The default event of Label is the
Click event. Notable property of the label control is the text property which is used to
set the text for the label.

Label1.Text = "Label"

11
CheckBox

CheckBoxes are those controls which gives us an option to select, say, Yes/No or
True/False. A checkbox is clicked to select and clicked again to deselect some option.
When a checkbox is selected a check (a tick mark) appears indicating a selection.
The CheckBox control is based on the TextBoxBase class which is based on the
Control class. The default event of the CheckBox is the CheckedChange
event. Below is the image of a Checkbox.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_


System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
TextBox1.Text = "Checked"
Else
TextBox1.Text = "UnChecked"
End If
End Sub

RadioButton

RadioButtons are similar to CheckBoxes but RadioButtons are displayed as rounded


instead of boxed as with a checkbox. Like CheckBoxes, RadioButtons are used to
select and deselect options but they allow us to choose from mutually exclusive
options. The RadioButton control is based on the ButtonBase class which is based on
the Control class. A major difference between CheckBoxes and RadioButtons is,
RadioButtons are mostly used together in a group. The default event of the
RadioButton is the CheckedChange event

12
Below is the image of a RadioButton.

Code to check a RadioButton's state

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_


System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
TextBox1.Text = "Selected"
Else
TextBox1.Text = "Not Selected"
End If
End Sub

13
Database and
Information System

14
Practical No. 01
SQL Syntax

SELECT Company, Country FROM Customers WHERE Country <> 'USA'

SQL Result

Company Country
Island Trading UK
Galería del gastrónomo Spain
Laughing Bacchus Wine Cellars Canada
Paris spécialités France
Simons bistro Denmark
Wolski Zajazd Poland

SQL WHERE

The SQL WHERE clause is used to select data conditionally, by adding it to


already existing SQL SELECT query. We are going to use the Customers
table from the previous chapter, to illustrate the use of the SQL WHERE
command.

Table: Customers

FirstName LastName Email DOB Phone


John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222
Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

If we want to select all customers from our database table, having last name
'Smith' we need to use the following SQL syntax:

15
SELECT *
FROM Customers
WHERE LastName = 'Smith'

The result of the SQL expression above will be the following:

FirstName LastName Email DOB Phone


John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

In this simple SQL query we used the "=" (Equal) operator in our WHERE
criteria:

LastName = 'Smith'

But we can use any of the following comparison operators in conjunction with
the SQL WHERE clause:

<> (Not Equal)

SELECT *
FROM Customers
WHERE LastName <> 'Smith'

> (Greater than)

SELECT *
FROM Customers
WHERE DOB > '1/1/1970'

>= (Greater or Equal)

SELECT *
FROM Customers
WHERE DOB >= '1/1/1970'

16
< (Less than)

SELECT *
FROM Customers
WHERE DOB < '1/1/1970'

<= (Less or Equal)

SELECT *
FROM Customers
WHERE DOB =< '1/1/1970'

LIKE (similar to)

SELECT *
FROM Customers
WHERE Phone LIKE '626%'

17
Practical No. 02
SQL UPDATE

The SQL UPDATE general syntax looks like this:

UPDATE Table1
SET Column1 = Value1, Column2 = Value2
WHERE Some_Column = Some_Value

The SQL UPDATE clause changes the data in already existing database
row(s) and usually we need to add a conditional SQL WHERE clause to our
SQL UPDATE statement in order to specify which row(s) we intend to
update.

If we want to update the Mr. Steven Goldfish's date of birth to '5/10/1974' in


our Customers database table

FirstName LastName Email DOB Phone


John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222
Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

we need the following SQL UPDATE statement:

UPDATE Customers
SET DOB = '5/10/1974'
WHERE LastName = 'Goldfish' AND FirstName = 'Steven'

If we don’t specify a WHERE clause in the SQL expression above, all


customers' DOB will be updated to '5/10/1974', so be careful with the SQL
UPDATE command usage.

We can update several database table rows at once, by using the SQL WHERE
clause in our UPDATE statement. For example if we want to change the
phone number for all customers with last name Smith (we have 2 in our
example Customers table), we need to use the following SQL UPDATE
statement:

18
UPDATE Customers
SET Phone = '626 555-5555'
WHERE LastName = 'Smith'

After the execution of the UPDATE SQL expression above, the


Customers table will look as follows:

FirstName LastName Email DOB Phone


John Smith John.Smith@yahoo.com 2/4/1968 626 555-5555
Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 626 555-5555

19
Practical No. 03
SQL DELETE

The SQL DELETE command has the following generic SQL syntax:

DELETE FROM Table1


WHERE Some_Column = Some_Value

If you skip the SQL WHERE clause when executing SQL DELETE
expression, then all the data in the specified table will be
deleted. The following SQL statement will delete all the data
from our Customers table and we’ll end up with completely empty
table:

DELETE FROM Table1

If you specify a WHERE clause in your SQL DELETE statement, only


the table rows satisfying the WHERE criteria will be deleted:

DELETE FROM Customers


WHERE LastName = 'Smith'

The SQL query above will delete all database rows having LastName
'Smith' and will leave the Customers table in the following
state:

FirstName LastName Email DOB Phone


Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232

20
Practical No. 04
SQL JOIN

The SQL JOIN clause is used whenever we have to select data from 2 or
more tables.

To be able to use SQL JOIN clause to extract data from 2 (or more) tables,
we need a relationship between certain columns in these tables.

We are going to illustrate our SQL JOIN example with the following 2 tables:

Customers:

CustomerID FirstName LastName Email DOB Phone


626
1 John Smith John.Smith@yahoo.com 2/4/1968 222-
2222
323
2 Steven Goldfish goldfish@fishhere.net 4/4/1974 455-
4545
416
3 Paula Brown pb@herowndomain.org 5/24/1978 323-
3232
416
4 James Smith jim@supergig.co.uk 20/10/1980 323-
8888

Sales:

CustomerID Date SaleAmount


2 5/6/2004 $100.22
1 5/7/2004 $99.95
3 5/7/2004 $122.95
3 5/13/2004 $100.00
4 5/22/2004 $555.55

As you can see those 2 tables have common field called CustomerID and
thanks to that we can extract information from both tables by matching their
CustomerID columns.

Consider the following SQL statement:

21
SELECT Customers.FirstName, Customers.LastName,
SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers, Sales
WHERE Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName

The SQL expression above will select all distinct customers (their first and last
names) and the total respective amount of dollars they have spent.
The SQL JOIN condition has been specified after the SQL WHERE clause and
says that the 2 tables have to be matched by their respective CustomerID
columns.

Here is the result of this SQL statement:

FirstName LastName SalesPerCustomers


John Smith $99.95
Steven Goldfish $100.22
Paula Brown $222.95
James Smith $555.55

The SQL statement above can be re-written using the SQL JOIN clause like
this:

SELECT Customers.FirstName, Customers.LastName,


SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName

There are 2 types of SQL JOINS – INNER JOINS and OUTER JOINS. If you
don't put INNER or OUTER keywords in front of the SQL JOIN keyword, then
INNER JOIN is used. In short "INNER JOIN" = "JOIN" (note that different
databases have different syntax for their JOIN clauses).

The INNER JOIN will select all rows from both tables as long as there is a
match between the columns we are matching on. In case we have a customer
in the Customers table, which still hasn't made any orders (there are no
entries for this customer in the Sales table), this customer will not be listed in
the result of our SQL query above.

If the Sales table has the following rows:

22
CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/6/2004 $99.95

And we use the same SQL JOIN statement from above:

SELECT Customers.FirstName, Customers.LastName,


SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName

We'll get the following result:

FirstName LastName SalesPerCustomers


John Smith $99.95
Steven Goldfish $100.22

Even though Paula and James are listed as customers in the Customers table
they won't be displayed because they haven't purchased anything yet.

But what if you want to display all the customers and their sales, no matter if
they have ordered something or not? We’ll do that with the help of SQL
OUTER JOIN clause.

The second type of SQL JOIN is called SQL OUTER JOIN and it has 2 sub-
types called LEFT OUTER JOIN and RIGHT OUTER JOIN.

The LEFT OUTER JOIN or simply LEFT JOIN (you can omit the OUTER
keyword in most databases), selects all the rows from the first table listed
after the FROM clause, no matter if they have matches in the second table.

If we slightly modify our last SQL statement to:

SELECT Customers.FirstName, Customers.LastName,


SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers LEFT JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName

and the Sales table still has the following rows:

23
CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/6/2004 $99.95

The result will be the following:

FirstName LastName SalesPerCustomers


John Smith $99.95
Steven Goldfish $100.22
Paula Brown NULL
James Smith NULL

As you can see we have selected everything from the Customers (first table).
For all rows from Customers, which don’t have a match in the Sales (second
table), the SalesPerCustomer column has amount NULL (NULL means a
column contains nothing).

The RIGHT OUTER JOIN or just RIGHT JOIN behaves exactly as SQL LEFT
JOIN, except that it returns all rows from the second table (the right table in
our SQL JOIN statement).

24
Practical No. 05
SQL AND & OR

The SQL AND clause is used when you want to specify more than one
condition in your SQL WHERE clause, and at the same time you want all
conditions to be true.
For example if you want to select all customers with FirstName "John" and
LastName "Smith", you will use the following SQL expression:

SELECT * FROM Customers


WHERE FirstName = 'John' AND LastName = 'Smith'

The result of the SQL query above is:

FirstName LastName Email DOB Phone


John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

The following row in our Customer table, satisfies the second of the
conditions (LastName = 'Smith'), but not the first one (FirstName = 'John'),
and that's why it's not returned by our SQL query:

FirstName LastName Email DOB Phone


James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

The SQL OR statement is used in similar fashion and the major difference
compared to the SQL AND is that OR clause will return all rows satisfying any
of the conditions listed in the WHERE clause.

If we want to select all customers having FirstName 'James' or FirstName


'Paula' we need to use the following SQL statement:

SELECT * FROM Customers


WHERE FirstName = 'James' OR FirstName = 'Paula'

25
The result of this query will be the following:

FirstName LastName Email DOB Phone


Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

You can combine AND and OR clauses anyway you want and you can use
parentheses to define your logical expressions.
Here is an example of such a SQL query, selecting all customers with
LastName 'Brown' and FirstName either 'James' or 'Paula':

SELECT * FROM Customers


WHERE (FirstName = 'James' OR FirstName = 'Paula') AND LastName
= 'Brown'

The result of the SQL expression above will be:

FirstName LastName Email DOB Phone


Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232

26
Practical No. 06
SQL ORDER BY

The SQL ORDER BY clause comes in handy when you want to sort your SQL
result sets by some column(s). For example if you want to select all the
persons from the already familiar Customers table and order the result by
date of birth, you will use the following statement:

SELECT * FROM Customers


ORDER BY DOB

The result of the above SQL expression will be the following:

FirstName LastName Email DOB Phone


John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222
Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

As you can see the rows are sorted in ascending order by the DOB column,
but what if you want to sort them in descending order? To do that you will
have to add the DESC SQL keyword after your SQL ORDER BY clause:

SELECT * FROM Customers


ORDER BY DOB DESC

The result of the SQL query above will look like this:

FirstName LastName Email DOB Phone


James Smith jim@supergig.co.uk 20/10/1980 416 323-8888
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

If you don't specify how to order your rows, alphabetically or reverse, than
the result set is ordered alphabetically, hence the following to SQL
expressions produce the same result:

27
SELECT * FROM Customers
ORDER BY DOB

SELECT * FROM Customers


ORDER BY DOB ASC

You can sort your result set by more than one column by specifying those
columns in the SQL ORDER BY list. The following SQL expression will order
by DOB and LastName:

SELECT * FROM Customers


ORDER BY DOB, LastName

28
Networking with Red
Hat Linux &
Wireless
Communication

29
Basic Linux Commands:

Comman
Example Description
d
cat Sends file contents to standard output. This is a
way to list the contents of short files to the
screen. It works well with piping.
cat .bashrc Sends the contents of the ".bashrc" file to the
screen.
cd Change directory
cd /home 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".
cd httpd Change the current working directory to httpd,
relative to the current location which is
"/home". The full path of the new working
directory is "/home/httpd".
cd .. Move to the parent directory of the current
directory. This command will make the current
working directory "/home.
cd ~ Move to the user's home directory which is
"/home/username". The '~' indicates the users
home directory.
cp Copy files
cp myfile yourfile 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.
cp -i myfile yourfile With the "-i" option, if the file "yourfile" exists,
you will be prompted before it is overwritten.
cp -i /data/myfile . Copy the file "/data/myfile" to the current
working directory and name it "myfile". Prompt
before overwriting the file.
cp -dpr srcdir Copy all files from the directory "srcdir" to the
destdir directory "destdir" preserving links (-p option),
file attributes (-p option), and copy recursively
(-r option). With these options, a directory and
all it contents can be copied to another
directory.

dd dd if=/dev/hdb1 Disk duplicate. The man page says this


of=/backup/ command is to "Convert and copy a file", but
although used by more advanced users, it can
be a very handy command. The "if" means
30
input file, "of" means output file.
df Show the amount of disk space used on each
mounted filesystem.
less Similar to the more command, but the user can
less textfile page up and down through the file. The
example displays the contents of textfile.
ln Creates a symbolic link to a file.
ln -s test symlink Creates a symbolic link named symlink that
points to the file test Typing "ls -i test symlink"
will show the two files are different with
different inodes. Typing "ls -l test symlink" will
show that symlink points to the file test.
locate A fast database driven file locator.
slocate -u This command builds the slocate database. It
will take several minutes to complete this
command. This command must be used before
searching for files, however cron runs this
command periodically on most systems.
locate whereis Lists all files whose names contain the string
"whereis".
logout Logs the current user off the system.
ls List files
ls List files in the current working directory except
those starting with . and only show the file
name.
ls -al List all files in the current working directory in
long listing format showing permissions,
ownership, size, and time and date stamp
more Allows file contents or piped output to be sent
to the screen one page at a time.
more /etc/profile Lists the contents of the "/etc/profile" file to the
screen one page at a time.
ls -al |more Performs a directory listing of all files and pipes
the output of the listing through more. If the
directory listing is longer than a page, it will be
listed one page at a time.

mv Move or rename files


mv -i myfile yourfile Move the file from "myfile" to "yourfile". This
effectively changes the name of "myfile" to
"yourfile".
mv -i /data/myfile . Move the file from "myfile" from the directory
"/data" to the current working directory.
pwd Show the name of the current working directory
more /etc/profile Lists the contents of the "/etc/profile" file to the
screen one page at a time.
shutdow
Shuts the system down.
n

31
shutdown -h now Shuts the system down to halt immediately.
shutdown -r now Shuts the system down immediately and the
system reboots.
whereis Show where the binary, source and manual
page files are for a command
whereis ls Locates binaries and manual pages for the ls
command.

32
Practical No. 01
Cal

1. Name
cal - displays a calendar and the date of easter

2. Synopsis
cal [-3jmy] [[month] year]

3. Frequently used options


-3 Print the previous month, the current month, and the next month all on
one row.

4. Examples
cal without any options displays current month in current year:
$ cal

If we would like to see November in 2009:


$ cal 11 2009

How about to see all months of year 2010:


$ cal 2010

33
Cal is also able to predict vary distant future, not sure if any of us will ever
need it, but here is month Jun for year 10001, so make sure that you do not
miss your appointment !:
$ cal 6 10001

Display one months before and after current September 2007 :


$ cal -3 9 2007

34
Practical No. 02
cd:

1. Name
cd - change directory

2. Frequently used options


- change to previous directory

3. Examples
In unix systems there two most frequently used commands, one is "ls" and
the other is "cd". Command "cd" allows us to navigate through file system. To
navigate to /etc/ directory simply enter:
$ cd /etc

To navigate to previous directory we can enter:


$ cd -

By entering cd without any arguments navigate to home directory:


$ cd

4. Relative Path

5. Absolute Path

35
Practical No. 03
Touch

1. Name
touch - change file timestamps

2. Synopsis
touch [OPTION]... FILE...

3. Frequently used options


-a change only the access time
-m change only the modification time
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time

4. Examples
touch command enables us to change access and modification time for a
given files. Also touch command can be use to create files. Lets create file
named touchfile.txt:
$ touch touchfile.txt

To investigate more about this file we can use stat command.


$ stat touchfile.txt

now we can use touch command to change access and modification time to
current time:
$ touch touchfile.txt

To change only modification time we can use -m option:


$ touch -m touchfile.txt

36
Or if we want to change just access time we use -a option
$ touch -a touchfile.txt

Now lets say that we would like to change access and modification time back
to 29 January 2005 13:45.45
$ touch -t 200501291345.45 touchfile.txt

37
Practical No. 04

Tail

1. Name
tail - output the last part of files

2. Synopsis
tail [OPTION]... [FILE]...

3. Frequently used options


-c, --bytes=N
output the last N bytes
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and
--follow=descriptor are equivalent
-n, --lines=N
output the last N lines, instead of the last 10

4. Examples
Lets create sample file. This file will contain names of all directories in /var/.
We can also number each line for better overview.
for f in $( ls /var/ ); do echo $f; done | nl > file1

By default a tail command displays last 10 lines of given file.


tail

To display just last 3 lines from this file we use -n option:


38
tail -n 3 file1

Moreover the same output can be produced by command:


tail -3 file1

To use tail command on byte level we can use -c option. This option will make
tail command to display last 4 bytes (4 characters) if a given file:

tail -c 4 file1

If you wonder why we can see only 3 characters, use od command to see
where the 4th byte is:
tail -c 4 file1 | od -a

Another very useful option for tail command is -f. This option will continuously
display a file as it is dynamically edited by another process. This option is
very useful for watching log files.
tail -f /var/log/syslog

39
Practical No. 05
Time

1. Name
time - run programs and summarize system resource usage

2. Synopsis
time [ -apqvV ] [ -f FORMAT ] [ -o FILE ]
[ --append ] [ --verbose ] [ --quiet ] [ --portability ]
[ --format=FORMAT ] [ --output=FILE ] [ --version ]
[ --help ] COMMAND [ ARGS ]

3. Examples
Measure time for running program time itself:
$ time

Measure execution time of "find" command and redirect stderr to /dev/null:


$ time find / -name resolv.conf 2>/dev/null

40
Practical No. 06
pwd

1. Name
pwd - print name of current/working directory

2. Synopsis
pwd [OPTION]

3. Examples
Pwd linux command prints name of current/working directory.
pwd

Alternative to the pwd command could be dirs which prints the same output:
dirs -l

41

Das könnte Ihnen auch gefallen