Sei sulla pagina 1di 62

UNIX For Smarties

UNIX History
Typical vendor operating systems of the time were extremely large and all written in assembly language. UNIX had a relatively small amount of code written in assembly language (this is called the kernel) and the remaining code for the operating system was written in a high level language called C In UNIX because the C language was written to be used to implement an operating system rather than a traditional "input-processing-output" application, use of these sophisticated features is quite easily done from the C language without writing any assembly language

Introduction To Solaris
Solaris is the SunTM version of UNIX

The original version of UNIX, the SunOSTM (Sun Operating System) was based on BSD UNIX version 4.2 and the AT&T Bell Labs version

Basic Terms
User
Every UNIX user has a username and password

System Administrator
A super user who has authorization higher than a regular user such as defining new users in the system, deleting users directories etc.

Operating System
There are three main components of the operating system :
Kerenel
Shell File system

Kerenel

Kernel
The Kernel manages all the devices, memory and processes Controls transmission of information between the hardware and the system programs (shell)

Shell Kernel Hardware

What Is A shell
A shell is the first program UNIX runs after logging in

A shell is the interface between the user and the Kernel


The shell interprets all the commands entered by the user There are many shells available, all of which offer different features. All the popular shells falls into two groups : The Bourne (or Korn or BASH) shell The C shell

The Bourne & Bourne Again Shell


The Bourne shell is on all UNIX systems The Bourne shell program name is : sh

