unix tutorial csu480. outline getting started system resources shells special unix features ...

82
Unix Tutorial CSU480 CSU480

Post on 19-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Unix Tutorial

CSU480CSU480

Outline

Getting StartedGetting Started System ResourcesSystem Resources ShellsShells Special Unix FeaturesSpecial Unix Features Text ProcessingText Processing Other Useful CommandsOther Useful Commands

The Unix Operating System

KernelKernel System callsSystem calls Multi-user, multi-tasking OSMulti-user, multi-tasking OS

File System

FS is the focus point of UnixFS is the focus point of Unix Looks like an inverted treeLooks like an inverted tree Root directory /Root directory / Each node is either a file or a directoryEach node is either a file or a directory Path namePath name

Absolute path Absolute path Relative pathRelative path. The current directory. The current directory.. The parent of current directory.. The parent of current directory

Directory, File and I-node

Every directory and file is listed in its parent Every directory and file is listed in its parent directorydirectory

The root’s parent is itselfThe root’s parent is itself All Unix files are non-structured (bit stream)All Unix files are non-structured (bit stream) A directory is a special file:A directory is a special file:

File nameFile name I-node numberI-node number

Unix Programs

Shell: between user and the kernelShell: between user and the kernel Commands:Commands:

Built-in shell command (pwd)Built-in shell command (pwd) Source compiled, object code file (ls)Source compiled, object code file (ls) Shell script (**.sh)Shell script (**.sh)

$PATH=/bin:/usr/bin:/usr/local/bin, etc$PATH=/bin:/usr/bin:/usr/local/bin, etc

Getting started

Logging inLogging in Linux Command Line StructureLinux Command Line Structure Control KeysControl Keys stty – terminal controlstty – terminal control Getting HelpGetting Help Directory Navigation and ControlDirectory Navigation and Control File Maintenance CommandsFile Maintenance Commands Display CommandsDisplay Commands

Logging in

Username & passwordUsername & password Unux is case sensitiveUnux is case sensitive Changing passwordChanging password

passwdpasswd Choosing an hard-to-crack passwordChoosing an hard-to-crack password Exiting systemExiting system

Ctrl-DCtrl-D

Identity

username<->useridusername<->userid groupname<->groupidgroupname<->groupid A user can belong to more than one groupA user can belong to more than one group

Primary group: /etc/passwdPrimary group: /etc/passwd Additional groups: /etc/groupAdditional groups: /etc/group

Identity

% % idid

uid=1101(frank) gid=10(staff)uid=1101(frank) gid=10(staff)

% % idid

uid=1101(frank) gid=10(staff) uid=1101(frank) gid=10(staff) groups=10(staff),5(operator),14(sysadmin),110(uts)groups=10(staff),5(operator),14(sysadmin),110(uts)

% % groupsgroups

staff sysadmin uts operatorstaff sysadmin uts operator

Unix Command Line Structure

Format: command [Format: command [optionsoptions] [] [argumentsarguments]] Arguments: action object, usu. filesArguments: action object, usu. files Options: change the way it performsOptions: change the way it performs command command -[option][option][option]-[option][option][option]

ls ls -alR-alR command command -option1 -option2 -option3-option1 -option2 -option3

ls ls -a -l -R-a -l -R

Control Keys

Used to perform special functionsUsed to perform special functions Ctrl-Key (^-Key)Ctrl-Key (^-Key)

^-U: “line-kill” signal^-U: “line-kill” signal

^-A: go to the beginning of the line^-A: go to the beginning of the line

^-E: go to the end of the line^-E: go to the end of the line

^-^-K: kill until the end of the lineK: kill until the end of the line

Getting Help

man man commandcommand % man -k password% man -k password

passwd (5) - password filepasswd (5) - password file passwd (1) - change password passwd (1) - change password

informationinformation % man 5 passwd% man 5 passwd

Getting Help

Directory Navigation and Control

cd

mkdir & rmdir

mkdir mkdir

-p -p create the intermediate (parent) create the intermediate (parent) directories, as neededdirectories, as needed

