cs4315a. berrached::cms::uhd1 quick unix tutorial

57
CS4315 A. Berrached::CMS::UHD 1 Quick UNIX Tutorial

Upload: egbert-murphy

Post on 13-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 1

Quick UNIX Tutorial

Page 2: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 2

Outline

• Getting help while on the system

• The shell

• Working with files & directories

• Wild card characters

• Security

• I/O redirection

• pipes

• process and job control commands

Page 3: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 4

Logging in the first time

• Change your password

passwd

• To logout

logout or exit

Note: all Unix commands are case sensitive

Page 4: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 5

Getting Help from the System

• All Unix commands are described online in a collection of files called “man pages”

man command

• For help on some topic

man -k keyword

• For more information on using the man pages

man man

Page 5: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 8

General command format

Command -options arguments

• options/flags generally identify some optional capabilities

• some parts of a command are optional. These are indicated in the man pages with [ ]

• case sensitive

Page 6: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 9

The shell• The Unix process that interprets your commands is called

the “shell”• When you login, the login process, after it verifies the

user’s username and password, creates a shell process.• The shell process displays a prompt on the screen and

waits. • When the user enters a command, the shell examines it,

interprets it and either executes it or calls another program to do so.

• After the command is executed, the shell displays the command prompt and waits again.

Page 7: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 10

The shell

• There are several Unix shells• The Bourne shell(sh) and the C shell(csh) are the

most popular. The TC shell (tcsh) is variation of the C shell. Bourne Again Shell (bash) is the default on gator

• To display the shell you’re using

echo$SHELL

--> /bin/tcsh• To change to another shell

chsh

Page 8: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 11

Files and Directories

• Home directory:– The actual path of your home directory may be something like: /home/student/username

– Note the forward slashes

Page 9: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 12

Listing contents of a directory

Ls (list files and directories) ls

– The ls command lists the contents of your current working directory.

• > ls

Mail courses jets.com News cs4315 g.cc junk proj3 vhdl adsrc ddm ga mail public_html bin exam2.cc misc resch

Page 10: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 13

Listing contents of a directory

To generate a detailed listing

ls -l• to display type of file

ls -F• May combine flags

ls -lF To generate listing of a specific directory

ls -lF pathname

where pathname is the path of intended directory.

Page 11: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 14

Aliases

alias dir = 'ls -lF'

Page 12: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 15

Configuration Files

• ls lists all files except those starting with a dot: "."

• Generally, files that start with a dot are supposed to be program configuration files

• to list all files and directories in current directory, including hidden files

ls -a

Page 13: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 16

.files

• In your home directory there are two hidden files “.login” and ".cshrc".

.login: login configuration file

.bash_profile: the bash initialization file• In every directory there are “.” and “..”

“.”: points to the current working directory

“..”: points to the parent directory of the current working directory.

Page 14: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 17

Wildcards• The characters * will match against one or more

characters in a file or directory name.

ls proj*

• The character ? Will match against any single character

• [ ]: the brackets enclose a set of characters any one of which may match a single character in that position.

e.g cat proj[125]

cat proj[1-7]

Page 15: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 18

Wildcards

• ~: a tilde at the beginning of a word expands to the name of your home directory.

e.g: ls ~

cat ~/proj1.cc• if you append ~ to a user name, it refers to that

user’s home directory.

e.g: ls ~smith

lists all files in home directory of user smith

Page 16: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 19

Making Directories

mkdir (make directory)

mkdir name• creates a subdirectory in current working directory

mkdir somepath/name

• creates a subdirectory in directory somepath

Page 17: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 20

Changing to a different directory

cd (change directory)

cd pathname

• change current working directory to pathname.

• cd by itself will make your home directory the current working directory

• cd ..: cd to parent of current directory• cd ~ : cd to home directory

Page 18: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 21

Pathnames

• pwd (print working directory)

> pwd

/home/student/smith

Page 19: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 22

Copying filescp (copy)

cp file1 file2

• makes a copy of file1 and calls it file2. File1 and file2 are both in current working directory.

cp pathname1/file1 pathname2

• copies file1 to pathname2

e.g cp ~/tutorial/science.txt .

Page 20: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 23

Moving files

mv (move)

mv file1 file2

• moves (or renames) file1 to file2

• use the -i option to prevent an existing file from being destroyed

mv -i file1 file2

• if file2 already exist, mv will ask if you really want to overwrite it.

Page 21: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 24

Removing files and directories

