Unix Basics: by Rajesh Menghani

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

UNIX BASICS

By Rajesh Menghani
Topics
• Unix Introduction
• The Shell
• Unix Commands
• Files and Directories
• Permissions
• Program and Process
• Make Utility
• Shell Programming
What’s UNIX?
•Name derived from Multics, a multi-user OS

•Invented by AT&T Bell Labs in late 60’s

•Currently there are different versions of UNIX such as


SunOS, Linux, DEC OSF/1, AIX, HP-UX 10, Solaris, IRIX,
SCO UNIX, etc.

•UNIX is multi-user and multi-tasking operating


system.
UNIX Introduction
• The UNIX operating system is made up of three parts;
– the kernel, the shell and the programs.

• The kernel of UNIX is the hub of the operating system:


– it allocates time and memory to programs and handles the file
store and communications in response to system calls.

• The shell acts as an interface between the user and the


kernel.
The SHELL
• When you log in to interact with UNIX, you see
a “shell prompt” ($ or %).

• The shell is a program that runs constantly


and executes the commands you give it.

• You can choose which shell you prefer.


UNIX Shells
• Common Shells: Bourne shell, the C shell, and
the Korn shell.

• Users can switch between shells, using the


commands bash, csh, ksh, sh.

• Control D (^d) to return back to original shell,


or just use the command: exit.
Basic Commands
• ls list files and directories
• ls –a list all files and directories
• Mkdir make a directory
• cd dir change to named directory
• cd change to home-directory
• cd ~ change to home-directory
• cd .. change to parent directory
• Pwd display current dir path
Basic Commands
• cp file1 file2 copy file1 and call it file2
• mv file1 file2 move or rename file1 to file2
• rm file remove a file
• rmdir directory remove a directory
• cat file display a file
• more file display a file a page at a time
• who list users currently logged in
• lpr –P printer psfile print postscript file to named printer
• * match any number of characters
• ? match one character
• man read the online manual page for
a command
Basic Commands
• command > file redirect standard output to a file
• command >> file append standard output to a file
• command < file redirect standard input from a file
• Find find . -name myfile –print
• grep search a file for keywords
grep 'keyword' file % grep science science.txt

• wc file count number of lines/words/characters in file


Wc file % wc -w science.txt

• sort sort data (numerically or alphabetically)


Ex:
to sort the list of object, type % sort < biglist ,
and the sorted list will be output to the screen.
What is a Directory?
Unix Directory Structure
• /bin — stands for "binaries" and contains certain
fundamental utilities, such as ls or cp.
• /etc — contains configuration files and system
databases.
• /dev — stands for "devices". Contains file
representations of peripheral devices.
• /home — contains the home directories for the users.
• /lib — contains system libraries.
• /tmp — a place for temporary files. Many Unixes clear
this directory upon start up
Editors
• Different editors: emacs, pico, vi
• emacs <filename>
• pico <filename>
• vi <filename>
Permissions
• There are three types of file access supported
by UNIX.
– r – read, view the contents of a file or a directory
– w –write, edit file/directory contents
– x –execute, run executable file
Here’s an example
– Suppose you type in ls -l and the result is
• rwx r-x r-- 1 hans doc 858 Aug 22 22:28 hw1
• What do all these symbols mean?
Permissions
Permissions
• User – the person who created the file.
• Group – the group owns the file.
• Other – the rest of the world
• “754” is a decimal number. But you can
represent each digit with a binary number.
4 => read permission, 2 => write permission,
1=> execute permission
Permissions
Permissions
rwx r-x r-- is a symbolic way to specify file modes,
while 754 is a numeric way (remember 7  111,
5 101, 4100 ? ). How would you represent
this file mode numerically?
--x --x –wx
How would you represent this bit string
symbolically?
614
Permissions
• chmod mode file(s) – another UNIX
command! Change the access mode of one or
more files. Examples:
– chmod 751 my_file – the owner of my_file has rwx(7)
permission, the group has r-x(5) permission, others have --
x permission.
– Tell me what the following command will do?
chmod u=rwx, g=r, o=wr my_file
Remember user, group and others?
Program & Process
• Program is an executable file that resides on
the disk.

• Process is an executing instance of a program

• A Unix process is identified by a unique non-


negative integer called the process ID.

• Check process status using the “ps” command.


Foreground/background processes
• A program run using the ampersand operator
“&” creates a background process.
• E.g.:
% back &
• otherwise it creates a foreground process.
• E.g.:
% back
Foreground/background processes
• Only 1 foreground process for each session.
Multiple background processes.
• Where are background processes used?
• All system daemons, long user processes, etc.
e.g. printer-daemon process or mailer-daemon
process.
• These processes are always running in
background.
• Pine is foreground process.
Process Status
% back &
[1] 16488 the process id assigned by system
% ps
PID TTY TIME CMD
1973 pts/39 0:01 tcsh
16488 pts/39 0:00 back
How to stop a process?
• Foreground processes can generally be stopped by pressing
CONTROL C (^C).

• Background processes can be stopped using the kill command.

• Usage: kill SIGNAL <process id list>

• kill -9 <process id list> (-9 means no blocked)