-m -m mode access permissions (SVR4). mode access permissions (SVR4). rmdir: directory must be emptyrmdir: directory must be empty

ls

ls

File Maintenance Commands

File Maintenance Commands

cp –r: recursively copy a directorycp –r: recursively copy a directory mv: Actually all you’ve done is to update the directory mv: Actually all you’ve done is to update the directory

table entry to give the file a new name. The contents of the table entry to give the file a new name. The contents of the file remain where they were.file remain where they were.

rm: Actually, all you’ve done is to remove the directory rm: Actually, all you’ve done is to remove the directory table entry and mark the inode as unused. The file contents table entry and mark the inode as unused. The file contents are still on the disk, but the system now has no way of are still on the disk, but the system now has no way of identifying those data blocks with a file name.identifying those data blocks with a file name.

-r: recursively-r: recursively

-f: force-f: force

chmod

u – useru – user

g – groupg – group

o – othero – other

read=4, write=2, execute=1

or

read=r, write=w, execute=x

+ add permissions

- remove permissions

= set permissions

chmod

chmod 755 file1 chmod 755 file1 or or chmod u=rwx,go=rx file1chmod u=rwx,go=rx file1 As for directory:As for directory:

r: can read the content of the directoryr: can read the content of the directory

w: can create/delete files of the directoryw: can create/delete files of the directory

x: can “go through” the directoryx: can “go through” the directory

chown

chown new_user:new_group filechown new_user:new_group file Note: On most versions of Unix this can only be Note: On most versions of Unix this can only be

done by the super-user, i.e. a normal user can’t done by the super-user, i.e. a normal user can’t

give away ownership of their filesgive away ownership of their files..

Display Commands

echo

echo

more, less, & pg

more, less, & pg

System Resources

df

df df is used to report the number of disk is used to report the number of disk blocks and inodes used and free for each blocks and inodes used and free for each file system.file system.

Options:Options:

--l l local file systems only local file systems only (SVR4)(SVR4)

-k -k report in kilobytes (SVR4)report in kilobytes (SVR4)

du

du du reports the amount of disk space in use reports the amount of disk space in use for the files or directories you specify.for the files or directories you specify.

Options:Options:

-a -a display disk usage for each file, not display disk usage for each file, not just subdirectoriesjust subdirectories

-s -s display a summary total onlydisplay a summary total only

-k-k report in kilobytes (SVR4)report in kilobytes (SVR4)

ps

ps ps is used to report on processes currently is used to report on processes currently running on the system.running on the system.

Options:Options:

-e Select all processes. -e Select all processes.

-f does full-format listing.-f does full-format listing.

kill

who

who who reports who is logged in at the present reports who is logged in at the present time.time.

SyntaxSyntax

who who [am i][am i]

whereis

which

which which will report the name of the file that is will report the name of the file that is be executed when the command is invoked. be executed when the command is invoked. This will be the full path name or the alias This will be the full path name or the alias that’s found first in your path.that’s found first in your path.

SyntaxSyntax

which which command(s)command(s)

hostname/uname

hostname hostname ((uname -n uname -n on SysV) reports the on SysV) reports the host name of the machine the user is logged host name of the machine the user is logged intointo

uname uname has additional options to print has additional options to print information about system hardware type information about system hardware type and software version.and software version.

script

script script creates a script of your session input and output. creates a script of your session input and output. Using the Using the script script command, you can capture all the data command, you can capture all the data transmission from and to your terminal screen until you transmission from and to your terminal screen until you exit exit the script program. This can be useful during the the script program. This can be useful during the programming-and-debugging process, to document the programming-and-debugging process, to document the combination of things you have tried, or to get a printed combination of things you have tried, or to get a printed copy of it all for later perusal.copy of it all for later perusal.

SyntaxSyntax

script script [-a] [file] <. . .> exit[-a] [file] <. . .> exit

date

date date displays the current data and time. A displays the current data and time. A superuser can set the date and time.superuser can set the date and time.

SyntaxSyntax

date date [options] [+format][options] [+format]

Shells The shell sits between you and the operating system, acting as a command The shell sits between you and the operating system, acting as a command

interpreter.interpreter. cat /etc/shellscat /etc/shells