One of the Bourne shell versions is the Bourne Again shell (or BASH, whose program name is : bash.
The Bourne and BASH shells are similar. The advantage of BASH is that it has command editor , used to correct commands as typed with the arrow keys bash

The Korn Shell


The Korn shell based on the Bourne shell The Korn shell program name is : ksh

Most of the new features added are in use by people who write shell scripts
The Korn shell also have command editor

The C & Extended Shell


The C shell has many useful characters such as : An exclamation point (!) Tells the C shell to run a command again

The carets (^) Tells the C shell to repeat a command with some changes
The C shell program name is : csh Extended C shell is slightly extended, and has a command editing. The extended C shell program name is : tcsh

10

How To Get Shell Information


Information about which shell you are using can be found in the SHELL environment variable. The command : echo $SHELL displays the value of this variable You can identify which shell you are currently using from the last part of the pathname Pathname Shell /.../sh /.../csh Bourne shell C shell

/.../tcsh
/.../ksh /.../bash

TC shell
Korn shell Bourne Again Shell

11

Unix Commands Location


The shell checks every command entered. If the command isnt one the shell can execute by itself, the shell looks for a program stored in a file by the same name Many of the commands that we use are located in the directory : /usr/bin A typical value for the PATH variable might be : /bin:/usr/bin:/usr/local/utils/bin:$HOME/bin This is a path list of colon separated pathnames to the directories that contain the executable binary files for each command The shell will first look for the command that has been entered in the directory /bin. If it is not found there it will then look in the directory /usr/bin and so on until the command is found If the command is not found on your path this message is displayed command_name: command not found.
12

How To Log In
Every UNIX user has a login name and a password When entering the UNIX workstation the login prompt is automatically displayed :

SunOS 5.6

login:

talmor

Password: Last login: Mon Dec 11 08:19:34 from 10.202.0.29 Sun Microsystems Inc. > SunOS 5.6 Generic August 1997

13

How To Log In To Another Machine


The command to log in to another machine on the network is : rlogin machine

Another command to log in and also to execute shell command is : rsh machine command
Use this if the machine you are on seems to be running slowly and the who command indicates that there are lots of others on the same machine

14

How To Define a New User


Only the system administrator can define a new user The command to define a new user is : useradd

15

How To Change Password


The command to change the login name and the password is : passwd user_ name passwd talmor

>

Enter login(NIS) password: New password:

16

Listing Logged In Users


The command to list everyone logged into the same machine that the current user is using is : who who talmor pts/1 passwd user name
>

Dec 19 09:08

(10.202.0.29)

The command to list the username of the invoking user is : who am i


>

who am i
pts/1 Dec 19 09:08 (10.202.0.29)

talmor

17

How To Log Out


When the session is finished, logging out is required The commands to log out are :

logout
or exit or ctrl+d (end of input signal)

18

Displaying Current Host Architecture


The command to display the architerture of the current host is : arch arch sun passwd user name
>

The command to display the UNIX version is : uname r The command to display the UNIX version and more information is : uname a

>

uname -a

SunOS gilat04 5.6 Generic_105181-05 sun4u sparc SUNW,UltraSPARC-IIi-Engine

19

How To Get The Time & Date


The command to print the current time and date is : date
>

date

Sun Dec 31 14:49:08 IST 2000

To display the date and time at a different format : date date_&_ time

>

Date +%d/%m/%y

31/12/00

20

Unix Special Characters


ctrl+c - Is used to abort a program/command that is running too long or working incorrectly Beware : aborting (Ctrl+c) a program that is

updating a file may leave garbage in that file


ctrl+d ctrl+h ctrl+b - is used when a command/program is reading many lines of input

from the keyboard and you want to signal the end of the input
- the "Backspace" key, and the "Delete" key all delete the most

recently typed character


- moves the cursor Backwards, without deleting those characters

21

Unix Special Characters (Cont.)


ctrl+f - moves the cursor Forwards over what you have just typed, without

deleting those characters. The "right" arrow on your keyboard may also do the same thing
ctrl+p ctrl+n - retrieves the Previous command that you had typed in. The "up"

arrow on your keyboard may also do the same thing


- is the opposite of ctrl+p. After one or more ctrl+p's, a ctrl+n allows

you to move back to the Next more recent command. The "down" arrow on your keyboard may also do the same thing
ctrl+u ctrl+z - Delete the entire line - Suspend the current command

22

Multiple Commands
; (semicolon) is used to invoke multiple commands on the same line

> who;time
talmor pts/1 Jan 0.27u 0.68s 2:33:48.24 0.0% 1 07:40 (10.202.0.29)

23

Manual Pages Online Help


UNIX has an online help called manual pages The command to display online help about a command is : man command All manual pages have the same format Title Name - Includes the name and page number of the manual - The name of the manual page. Usually the name is the command or commands the page describes and a brief description - What the used typed and all the options available - A few paragraphs about the command, the options and operands that can be use, examples and etc.

Synopsis Description

24

Manual Pages Example


> man ls Reformatting page. User Commands NAME ls - list contents of directory SYNOPSIS /usr/bin/ls [ -aAbcCdfFgilLmnopqrRstux1 ] [ file... ] /usr/xpg4/bin/ls [ -aAbcCdfFgilLmnopqrRstux1 ] [ file... Wait... done ls(1)

DESCRIPTION For each file that is a directory, ls lists the contents of the directory; for each file that is an ordinary file, ls repeats its name and any other information requested. The output is sorted alphabetically by default. When no argument is given, the current directory is listed. When several arguments are given, the arguments are first sorted appropriately, but file arguments appear before directories and their contents. There are three major listing formats. The default format for output directed to a terminal is multi-column with entries sorted down the columns. The -1 option allows sin25

The UNIX File System The File


The file system in UNIX is composed of files and directories and has a hierarchical storage structure

At the root of this hierarchical structure is a directory known simply as /


The file system supports the following file manipulations : Listing information about files

Showing permissions to use files


Duplicating a file Erasing a file

Renaming a file
Listing the file content Printing a text file
26

Special Files In The UNIX


File and directories names started with . (dot) are often treated specially In the Bourne, Korn and BASH shells there is a special file : .profile - Every logging in the shell looks for this file in the user home directory. In this file the user can edit all the commands he wants the shell to execute right after logging in In the C shell there are two special files : .login .cshrc .tcshrc - Runs every logging in - Runs every time that the user starts a new C shell - Runs every time that the user starts a new TC shell

27

Listing Files
One of the most common command is the list command : ls options
Option -a -d -F -g -i -l -R -t Action list hidden files list the name of the current directory show directories with a trailing '/' show group ownership of file in long listing print the inode number of each file long listing giving details about files and directories list all subdirectories encountered sort by time modified instead of name

28

List Command Examples


The command to list files in the working directory is :
> ls CAD NT TT_DB bin cdrom dev

ls

devices etc export floppy home kernel

lib lost+found mnt n neclic.log net

nsmail nt opt patch platform proc

sbin tmp usr var visula visula_data

vol xfn

The command to list more information (full) about files is :


> ls -l total 24 drwxrwxr-x drwxrwxr-x drwxr-xr-x

ls -l

8 cadadmin other 10 root other 8 root root

4096 Nov 1 15:03 apps 4096 Nov 15 09:53 dyn 4096 Dec 4 10:32 store

29

List Command Examples (Cont.)


The command to list all files including hidden files in the directory is : ls -a
> ls -a . .. .ha .snapshot Engineering1 Program_Management1 QA1 Tapi1 Tech_Support1 Technical Training&Docs1 gvt hr legal logistics marketing

The command to see all the information about all the files in the directory is : ls -al
> ls -al total 120 drwxrwxr-x drwxrwxrwx drwxrwxrwx drwxrwxr-x drwxrwxr-x drwxr-xr-x

6 46 21 8 10 8

cadadmin root root cadadmin root root

cadnet root root other other root

4096 8192 36864 4096 4096 4096

Nov 5 13:01 . Nov 15 17:36 .. Dec 10 09:08 .snapshot Nov 1 15:03 apps Nov 15 09:53 dyn Dec 4 10:32 store

30

List Command Examples (Cont.)


To list files in another directory which is at the same level in the directory hierarchy as the current directory : ls ./directory_name
> ls ./doc bills

31

Defining Files With Wildcard Characters


Wildcard characters can be used to represent many other characters. Use them whenever you need to define a string of characters, such as a filename, for use with a command
Useful wildcards are :

* matches any characters


? match any single character [...] matches any character in the enclosed list or range

32

Wildcard Characters Examples


List all files in a directory ending with known characters using wildcard : ls *al
> ls *al fal sal

List all files in a directory with some unknown characters using wildcard : ls ??l
> ls ?a? fal sal

33

Copy Command
The command to copy files is : cp
The copy command is used to make another copy of a file : cp file1 file2 where file1 is the original file and file2 is the name of the target file. In this example both of files will be located in the same directory

34

Copy Command (Cont.)


A file can be copied to another directory with the command : cp file1 /directory If more than one file is specified then the target should be a directory The command to copy a full directory with its all subdirectories is done with the parameter -r : cp r ./directory path

35

Copy Command Examples


A copy command with a checking if the target file name (file2) already exists is : cp i file1 file2

> cp i file1 file2 cp: overwrite file2 (yes/no)?

36

Searching For A File


The command for finding particular groups of files is : find path_name name file_name -print

> find . name ./doc/mor ./mos

bills -print

The dot (.) refers to the current directory The -print option is the default and if not entered the system will add it automatically

37

Searching For A File Example


To find a file below the user home directory : find ~/ name file_name -print

>

find ~/ name bills -print

/n/gna2/vol/vol0/cad/dyn/USERS/talmor/bills /n/gna2/vol/vol0/cad/dyn/USERS/talmor/doc/mor/bills

38

Remote Copy Command


The command to copy files between a local host and other remote hosts is : rcp source_file destination_file Prerequisites : Before using rcp you must : 1. 2. have an account on the UNIX host you want to copy files to and from have a special file called .rhosts in your login directory on the other UNIX host

3. The .rhosts file is a text file that you can create using any text editor
4. Each line of the file has the name of a remote host Followed by a space and your username on that host

39

Remote Copy Command (Cont.)


To copy files between a remote located at another machine to the local host : rcp machine_name:file_name destination
> rcp g06:/etc/passwd > ls Permission fal c:\test\tty file_list doc billss.txt

.
linecalc.inf lineline.inf mom mor.txt billings passwd: tar.tar temp.tar tty

40

Listing File Contents


The command to list the contents of a file is : cat file_name
> cat ls ls -l cd ..

file

The command to list the contents of a file one page at a time is : more file_name

41

Listing File Contents )Cont.)