Or kill <process id list>.

• If a foreground process is not stopped by ^C, you can open another


session and use the kill command.
Overview of Make Utility
• The make utility is a software engineering tool for
managing and maintaining computer programs.

• Make provides most help when the program consists of


many component files.
– As the number of files in the program increases so to does the
compile time, complexity of compilation command and the
likelihood of human error when entering command lines, i.e. typos
and missing file names.

• By creating a descriptor file containing dependency rules,


macros and suffix rules,
– you can instruct make to automatically rebuild your program
whenever one of the program's component files is modified.
– Make is smart enough to only recompile the files that were
affected by changes thus saving compile time.
What Make does ?
• Make goes through a descriptor file starting with the target it is
going to create.

• Make looks at each of the target's dependencies to see if they are


also listed as targets.

• It follows the chain of dependencies until it reaches the end of the


chain and then begins backing out executing the commands found
in each target's rule.

• Actually every file in the chain may not need to be compiled.

• Make looks at the time stamp for each file in the chain and
compiles from the point that is required to bring every file in the
chain up to date. If any file is missing it is updated if possible.
What Make does ?
• Make builds object files from the source files
and then links the object files to create the
executable file.

• If a source file is changed only its object file


needs to be compiled and then linked into the
executable instead of recompiling all the
source files.
Descriptor File
prog1 : file1.o file2.o file3.o
CC -o prog1 file1.o file2.o
file3.o
file1.o : file1.cc mydefs.h
CC -c file1.cc
file2.o : file2.cc mydefs.h
CC -c file2.cc
file3.o : file3.cc
CC -c file3.cc
clean :
rm file1.o file2.o file3.o
Simple Example
• This is an example descriptor file to build an executable file called prog1.
– It requires the source files file1.cc, file2.cc, and file3.cc.
– An include file, mydefs.h, is required by files file1.cc and file2.cc.
– If you want to compile this file from the command line using C++ the
command would be
% CC -o prog1 file1.cc file2.cc file3.cc

• This command line is rather long to be entered many times as a program is


developed and is prone to typing errors. A descriptor file could run the
same command better by using the simple command

• % make prog1
Explanation of Descriptor File
• make finds the target prog1 and sees that it depends on the object files
file1.o file2.o file3.o

• make next looks to see if any of the three object files are listed as targets.
– They are so make looks at each target to see what it depends on.
– make sees that file1.o depends on the files file1.cc and mydefs.h.

• Now make looks to see if either of these files are listed as targets
– since they aren't, it executes the commands given in file1.o's rule and
compiles file1.cc to get the object file.

• make looks at the targets file2.o and file3.o and compiles these object files in a
similar fashion.

• make now has all the object files required to make prog1 and does so by
executing the commands in its rule.
Dependency Rules
• A rule consist of three parts, one or more targets,
zero or more dependencies, and zero or more
commands in the following form:

• target1 [target2 ...] :[:]


[dependency1 ...] [; commands]
[<tab> command]

• Target : A target is usually the name of the file


that make creates, often an object file or
executable program.
Dependency Rules
• Dependencies:
– A dependency identifies a file that is used to create another file.
– For example a .cc file is used to create a .o, which is used to
create an executable file.

• Commands:
– Each command in a rule is interpreted by a shell to be executed.
– By default make uses the /bin/sh shell.
– The default can be over ridden by using the macro SHELL =
/bin/sh or equivalent to use the shell of your preference.
– This macro should be included in every descriptor file to make
sure the same shell is used each time the descriptor file is
executed.
Shell Programming
• Shell scripting skills have many applications, including:
• Ability to automate tasks, such as
– Backups
– Administration tasks
– Periodic operations on a database via cron
– Any repetitive operations on files
• Increase your general knowledge of UNIX
– Use of environment
– Use of UNIX utilities
– Use of features such as pipes and I/O redirection
Examples of Shell Programming
• Store the following in a file named simple.sh and execute it
#!/bin/sh
# Show some useful info at the start of the day
date
echo Good morning $USER
cal
last | head -6

• Shows current date, calendar, and a six of previous logins

• Notice that the commands themselves are not displayed, only the
results
Storing File Names in Variables
• A variable is a name that stores a string
• It's often convenient to store a filename in a variable
• Store the following in a file named variables.sh and execute it
#!/bin/sh
# An example with variables
filename="/etc/passwd"
echo "Check the permissions on $filename"
ls -l $filename
echo "Find out how many accounts there are on
this system"
wc -l $filename

• Now if we change the value of $filename, the change is automatically


propagated throughout the entire script
Performing Arithmetic
• Backslash required in front of '*' since it is a filename
wildcard and would be translated by the shell into a list
of file names
• You can save arithmetic result in a variable
• Store the following in a file named arith.sh and execute it
#!/bin/sh
# Perform some arithmetic
x=24
y=4
Result=`expr $x \* $y`
echo "$x times $y is $Result"
?? ?
QUIZ
• What is a shell. Name some common shells.

• What is a kernel of Unix OS

• Name some unix flavours

• What does following commands do – more,


cat, who, find, grep
Thank You
And
Feedback

You might also like