/bin/sh/bin/sh/bin/bash/bin/bash/sbin/nologin/sbin/nologin/bin/ash/bin/ash/bin/bsh/bin/bsh/bin/ksh/bin/ksh/usr/bin/ksh/usr/bin/ksh/usr/bin/pdksh/usr/bin/pdksh/bin/tcsh/bin/tcsh/bin/csh/bin/csh/bin/zsh/bin/zsh

Shells

sh: Bourne shell (not suited for interactive)sh: Bourne shell (not suited for interactive) csh: uses C type syntax (job control)csh: uses C type syntax (job control) ksh: Korn shellksh: Korn shell bash: Bourne Again Shell (GNU)bash: Bourne Again Shell (GNU) tcsh: T-C shelltcsh: T-C shell cshe: extended C shellcshe: extended C shell

Built-in Commands

These commands are executed directly in These commands are executed directly in the shell and don’t have to call another the shell and don’t have to call another program to be run.program to be run.

Environment Variables

Environmental variables are used to provide Environmental variables are used to provide information to the programs you use.information to the programs you use.

Global environment variablesGlobal environment variables are set by your are set by your login shell and new programs and shells inherit login shell and new programs and shells inherit the environment of their parent shell.the environment of their parent shell.

Local shell variablesLocal shell variables are used only by that shell are used only by that shell and are not passed on to other processes.and are not passed on to other processes.

Environment Variables

Shell profile

Global:Global:

/etc/profile/etc/profile User:User:

/home/duanjj/.bash_profile/home/duanjj/.bash_profile

profile

#: #: commentscomments

$PATH: separated by a colon : and dot . represents current $PATH: separated by a colon : and dot . represents current directorydirectory

A variable set in A variable set in .profile .profile is set only in the login shell unless is set only in the login shell unless you "you "exportexport" it or " it or source .profile source .profile from another shell.from another shell.

umask 0022umask 0022

Job control

&: &: put jobs into the backgroundput jobs into the background

Ctrl-Z: suspend a jobCtrl-Z: suspend a job

bg: put jobs into the backgroundbg: put jobs into the background

fg: bring jobs back to the foregroundfg: bring jobs back to the foreground

jobs: list your background jobsjobs: list your background jobs

Job control

Background jobs can’t read from keyboard, but they can Background jobs can’t read from keyboard, but they can write onto the terminal, interspersing with whatever else is write onto the terminal, interspersing with whatever else is typed or displayed by your current job. typed or displayed by your current job.

Solution: You may want to redirect I/O to or from files for Solution: You may want to redirect I/O to or from files for the job you intend to background.the job you intend to background.

Your keyboard is connected only to the current, foreground, Your keyboard is connected only to the current, foreground, job.job.

Changing your shell

chshchsh /etc/shells/etc/shells

Working With Files

cmp

reports the location of the first differencereports the location of the first difference can deal with both binary and ASCII file can deal with both binary and ASCII file

comparisons.comparisons. does a byte-by-byte comparisondoes a byte-by-byte comparison

diff

compares two files, directories, etc, and compares two files, directories, etc, and reports all differences between the two.reports all differences between the two.

deals only with ASCII files.deals only with ASCII files. It’s output format is designed to report the It’s output format is designed to report the

changes necessary to convert the first file changes necessary to convert the first file into the second.into the second.

cut

allows a portion of a file to be extracted for another use.allows a portion of a file to be extracted for another use.

cut

File: usersFile: users cut -f 1,2 userscut -f 1,2 users cut -c 1-4 userscut -c 1-4 users

paste

allows two files to be combined side-by-side.allows two files to be combined side-by-side. The default delimiter between the columns in a paste is a The default delimiter between the columns in a paste is a

tabtab A hyphen (A hyphen (--) in place of a file name is used to indicate that ) in place of a file name is used to indicate that

field should come from standard input.field should come from standard input.

paste

users + phoneusers + phone method 1: method 1:

cut –f 2 phone > temp.filecut –f 2 phone > temp.file paste users temp.file > listingpaste users temp.file > listing

