0% found this document useful (0 votes)
16 views17 pages

Lab 1 Matl and Exr

The document provides instructions for students regarding lab sessions, emphasizing the importance of coming prepared and maintaining observation notebooks. It also introduces Linux OS, detailing its features, command line usage, file management, and basic commands for file manipulation and programming. Additionally, it covers the vi editor commands and how to access Linux hosting accounts.

Uploaded by

kireetiv2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views17 pages

Lab 1 Matl and Exr

The document provides instructions for students regarding lab sessions, emphasizing the importance of coming prepared and maintaining observation notebooks. It also introduces Linux OS, detailing its features, command line usage, file management, and basic commands for file manipulation and programming. Additionally, it covers the vi editor commands and how to access Linux hosting accounts.

Uploaded by

kireetiv2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

Instructions to students

1. Students should come in uniform and wear their ID cards


2. Students are expected to come fully prepared for an evaluation lab session.
Hence use of material/handouts/tutorials in any form is strictly discouraged during
these sessions.
3. 2 Evaluation labs and 1 exam lab per term is expected to be conducted. The
remaining labs are to be made as practice sessions. Quizzes may be conducted
during these sessions.
4. Each student should come prepared for all the corresponding topics for
practice/Evaluation in the lab.
5. Material/handouts/tutorials along with exercises may be given during practice
sessions.
6. Students are requested to maintain separate observation notebook for the lab
course.
7. The output of practice lab questions needs to be recorded in the lab observation
notebook.
Introduction to LINUX OS

Linux is an Operating System which was developed by Linus Torvalds


Linux is a 32 bit multi-tasking, multi-user operating system. Linux is an
Open Source System - the source code is available, allowing any good
programmer to tailor the system for his/her needs, or provide an in-depth
support, something that cannot be done with other major operating systems.

Linux Filesystem: Linux filesystems are very different from Windows.


UNIX/Linux is organized in a hierarchy, starting with "slash." Slash is this: /
and it is called as root directory. The rest of the directories and
subdirectories continue down from root.

/
(root)

bin usr boot dev lib etc


Linux Command Line
The shell is where commands are invoked. A command is typed at a
shell prompt
Prompt usually ends in a dollar sign ($)
After typing a command press Enter to invoke it
The shell will try to execute the command. Another prompt will appear.
To display date and Time
$ date
Thu Jun 14 12:28:05 BST 2001
Clear screen using clear
$ clear
To find out username
$ whoami
To list all the users who have currently logged in
$ who
To see the calendar for the current year
$ cal
To see the calendar for any year
$ cal 2005
To see the calendar for the any month any year
$ cal 04 2003
Display a message using echo
Most commands take parameters (also known as arguments) as inputs
For example, echo simply displays its arguments:
$ echo
$ echo Hello there
Hello there
Commands are case-sensitive
Usually lower-case
Working with Files

Data can be stored in a file. Each file has a filename. A label referring to
a particular file

Permitted characters include letters, digits, hyphens (-), underscores (_),


and dots (.)

Case-sensitive — NewsCrew.mov is a different file from NewScrew.mov

Creating Files: There are many ways of creating a file. One of the
simplest is using the cat command:
$ cat > shopping_list
cucumber
bread
yoghurts
fish fingers

Note the greater-than sign (>) — this is necessary to create the file. The
text typed after the file name is written to a file with the specified name
Press Ctrl+D after a line-break to denote the end of the file and the shell
prompt gets displayed denoting the end of successful file creation
process

To append more contents to an existing file, use >> instead of >


login4$ cat >> shopping_list
salad
juice
dessert
CTRL-D
Listing the names of files
The ls command lists the names of files
$ ls
$ ls –l
drwxrwxr-x 2 fred users 4096 Jan 21 10:57 Accounts
-rw-rw-r-- 1 fred users 345 Jan 21 10:57 notes.txt

This command displays a columnar output as shown.

First column indicates whether it is a directory or which type of file it is and


its permissions for the three types of users i.e. users or owners, group and
others. If the first letter is d it is a directory. If it is – the it is a file.
Second column indicates the number of links
Third column username
Fourth column indicates the user’s group.
Fifth column is the file size in bytes.
Sixth column is month and day
Seventh column is time at which file or directory is created.
Eighth column displays the filename or directory name.