rm (remove)

rm file1 [file2]…

• Use the -i option for interactive remove:

rm -i proj*.*

Page 22: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 25

Removing files and directories

rmdir (remove directory)

rmdir path

• will not remove your current working directory

• will not remove a directory that is not empty

• To remove a directory and any files and subdirectories it contains use -r (recursively)

rmdir -r path

rmdir -ir path

Page 23: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 26

Displaying the contents of a file on the screen

cat (concatenate)

cat myfile

displays the contents of myfile on monitor

cat file1 file2 file3

more

displays a file on the screen one page at a time. Use space bar to display to next page.

Head -- displays first 10 lines

tail -- displays last 10 lines

Page 24: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 27

Searching the contents of a file

• Searching using more

For example, to search myfile for the word science, type more myfile

then type / science

Type n to search for the next occurrence of the word

Page 25: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 28

Searching the contents of a file

• Searching using grep> grep music myfile • To ignore upper/lower case distinctions, use the -i

option> grep -i music myfile • To search for a phrase or pattern, you must

enclose it in single quotes. For example to search for the phrase operating systems, type

> grep -i 'operating systems' myfile

> grep -i 'operating systems' *

Page 26: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 29

Searching the contents of a file

Some of the other options of grep are: -v display those lines that do NOT match

-n precede each matching line with the line number

-c print only the total count of matched lines

Page 27: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 30

Other Useful Commands

wc (word count)

• To do a word count on myfile, type wc -w myfile

• To find out how many lines the file has, type wc -l myfile

• To do bothwc myfile

Page 28: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 31

Other Useful Commands

• who

lists on the screen all the users currently logged to the system

• finger username

lists information about a user• sort

takes it is input from the standard input (keyboard) and sorts the lines in alphabetical order.

Page 29: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 32

Redirecting Input and Output• In general, Unix commands use the standard input

(keyboard) and output (screen).• < : redirect input• > and >> : redirect outputExample:

who > namelistwho >> namelistsort < namelistsort < namelist > newnamelistsort < namelist > namelist

Page 30: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 33

Redirecting Input and Output

• Another example: search for the word mysort in all the c source files in the current directory and write the output to file1.

• grep mysort *.c > file1

Page 31: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 34

Using redirection to concatenate files

• Examples:

cat file1 > file2

copies file1 into file2

• To concatenate files:

cat file1 file2 > file3• or

cat file2 >> file1

Page 32: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 35

Pipes• A pipe is a way to use the output from one

command as the input to another command without having to create intermediary files.

• Example: want to see who is logged in, and you want the result displayed alphabetically:

who > namelist

sort namelist• Using a pipe:

who | sort

Page 33: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 36

Pipes

• Example: want to get a count of the users logged in to the system:

who | wc -l

• If you want to display the output of any command one screen at a time:

command | more

example:

ls -alF | more

Page 34: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 37

Protecting files and directories

• The ls -l command display detailed listing of a file, including its protection mode:

drwxrwxrwx owner size directoryname …..-rwxrwxrwx owner size filename …

• the first character (d or -) indicates whether it is a file or directory name.

• The following 9 character indicate the protection mode.

Page 35: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 38

Protecting files and directories

rwx rwx rwx

• Each group of three characters describes the access permission: read , write and execute

• the first three settings pertain to the access

permission of the owner of the file or directory, the middle three pertain to the group to which the owner belongs, and the last three pertain to everyone else.

Page 36: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 39

Access rights on files.

• r (or -), indicates read permission, that is, the presence or absence of permission to read and copy the file

w (or -), indicates write permission; that is, the permission to change a file

x (or -), indicates execution permission; that is, the permission to execute a file, where appropriate

example: -rwxrw-r--

Page 37: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 40

Access rights on directories. r: allows users to list files in the directory; w: means that users may delete files from the

directory or move files into it. Never give write permission to others to your

home directory or any of its subdirectories. x: means the right to access files in the directory.

This implies that you may read files in the directory if you have read permission on the individual files.

example: drwxrw-r--

Page 38: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 41

Changing file access permission

chmod (changing protection mode)

• Consider each group of three to be a 3-bit number

example: you want to set permission to

rwx r-- ---

111 100 000

7 4 0

chmod 740 filename

Page 39: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

chmod codes

Users– u – owner

– g – group

– o – others

– a - all

Access code

• r – read

• w – write

• x- execute

CS4315 A. Berrached::CMS::UHD 42

