Heading Goes Here

Download Report

Transcript Heading Goes Here

Introduction to
UNIX and Linux






Written by Dennis Ritchie and Ken Thomsom at
Bell Labs in 1969
Initially written in assembly language and a
high-level language called B. Later converted
from B to C language.
Linux written by Linus Torvalds (an
undergraduate student at the Univ. of Helsinki,
Finland) in 1991.
IEEE developed a standard for UNIX, called
POSIX, that most versions of UNIX now support.
Most popular operating systems
Internet runs on UNIX and Linux
2
UNIX is basically a Simple Operating System
But YOU have to be a GENIUS to understand the Simplicity
Dennis Ritchie
3



UNIX has a hierarchical file system structure
consisting of a root directory with other
directories and files hanging under it
In a command-line user interface, typed
commands are used to navigate the system
Directories and files are specified by pathnames
speicfying the location of the file


cmp320/assignments/assign1.c
/home/students/courses
4
/
bin
dev
home
faculty
…
…
sbin
usr
students
5
faculty
ali
…
personal
asif
…
…
courses
cmp320
…
zia
ICS370
6
/
The root directory is the directory that
contains all other directories. When a
directory structure is displayed as a
tree, the root directory is at the top.
/bin This directory holds binary executable
files that are essential for correct
operation of the system
/boot This directory includes essential
system boot files including the kernel
image .
7
/dev
This directory contains the devices
available to on the machine
/etc
Linux uses this directory to store
system configuration files
/home This is where every user on a Linux
system has a personal directory
/lib
Shared libraries and kernel modules
are stored in this directory
8
/root
/sbin
/tmp
The home directory for the
superuser
Utilities used for system
administration (halt, ifconfig, fdisk,
etc.) are stored in this directory
Used for storing temporary files.
Similar to C:\Windows\Temp.
9
/usr
/var
Typically a shareable, read-only
directory. Contains user
applications and supporting files
for those applications.
This directory contains variable
data files such as logs (/var/log),
mail (/var/mail), and spools
(/var/spool) among other things.
10
• A shell command can be internal/built-in or External
• The code to execute an internal command is part of the shell
process, e.g., cd, pwd.
• The code to process an external command resides in a file in the
form of a binary executable program file or a shell script, e.g.ls,
mkdir.
• The general syntax of a shell command is
command [option(s)] [argument(s)]
• To get the help against the use of a command, type
man command
11
Browsing the File Hierarchy








ls
cd
pwd
mkdir
rmdir
cp
mv
rm
Display contents of a directory
Change directory
Print working directory
Create directory
Remove directory
Copy file
Move file
Remove file
12
Browsing the File Hierarchy
ls (list)
The ls command lists the contents of your current
working directory.
% ls (short for list)
Ls does not show those files whose name s not begin
with a dot (.) Files beginning with a dot (.) are known
as hidden files and usually contain important
program configuration information.
To list all files in your home directory including
those whose names begin with a dot, type
% ls -a
13
Browsing the File Hierarchy
mkdir (make directory)
% mkdir unixstuff
cd (change directory)
• To change to the directory you have just made, type
• % cd unixstuff
mkdir ~/courses/OS/programs
Create the ‘programs’ directory in your
~/courses/OS
directory
• (..) means the parent of the current directory, so typing % cd
..
will take you one directory up the hierarchy (back to
your home directory).
• Typing cd / will take you to the root.
• Current working directory (.)
•Note: typing cd with no argument always returns you to
your home directory. This is very useful if you are lost in the
14
file system.
Browsing the File Hierarchy
~ (your home directory)
Home directories can also be referred to by the tilde
~ character. It can be used to specify paths starting
at your home directory. So typing
% ls ~/unixstuff will list the contents of your unixstuff
directory, no matter where you currently are in the file
system.
pwd (print working directory)
Pathnames enable you to work out where you are in
relation to the whole file-system.
% pwd
The full pathname of your current
directory will be displayed.
15
Important Shell Commands
cp (copy)
cp file1 file2 makes a copy of file1 in the current working
directory and calls it file2
mv (move)
mv file1 file2 moves (or renames) file1 to file2
This has the effect of moving rather than copying the file. It
can also be used to rename a file, by moving the file to the
same directory, but giving it a different name.
rm (remove), rmdir (remove directory)
To delete (remove) a file, use the rm command.
You can use the rmdir command to remove a directory (make
sure it is empty first).
16
Command
date
cal
whoami, who,
Passwd
man [-k]
clear
logout/exit/^D
Description
Prints or sets the system date and time
Displays the calendar for a specified month
or a year
User Information look up programs
Used to change the user password
Displays online documentation
Clear the terminal screen
17
vi / vim Editor
History:
Vim is a text editor released by Bram Moolenaar in 1991 for
the Amiga computer. The name "Vim" is an acronym for "Vi
IMproved“ because Vim was created as an extended version
of the vi editor, with many additional features designed to be
helpful in editing program source code.
Introduction:
•Vim editor is very useful for editing programs and other plain
ASCII files. All commands are given with normal keyboard
characters.
•Vim is case sensitive.
18
vi / vim Editor
History:
Vim is a text editor released by Bram Moolenaar in 1991 for
the Amiga computer. The name "Vim" is an acronym for "Vi
IMproved“ because Vim was created as an extended version
of the vi editor, with many additional features designed to be
helpful in editing program source code.
Introduction:
•Vim editor is very useful for editing programs and other plain
ASCII files. All commands are given with normal keyboard
characters.
•Vim is case sensitive.
19
vi / vim Editor
Opening / Creating a File
$vim practice
The vim editor has three modes:
1. Command/Normal mode: This is the default mode,
whenever you starts vim, the vim starts in this mode. This
mode is used to give commands to vim, eg. save file,
copy/paste/delete text, quit etc. You can switch to this mode
by pressing the ESC key.
2. Insert mode: In this mode text is written/inserted in the
file. One can enter insert mode by pressing "i", "I", "a",
"A", "o", or "O".
3. Execute mode / Last line mode: From command mode
you can enter this mode by typing ":" which puts the
command line entry at the foot of the screen.
20
vi / vim Editor
Editing a File
Press i
(move command mode to insertion mode)
Exiting the Editor
Press Esc. Followed by:
:wq
:eq
:w
:e
Write to file and quit
Exit without saving
write(save) changes without quitting
Abandon changes since last save
21
Writing a C program
$ vi prog1.c
Press i to move command mode to insertion mode
Type the following program.
#include <stdio.h>
int main(){
printf(“Hello World \n”);
}
Press Esc. Followed by :wq to save the file and exiting from
the editor.
22
Compiling and Running C program
Use any editor to type your program and then to compile use gcc compiler:
$ gcc prog1.c
This will create an executable file a.out in the pwd. Now to execute the file
$ ./a.out
If you just type $ a.out, it will say a.out not found. So either use ./ before the exe
name
Once you compile another program in the same directory, the executable name is
again a.out , which will overwrite the previous executable file. To over come this
use –o flag when compiling your source file.
$ gcc prog1.c –o prog1
Now this will create an executable file with the name as the second argument
i.e., prog1 in this case. To execute the file give following command.
$ ./prog1
23