To display the subdirectories within the directory:

$ ls –R directoryname
Using wild characters ? and *
? stands for a single character and * for any number of characters.
To list all the filenames and directory names beginning with a
$ ls –l a*
To list all the filenames and directory names beginning with ‘a’ and
containing three or more characters.
$ ls –l a??
To list all the filenames and directory names with second letter ‘a’ and
containing any number of characters.
$ ls –l ?a*

Displaying a File’s Contents with cat

There are many ways of viewing the contents of a file One of the simplest is
with the cat command:
$ cat shopping_list
cucumber
bread
yoghurts
fish fingers

Deleting Files with rm

To delete a file, use the rm (‘remove’) command. Simply pass the name of
the file to be deleted as an argument:
$ rm shopping_list
The file and its contents are removed. There is no recycle bin. There is no
‘unrm’ command. The ls command can be used to confirm the deletion

Copying and Renaming Files with cp and mv


To copy the contents of a file into another file, use the cp command:
$ cp Amrita Amritapuri

To rename a file use the mv (‘move’) command:

$ mv Coimbatore Mumbai
Similar to using cp then rm For both commands, the existing name is
specified as the first argument and the new name as the second. If a file with
the new name already exists, it is overwritten.

Working with Directories

Creating directories using mkdir


Directories (Folders) is used to organize our files. We can use mkdir
command to create directories and subdirectories.
$ mkdir AMRITA

To change directory using cd

When we login by default every user is in a directory referred to as home


directory. That is created when the user is created.
For changing directory we use cd command.
$ cd AMRITA
To change to the previous directory we use
$ cd ..
.. stands for previous directory. . stands for current directory
To come back to the home directory we use
$ cd
To see the present working directory we use pwd.
$ pwd

Removing directories using rmdir

To remove a directory first and foremost the directory should be empty, and
we should be in the previous directory.
$ rmdir AMRITA

Command History
Often it is desired to repeat a previously-executed command. The shell keeps
a command history for this purpose. Use the Up and Down cursor keys to
scroll through the list of previous commands. Press Enter to execute the
displayed command. Commands can also be edited before being run. This is
particularly useful for fixing a typographic error in the previous command.
The Left and Right cursor keys navigate across a command. Extra characters
can be typed at any point. Backspace deletes characters to the left of the
cursor Del and Ctrl+D delete characters to the right Take care not to log out
by holding down Ctrl+D too long

To connect to your account, please follow these steps in windows


Double-click putty.exe to launch the program.
Enter the IP address of your website in the Host Name (or IP Address) field.
IP address is 172.17.9.15

Click on Open button.


Once the session opens you will be prompted to enter your username and
password. Once you login you will be able to manage your linux hosting
account.
Password:

7. Type your login name press Enter Key.


8. Type your password press Enter Key .Password will not be visible.
9. You will get a $ symbol
10. There you can try out all the Linux commands.
Start of with C programming
1.In the vi editor type the C program, and save the file ,quit vi .
$vi message.c

2.To compile the C program


$ cc message.c

3.To run the program


$./a.out

To store the output in any other file


$cc –o messout message.c

To run the program


$ ./message

Vi Quick Reference
Entering vi
% vi name edit name at top
% vi +n name edit name at line n
% vi + name edit file at the end
% vi -r list saved files
% vi -r name recover file name
% vi name1 name2 edit name1 first, rest via :n
% vi +/pat name search file for pat
File Manipulation
:w write back changes
:wq write and quit
:q quit
:q! quit and discard changes
:e name edit the file name
:e! reedit, discard changes
:e +n edit starting at line n
:w name write file name
:w! name overwrite file name
:n edit next file in arglist
Cursor Motion
back forward
letter ^H, h <space>
word end E, e
word beginning B, b W, w
sentence ( )
paragraph { }
section [[ ]]
line ends 0 $
1st char - +
same column ^P ^N, <linefeed>
specified line <line#>G <line#>G
1/2 screenful jump ^U ^D
screenful jump ^B ^F
Undo
u undo last change
U restore current line
"#p retrieve #th last delete
<esc> abandon incomplete command
:q! (drastic) quit without save
Insert
i before cursor
I before first nonblank
a after cursor
A after line
o open line below
O open line above
p paste last cut
<esc> terminates insert