method 2:method 2: cut -f2 phone | paste users - > listingcut -f2 phone | paste users - > listing

touch

used to create a new (empty) file used to create a new (empty) file or to update the last access date/time on an or to update the last access date/time on an

existing fileexisting file

wc – word count

Options:Options: --c c count bytescount bytes -l -l count linescount lines -w -w count wordscount words

% wc users% wc users

ln – link to another file

SyntaxSyntax

ln ln [options] source [target][options] source [target]

OptionsOptions

--s s make a symbolic linkmake a symbolic link hard linkhard link symbolic linksymbolic link

Hard link

Only on the same “file system”Only on the same “file system” Can’ link to a directoryCan’ link to a directory creates a new directory entry pointing to the creates a new directory entry pointing to the

same inodesame inode as the original file. as the original file. The file linked to must exist before the hard The file linked to must exist before the hard

link can be created.link can be created. The file will not be deleted until all the hard The file will not be deleted until all the hard

links to it are removedlinks to it are removed

Symbolic link

Used to create a new path to another file or Used to create a new path to another file or directorydirectory..

Can traverse file systemsCan traverse file systems Permissions are open to allPermissions are open to all

sort - used to order the lines of a file.

example

File : usersFile : users % sort users% sort users % sort +2 users:% sort +2 users: % sort -r users:% sort -r users: who | cut -d " " -f 1 | sort -u | wc -lwho | cut -d " " -f 1 | sort -u | wc -l

tee – copy command output

sends standard in to specified files and also sends standard in to specified files and also to standard out.to standard out.

often used in command pipelines.often used in command pipelines.

% who | tee users.file% who | tee users.file % who | tee users| wc -l% who | tee users| wc -l

uniq

filters duplicate adjacent lines from a file.filters duplicate adjacent lines from a file.

find

recursively search the indicated directory tree to find files matching a recursively search the indicated directory tree to find files matching a type or pattern you specifytype or pattern you specify

then list the files or execute arbitrary commands based on the results.then list the files or execute arbitrary commands based on the results.

SyntaxSyntax

find find directory [search options] [actions]directory [search options] [actions]

Common OptionsCommon Options

For the time search options the notation in days, For the time search options the notation in days, n n is:is:

+n +n more than more than n n daysdays

n n exactly exactly n n daysdays

-n -n less than less than n n daysdays

find

% % find . -newer library -printfind . -newer library -print % % find /usr/include “stdio.h” -printfind /usr/include “stdio.h” -print

File compression

gzip, gunzipgzip, gunzip bzip2, bunzip2bzip2, bunzip2

tar – archive files

combines files into one device or filename for combines files into one device or filename for archiving purposes.archiving purposes.

does not compress the files;does not compress the files; merely makes a large quantity of files more merely makes a large quantity of files more

manageable.manageable. accept its options either with or without a accept its options either with or without a

preceding hyphen (preceding hyphen (--).).

tar

Options:Options:

c c create an archive (begin writting at the start of the create an archive (begin writting at the start of the file)file)

t t table of contents listtable of contents list

x x extract from an archiveextract from an archive

vv verboseverbose

f f archive file namearchive file name

b b archive block sizearchive block size

od

dumps a file to stdout in different formats, including octal, decimal, dumps a file to stdout in different formats, including octal, decimal, floating point, hex, and character format.floating point, hex, and character format.

--b b octal dumpoctal dump

-d|-D -d|-D decimal (-d) or long decimal (-D) dumpdecimal (-d) or long decimal (-D) dump

-s|-S-s|-S signed decimal (-s) and signed long decimal (-S) dumpsigned decimal (-s) and signed long decimal (-S) dump

-f|-F -f|-F floating point (-f) or long (double) floating point (-F) dumpfloating point (-f) or long (double) floating point (-F) dump

-x|-X-x|-X hex (-x) or long hex (-X) dumphex (-x) or long hex (-X) dump

-c|-C -c|-C character (single byte) or long character dump (single or character (single byte) or long character dump (single or multi-multi- byte characters, as determined by locale settings) dumpbyte characters, as determined by locale settings) dump

-v -v verbose modeverbose mode

Thanks