The command to list the contents of number of files one screen at a time, pausing after each screen full is : more file_name1 .. file_nameN
> more bills billings :::::::::::::: bills :::::::::::::: Expenses Ept 1000 Deh 1500 :::::::::::::: billings :::::::::::::: Her 2000 Fer 3000 Dfe 900

After each screen is displayed, the Return key should be pressed


42

Searching A String In A File


The command to search a string in one or more files is : grep string file_name1 .. file_nameN
> grep 200 bills billings
billings:200

grep

43

Searching A String In A File Examples


To search a string in one or more files ignoring uppercase and lowercase distinctions : grep i string file_name1 .. file_nameN
> grep i cd bills billings
bills:cD . billings:cd ..

> cat bills


ls ls l

> cat billings


100 cd .

cD .

44

Searching A String In A File Examples


To search a string using regular expression with a line number for each line : grep -n string file_name1..file_nameN
> grep n cd bills
12:cd ..

To search a string expressed with more then one option with a line number for each line : grep -n [string] file_name1..file_nameN
> grep n [cC]d bills billings
bills:12:cd .. billings:3:Cd .

45

Searching A String In A File Examples


To print all lines that dont match with a specific string : grep -v string file_name1..file_nameN
> grep v talmor paths.txt names.txt
names.txt:ls

> cat paths.txt


talmor talmor pts/1 pts/1 Jan Jan 2 11:37 2 11:37 (10.202.0.29) (10.202.0.29)