Examples:chmod g+w filenamechmod o-r filenamechmod a-x filename

Page 40: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 43

Process & job control

• A process is an executing program with a unique ID (PID).

• To display information about your processes with their PID and status:

ps

• to display a list of all processes on the system with full listing

ps -Af

Page 41: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 44

Process & job control commands

• A process may be in the foreground, in the background, or be suspended. In general the shell does not return the UNIX prompt until the current process has finished executing.

• To run a program in the background, append a & at the end of the command

prog1 &

[1] 6259

• system returns the job number and PID

Page 42: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 45

Process & job control commands

• To suspend a running process

CTRL Z• example: % prog

CTRL Z• To background a running process

CTRL Z

bg• To bring a process to forground

fg %job

Page 43: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 46

Process & job control commands

• to kill a background process

kill PID• to suspend a running background process

stop PID

Page 44: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 47

Process & job control commands

• Background process can not use the standard I/O. ==> Need to redirect I/O

e.g: grep mysort *.c &

output will be lost

grep mysort *.c > file1 &

Page 45: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

Kill Command

• kill –s STOP PID --- suspend• kill –s CONT PID --- resume• kill –s TERM PID --- terminate

CS4315 A. Berrached::CMS::UHD 50

Page 46: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 52

Compiling C programs

cc [options] file …• by the default, the resulting executable is a.out

cc prog.c

cc -o prog prog.c• names the resulting executable prog instead of a.out

• Compile a C++ program

g++ prog.cpp

Page 47: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 53

Editing files

Available editors:• vi • emacs• pico

Check references on web

Page 48: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

vi commandsa – append

i – insert

x – erase

Esc – return to command mode

Shift : - enter file command mode

w – write

q – quit

w! – force write

q! – force quit

CS4315 A. Berrached::CMS::UHD 54

Page 49: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

File transfer• WS_FTP - a GUI ftp program• ftp on command prompt

– ftp gator.uhd.edu– After login:

• put – transfer a single file from the current folder on local machine to the current directory on Gator

• mput – upload multiple files

• get – transfer a single file from the current directory on Gator to the local machine

• mget – download multiple files

• cd – change the current directory on Gator

• lcd – change the current folder on the local machine

• bye – quit ftp

CS431 A. Berrached::CMS::UHD 55

Page 50: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 56

Example: Writing a C program on Unix

• Write a program that counts the number of non white-space characters in a text file. Program takes as command argument the name of the input file and displays the output on the standard output.

Page 51: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 57

// Character Count: Basic Algorithm#include <stdio.h>#define BLANK ' '#define NEWLINE '\n'int main(int argc, char *argv[]){ FILE *infile;

char c;int char_count=0;// count the number of charecters in infilewhile ( (c = getc(infile)) != EOF)

if ((c != BLANK) && (c != NEWLINE) )++char_count;

printf("%d characters\n", char_count);return 0;

}

Page 52: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 58

Testing the # of command arguments

if (argc != 2)

{

fprintf(stderr, " %s: expects 1 argument but was given %d\n", argv[0], argc-1);

fprintf(stderr, "Usage: %s inputfile \n", argv[0]);

exit(1);

}

int printf( char *format, arg1, arg2, ….)

int fprintf( FILE * stream, char *format, arg1, arg2, ….)

Page 53: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

Format specifiers used by printf

• %c – character• %[n]s – string. n is the width of the field• %[n]d – integer• %[n.m] – floating point number

– n – the width of the field– m – the number of digits in the fractional part

CS4315 A. Berrached::CMS::UHD 59

Page 54: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 60

Opening input file

if ( (infile = fopen(argv[1], "r")) == NULL)

{

fprintf(stderr,"%s: cannot open %s \n", argv[0], argv[1]);

exit(1);

}

File *fopen(char *filename, char *mode);

Page 55: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 61

file modes

"r" : open text file for reading

" w" : for writing

"a" : for appending

"r+" : reading and writing

"w+": for reading and writing (discard existing file)

"a+": open text file for appending

Page 56: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 62

Count and return # of chars

// count the number of charecters in infile

while ( (c = getc(infile)) != EOF)

if ((c != BLANK) && (c != NEWLINE) )

++char_count;

printf(" %d characters\n ", char_count);

return 0;

}

Page 57: CS4315A. Berrached::CMS::UHD1 Quick UNIX Tutorial

CS4315 A. Berrached::CMS::UHD 63

The End of Quick Unix Tutorial