UNIX Linux Basics Final
UNIX Linux Basics Final
UNIX Linux Basics Final
Objectives
Unix history.
Architecture of Unix/Linux. Important files and commands.
low-level (first layer loaded) software which handles the interface to system hardware (input/output devices, memory, file system, etc), schedules tasks, and provides common core services such as a basic user interface.
Colloquially,
all of the software that comes with a system before applications are installed.
What is Unix?
A portable, multi-tasking and multi-user operating system
Portable: runs on many different hardware architectures (Intel x86 and IA-64, Alpha, MIPS, HP PA-RISC, PowerPC, IBM S/390, SPARC, Motorola 680x0, etc.). Preemptive multi-tasking: several programs can run at the same time (time slices, interrupts, and task switching). Multi-user: many users can share the computer system at the same time.
4
Uses a simple, uniform file model which includes devices and access to other services in a flexible, hierarchical file system. Written in a high-level language (C) making it easy to read, understand, change and port. The command prompt is a simple user process, the Unix shell, which is also a convenient job programming language. Includes support for regular expressions which are convenient for complex searching.
5
Unix History
1964 joint project between AT&T Bell Labs, GE, and MIT to develop a new OS. Goal : develop an OS that could provide computational power, data storage and the ability to share data among multiple users. Result: Multiplexed Information & Computer Service - MULTICS.
Unix History
Two Bell Lab scientists, Ken Thompson and Dennis Ritchie, continue research. They were still left without a Convenient interactive computing service*.
* Ritchie, D.M. The Evolution of the Unix Time-sharing System, AT&T Bell Laboratories Technical Journal, Oct. 1984, Vol 63, No.8, Part 2, pp. 1577-1594.
Unix History
At the same time Ken Thompson wrote a game space travel in Fortran to run on GECOS OS (Honeywell 635).
The spaceship was hard to control and it was expensive to run. He was told to get the game off his work computer.
9
Unix History
Thompson ported the game to a little used PDP-7 computer. Unics (later Unix) was born as a pun on Multics.
10
11
Unix History
12
Linus Torvalds
1991 Linux 0.02 is first released to the public. 1994 Linux 1.0 is released.
14
your computer hardware and provides a library (POSIX) interface for user-level software. The Linux kernel runs on many platforms (Intel x86 and IA-64, Alpha, MIPS, HP PARISC, PowerPC, IBM S/390, SPARC, Motorola 680x0, etc.).
GNU/Linux
the GNU/Linux OS and lots of other software to make Linux easy to install, configure, and use (at least for the target audience).
The freedom to run the program, for any purpose. The freedom to study how the program works, and adapt it to your needs. The freedom to redistribute copies so you can help your neighbor. The freedom to improve the program, and release your improvements to the public, so that the whole community benefits.
What is GNU?
GNU stands for GNU's Not Unix. The three letter abbreviation is not a joke. It emphasizes a major projec of the Free Software Foundation (FSF) that really created the LINUX operating system with many of its popular tools. Richard Stallman created FSF, in order to encourage the development and use of freely redistributable code. Freely means the freedom of redistributing your code under certain conditions. It does NOT mean zero financial cost! The Gnu Public License (GPL) defines the terms and conditions of redistributing the LINUX kernel and other tools that make it usable, forming a LINUX distribution.
Architecture of Unix
Kernel:
Schedules programs, Manages data/file access and storage, Enforces security, Performs all hardware access.
First program run by kernel on booting. Presents each user a prompt, Interprets commands typed by user, Executes users commands, Provides user/programming environment.
19
Init: Shell:
Architecture of Unix
bin
usr
dev
home var tmp
etc
bin
spool
(users accounts) mail
log
X11R6
(users mail)
21
Typical Directories
/:
Typical Directories
tmp: holds temporary files.
23
Typical Directories
24
25
Unix stores a file's administrative information (its physical location on disk, permissions including ownership and modification times) in an inode (inode or Index Node). The file name (link) is stored in the contents of a directory entry. Deleting a file consists of removing a link to the inode (the inode itself is not deleted).
26
GNOME
30
31
Shell
Program that interacts with kernel Bridge between kernel and the user Command interpreter User can type command and the command is conveyed to the kernel and it will be executed
Types of Shell
To view the current shell that is being used, type echo $SHELL at the command prompt
UNIX COMMANDS
UNIX Commands
A command is a program which interacts with the kernel to provide the environment and perform the functions called for by the user. A command can be: a built-in shell command; an executable shell file, known as a shell script; or a source compiled, object code file.
The shell is a command line interpreter. The user interacts with the kernel through the shell. You can write ASCII (text) scripts to be acted upon by a shell.
To execute a command, type its name and arguments at the command line
ls -l /etc
Command name
Options (flags) Arguments
Standard Files
Redirecting Output
Redirecting Input
Common Commands
pwd - print (display) the working directory cd <dir> - change the current working directory to dir ls - list the files in the current working directory ls -l - list the files in the current working directory in long format
File Commands
rm <file>
More Commands
who
whoami
ps
List who is currently logged on to the system Report what user you are logged on as List your processes on the system List all the processes on the system
ps aux
More Commands
ar - Maintain archive libraries: a collection of files (usually object files which may be linked to a program, like a CMS TXTLIB)
More Commands
awk - a file processing language that is well suited to data manipulation and retrieval of information from text files chown - sets the user ID (UID) to owner for the files and directories named by pathname arguments. This command is useful when from test to production
More Commands
diff - attempts to determine the minimal set of changes needed to convert a file specified by the first argument into the file specified by the second argument find - Searches a given file hierarchy specified by path, finding files that match the criteria given by expression
More Commands
grep - Searches files for one or more pattern arguments. It does plain string, basic regular expression, and extended regular expression searching
More Commands
kill - sends a signal to a process or process group You can only kill your own processes unless you are root
C STIME TTY TIME CMD 2 14:34 ttyp0 00:00:00 sleep 10h 0 14:34 ttyp0 00:00:00 ps -ef kill 6715 sleep 10h
UID PID PPID root 6715 6692 root 6716 6692 [root@penguinvm log]# [1]+ Terminated
More Commands
make - helps you manage projects containing a set of interdependent files (e.g. a program with many source and object files; a document built from source files; macro files) make keeps all such files up to date with one another: If one file changes, make updates all the other files that depend on the changed file Roughly the equivalent of VMFBLD
More Commands
sed - applies a set of editing subcommands contained in a script to each argument input file
Pipes
An important early development in Unix was the invention of "pipes," a way to pass the output of one tool to the input of another. eg. $ who | wc l By combining these two tools, giving the wc command the output of who, you can build a new command to list the number of users currently on the system
cat file
cd directory1 cd /usr/bin cd
ls ls /usr/bin
List the files in the current directory List the files in the /usr/bin directory Print file1 out , lpr file1 file2 .. Print a number of files out
mv file1 file2 ... dir Move a number of files into a directory mv dir1 dir2 Move or rename a directory
rm file
Remove a file
Remove a number of files Remove a directory include the sub-directory Remove a directory
rmdir directory
DOS Command Unix Descriptions Command Change directory CD cd CHKDSK du Disk usage CLS clear Clear the current screen COPY cp Copying files DEL rm Removing files or directorie File listing of directories DIR ls MD mkdir Create a directory MORE more Type out a file with paging PRINT lpr Print out a file rmdir RD Remove a directory mv RENAME Moving files around cat TYPE Type out files
finger user Find out the personal information of a user finger name Try to find the persons info. by his/her name finger email-address Try to find the persons info across the network write user Write a message on somebodys screen talk user Talk to the person logging in the same system with you talk email-address Talk to somebody logging in the network date Display todays time and date cal year Display the calendar of the specified year (e.g. 1997)
Shell Scripting
Open a file with extension .sh using vi editor We can type any number of commands that we use to type at command prompt Save the file Execute the file
sh file.sh ./file.sh (if the file has execution permission)
VI Editor
Popular text editor Just type vi <<filename>> at the prompt and hit the enter key. A new file will be opened Type the contents needed and save To save, press the Esc Key and then press : (colon) w q and then enter To quit with out saving Esc + : + q and then enter
Vi editor
Navigation Left - h Down -j Up - k Right -l Top of the screen H (shift + h) //caps lock will not work Middle of the screen M (shift + m) Bottom of the screen L (shift + l) $ - End Key, 0 Home Key Edit Commands Cut X, x Copy yy, yw Paste P, p
Vi editor
X cuts the letter after cursor position x cuts the letter before cursor position yy Copies a line at which cursor is currently positioned yw Copies a word at which cursor is currently positioned P Pastes the copied content after cursor position p Pastes the copied content before cursor position dd Deletes a line
Shell Scripts
To Print a line
echo Hello World (Prints Hello World in the screen)
To read a line
read n (Stores the content entered by user in variable n
To Comment a line
# This is a comment Only single line comment is available. For multi line comment, we need to use # symbol in lines which we want to comment.
Conditions
We can have if, if else, if elif else and case statements (Nested if statements are also possible
1. if [ condn ] then fi 2. if [ condn ] then else fi 3. if [ condn ] then elif [ condn ] then else fi
Conditions (Case)
case expr in Option1) stmt ;; Option2) stmt ;; *) stmt ;; esac
Every option should be terminated with a double semicolon. Denotes default case Case should be termniated with esac
Loops
For loop
for i in 1 2 3 4 5 //Loops 5 times do Body of the loop done
While Loop
while [ condn ] do body of the loop done We need to ensure that the while loop condition is terminated in a finite way
Comparison
-eq : equal to -ne : not equal to -lt : less than -gt : greater than -le : less than or equal to -ge : greater than or equal to
Comparison
Arrays
Initialising an array
A[0] = 10 A[1] = Hi
Using an array
${A[0]} : retrieves 10
Here arrays can contain data belonging to different data types Uninitialised index in arrays will have null value by default
Functions
Local Function
Written at the command prompt Lasts for the current session alone
Written in .bashrc file Available as long as the definition is there in .bashrc file
Global Function
Parameters
Sample function
Functionname() { echo $1 }
Calling function: Functionname Ram Result: Ram
We can pass any number of parameters like this $0 File name $1,$2 - first, second parameters and so on $? stores the status (success or failure status of the last command that was executed) $# - stores the number of parameters passed through the command line #pwd /home #echo $? 0 // As the last command is success #abc Error:command not found #echo $? 127 // as the last command didnt execute successfully
C Program in Linux
Open a file with extension .c from the command prompt using vi editor vi hello.c
Type the contents and save (Esc : wq!) Compile the file gcc hello.c