> cat names.txt


talmor talmor pts/1 pts/1 Jan Jan 2 11:37 2 11:37 (10.202.0.29) (10.202.0.29

46

Removing Duplicated Data From A File


The command to remove duplicated adjacent lines from sorted file, sending one copy of each line to another file (or to another output) is : uniq file1 file2

> uniq bills billings

The original file:


> cat bills
1

The result file :


> cat billings
1

90
700 50 50

90
700 50

47

Removing Duplicated Data From A File Example


The command to remove duplicated adjacent lines from sorted file, sending one copy of duplicated lines to another file (or to another output) is : uniq d file1 file2

> uniq d bills billings

The original file:


> cat bills
1 90 700 50 50

The result file :


> cat billings
50

48

Sorting A File Content


The command to sort a file line by line in an alphabetic order is : sort file_name The command to sort numbers is : sort n file_name
> sort n bills
1 bnd 50 ytr 60 ers 90 fgd 100 dse 350 tyr 456 klh 700 isr

49

Sorting A File Content Example


To sort a file content without showing duplicate lines : sort u file_name
> sort u bills
10 100 20 30

The original file:


> cat bills
10 100 20 30 20

50

Deleting A File
The command to delete a file or a list of files is : rm file_name1 ..file_nameN
The command to delete a file with inquire before deleting is : rm -i file_name1 file_name2
> rm i bills billings

rm: remove bills (yes/no)? n rm: remove billings (yes/no)? y

51

Deleting A File (Cont.)


The command to delete all files in the current working directory is : rm *
The command to delete a listed files and directories (including everything in it) is : rm -r file_name1 ..file_nameN

52

Renaming A File
The command to rename a file is : mv file1 file2

> mv bills billings

53

Renaming A File Example


To rename a file with inquire before overwriting :
> mv i bills billings
mv: overwrite billings (yes/no)?

mv i file1 file2

54

Printing A File
The command to send file to printer for printing is : lpr file_name For sending other printers, the printer name should be as part of the command : lpr -Pprinter file_name

The command to list all the printers is : lpstat -t


> lpstat -t
scheduler is running system default destination: pinkfl3_1 system for lpcad01: gilat02 system for pinkfl3_1: gilat02 system for pinkfl2_1: gilat02 lpcad01 accepting requests since Jan 03 08:51 2001 pinkfl3_1 accepting requests since Jan 03 08:51 2001 pinkfl2_1 accepting requests since Jan 03 08:51 2001 printer lpcad01 is idle. enabled since Jan 03 08:51 2001. available. printer pinkfl3_1 is idle. enabled since Jan 03 08:51 2001. available. printer pinkfl2_1 is idle. enabled since Jan 03 08:52 2001. available.

55

Counting Words In A File


The command to print a character, word and line count for files is : wc The command to print line count only is : wc l file_name The command to print word count only is : wc w file_name The command to count the number of users logged in is : who | wc l

56

Counting In A File Example


The command to count the number of lines, words and characters is : wc lwc file_name

> wc lwc bills


4

16 bills

Counter of lines

Counter of words

Counter of characters

57

Comparing Files
The simplest command comparison between two files, which tells whether two files are the same or different is : cmp file1 file2

> cmp bills billings


billings bills differ: char 3, line 1

If the content of the two files are the same, cmp command doesnt say anything If there are different, cmp command tells how far into the files it got before it found a something different

58

Comparing Files (Cont.)


The command to display the line by line difference between two files is : diff file1 file2 The information given by the command tells you what changes need to be made for file1 and file2 to match. If there is no difference between the files you are returned to the shell prompt

The command diff indicates which lines need be added (a), deleted (d) or changed (c) Lines in file1 are identified with a (<) symbol: lines in file2 with a (>) symbol
The command to compare two files ignoring differences in the case of the letters and blank spaces is : diff -iw file1 file2

59

Comparing Files Example ^^^^^^^^^^^^


An example for diff command is : diff bills billings

> diff billings bills


1c1 < 10 --> 100 3c3 < 500 --> 300 5,6d4 < 500 < 600

> cat billings


10 200 500 400 500 600

> cat bills


100 200 300 400

60

Determining File Type


The command examines the content of a file and reports what type of file it is : file file_name Use this command to check the identity of a file, or to find out if executable files contain shell scripts, or are binaries. Shell scripts are text files and can be displayed and edited

61

Determining File Type Example


An example of file command is : file file_name

> file bills mos


bills: mos:

ascii text directory

62

Potrebbero piacerti anche