Change
cw
<newword> change word
C substitute line
s<char> substitute char
S substitute lines
r<char> replace char
R replace chars (overstrike)
xp transpose chars (cut paste)
<esc> terminates change

Delete
x char under cursor
X char before cursor
dw word
de delete word but leave punctuation
dd line
#dd # lines
D rest of line

Delete during Insert


^H last char
^W last word
@ all input this line

Miscellaneous Operations
J join lines
:sh run shell, then return
:!cmd execute a command, then return
:f show current file and line
^g same as :f
Numbers etc.
. (period) repeat last action
3. repeat it 3 times
3dd delete 3 lines
d3w or 3dw delete 3 words
3rx replace next 3 chars with x
The vi editor is a shell editor for Unix systems. It makes use of a
regular keyboard with an Escape key. On a DEC station the Escape key
is the F11 key. It works on all Unix computers. Use a Telnet or SSH
client and login to your web server. Enter your username and
password, then type
su -
at the command line. Enter your root password to get access to root.
Complete documentation is available by typing: man vi
START SESSION
vi filename (filename is the file to be edited)
UNDO
u - undo the last command
SCREEN COMMANDS (CTRL-X means pressing the CTRL key and the
X key
simultaneously)
CTRL-l - reprint current screen
CTRL-L - expose one more line at the top of the screen
CTRL-E - expose one more line at the bottom of the screen
CTRL-F - go forward one screen
CTRL-B - go back one screen
CTRL-D - page down half a screen
CTRL-U - page up half a screen

CURSOR POSITIONING COMMANDS


j - move cursor down one line, same column
k - move cursor up one line, same column
h - move cursor back one character
l - move cursor forward one character
RET - move cursor to beginning of next line (RET is the Return key)
0 - move cursor to beginning of current line
$ - move cursor to end of current line
SPACE - move cursor forward one character (SPACE is the Space bar)
nG - move cursor to the beginning of line n (Default is the last
line of the file)
:n - move cursor to the beginning of line n
b - move the cursor backward to the beginning of the previous word
e - move the cursor backward to the end of the previous word
w - move the cursor forward to the next word
/pattern - move cursor forward to the next occurrence of a pattern
?pattern - move cursor backward to the next occurrence of a pattern
n - repeats the last / or ? pattern search.

TEXT INSERTION COMMANDS


a - append text after the cursor (press ESC key to terminate the
command)
A - append text at the end of the line (press ESC key to terminate
the command)
i - insert text before the cursor (press ESC key to terminate the
command)
I - insert text at the beginning of the line (press ESC key to
terminate the command)
o - open a new line below the current line for text insertion
(press ESC key to terminate the command)
O - open a new line above the current line for text insertion
(press ESC key to terminate the command)
DEL - overwrite the last character during text insertion (DEL is
the Delete key)
ESC - stop text insertion. (ESC is the Escape key, on a DEC station
this is the F11 key)

TEXT DELETION COMMANDS


x - delete the current character
dd - delete the current line
dw - delete the current word
d) - delete the rest of the current sentence
D or d$ - delete from cursor to end of line
P - put text back from previous delete

TEXT MODIFICATION COMMANDS


cw - change characters of current word (until terminated with the
ESC key)
c$ - change text up to the end of the line
C or cc - change remaining text on current line (until terminated
with the ESC key)
~ - change case of the current character
xp - transpose current and following characters
J - join current line with next line
s - delete the current character and go into insertion mode
rx - replace the current character with x
R - replace the following characters (until terminated with the ESC
key)

CUT AND PASTE COMMANDS


yy - put the current line in a buffer (does not delete the line
from its current position)
p - places the line in the buffer after the current position of the
cursor

APPEND FILE COMMANDS


:R filename - insert the file filename where the cursor was before
the : was typed

EXIT COMMANDS
ZZ - exit vi and save changes
:wq - write changes to current file and quit editing session
:q! - quit editing session (no changes are made)

Additional Commands:

--help option
With almost every command, ‘--help’ option shows usage summary for that command.

$ date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

whatis command
This command gives one line description about the command. It can be used for quick
reference for a command.

$ whatis date
date (1) - print or set the system date and time

$ whatis whatis
whatis (1) - display manual page descriptions

Manual Pages
‘--help’ option and ‘whatis’ command do not provide thorough information about the
command. For detailed information, Linux provides man pages and info pages. To see a
command's manual page, man command is used.

$ man date

The man pages are properly documented pages. They have following sections:

NAME: The name and one line description of the command.

SYNOPSIS: The command syntax.

DESCRIPTION: Detailed description about what a command does.


OPTIONS: A list and description of all of the command's options.

EXAMPLES: Examples of command usage.

FILES: Any file associated with the command.

AUTHOR: Author of the man page

REPORTING BUGS: Link of website or mail-id where you can report any bug.

SEE ALSO: Any commands related to the command, for further reference.

With -k option, a search through man pages can be performed. This searches for a pattern in
the name and short description of a man page.

$ man -k gzip
gzip (1) - compress or expand files
lz (1) - gunzips and shows a listing of a gzip'd tar'd archive
tgz (1) - makes a gzip'd tar archive
uz (1) - gunzips and extracts a gzip'd tar'd archive
zforce (1) - force a '.gz' extension on all gzip files

touch command
For creating an empty file, use touch command.

$ touch file1 file2 file3


$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3

If a file already exists, touch will update its time stamp.There are a lot of other methods to
create a new file, e.g. using a text editor like vi or gedit, or using redirection. Here is an
example of creating a file using redirection:

$ ls -l /usr > usrlisting


$ ls -l
total 8
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting

a file named usrlisting is created in this example.

wc command
Word count

This command counts lines, words and letters of the input given to it.

$ wc /etc/passwd
35 57 1698 /etc/passwd

The /etc/passwd file has 35 lines, 57 words, and 1698 letters present in it.

grep command

Search for a given string in a file (case in-sensitive search).

$ grep -i "the" demo_file

Print the matched line, along with the 3 lines after it.

$ grep -A 3 -i "example" demo_text

Search for a given string in all files recursively

$ grep -r "ramesh" *

history – prints recently used commands

sort – sort lines of text files


Usage: sort [OPTION]... [FILE]...
eg. sort file1.txt
sort r
file1.txt

File Archival Commands:

tar – to archive a file


Usage: tar [OPTION] DEST SOURCE
eg. tar cvf
/home/archive.tar /home/original
tar xvf
/home/archive.tar

zip – package and compress (archive) files


Usage: zip [OPTION] DEST SOURSE
eg. zip original.zip original

unzip – list, test and extract compressed files in a ZIP archive


Usage: unzip filename
eg. unzip original.zip
Exercises

1. A library needs to organize its collection of electronic books on the hard drive. The
books are to be categorized as shown below:

a. Create files called electronics today under library


b. Create machines today and computers today under the respective sub directories
c. Create a sub directory called autobiographies under non-fiction
d. Add my experiments with truth under autobiographies
e. Move electronics today under electronics subdirectory
f. Create the files Indian history and Davinci code under the respective directories
g. Display the details of Library and Computers today magazine in long format
h. Copy computers today file to electronics folder
i. Rename the file indian history to history of India
j. Delete the subdirectory autobiographies

2. Enter the following commands at the prompt and interpret the output:
a. echo hello world
b. passwd
c. hostname
d. last
e. man man
f. Execute the following commands in order and interpret the output:
i. ls
ii. ls > myoutput
iii. ls
iv. cat myoutput
g. man head
h. man tail
i. Execute the following commands in order and interpret the output:
i. ls
ii. ls | head -3
iii. ls | head -3 | tail -1 >newoutput
iv. Display the contents of newoutput

3. Write the calendar of year 2000 to a file called year2k


4. Append the calendar of year 2001 to the file year2k
5. Display the list of all the commands that you have executed so far using a single
command
6. Create a file called abc which contains alphabets from a to e with one character per
line in random order
7. Sort the characters in the file called abc in ascending order and write to a file call
ascabc using a single command (try < and > operators)
8. Repeat the previous question 7 for descending order sorting of the file
9. Create a file called sample with the words – Bat, Goat, Apple, Dog, First, Eat and
Hide
10. Search for the words Apple and Eat using grep and display the output
11. Display all the occurrence of the character a (irrespective of case) from all the lines
using grep and display the output

You might also like