Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.

Similar presentations


Presentation on theme: "Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program."— Presentation transcript:

1 Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program as a background process More Unix/Linux utilities Search and Replace utilities (grep, sed, awk) Security utilities Using vi editor Writing a basic shell script 1

2 Lecture 02CS311 – Operating Systems 1 2 Redirection Redirection can be either output or input redirection Output redirection – “>” Sends output to the file listed after the > instead of to stdout (usually the monitor). – A new file is created in the instance above if a file does not exist. The file will be replaced if it exists. – “>>” same as above but appends the contents to a file if it exists Input redirection is achieved by “<“ 2

3 Pipes Very important feature in a Linux system Pipes enable us to execute more than one command in a single line. Pipes are represented by “|” symbol Syntax Prompt> command1 | command2 | command3 … Lecture 02CS311 – Operating Systems 1 3

4 Terminating a command Ctrl + C – Terminates a command/process Ctrl + Z – Suspends a command/process Ctrl + D – End of file (EOF) Lecture 02CS311 – Operating Systems 1 4

5 Running programs in background Running program as a background – Insert “&” at the end of command to run it in the background (bg command has similar effect) – fg command brings the process to the foreground “ps” list the processes currently running – -a option list all processes “kill” command terminates a process Lecture 02CS311 – Operating Systems 1 5

6 Browsing through history Browse through the command history using up and down cursor keys when in the prompt Search a previously executed command using Ctrl + r Lecture 02CS311 – Operating Systems 1 6

7 Lecture 02CS311 – Operating Systems 1 7 More UNIX Utilities wc - print the number of newlines, words, and bytes in files – wc –l prints only the number of lines in a file Global Regular Expression Print (grep) – Search text utility from the command prompt – Syntax: grep “pattern” filename – “pattern” can be simple keyword search or regular expressions tr - translate characters in a file – Syntax: tr “source_char” “target_char” < filename cut – to separate sections from files 7

8 Security Utilities - User User is any person who can access a linux system. Two types of users are added by default when linux is installed – Regular user – Root user/Administrator We can login as root using command “su –” (su stands for switch user) or use “sudo” to perform root actions. To login as another user use “su –username” useradd command - add users to a linux system All users in a linux system is listed under /etc/passwd Lecture 02CS311 – Operating Systems 1 8

9 Security Utilities - Groups Linux categorizes users into groups. Some of the commands related to group are – groups - print the groups a user is in – groupadd – add a new group – newgrp - log in to a new group – chgrp - change group ownership – usermod -G – add users to the group /etc/group file lists all the group created in the Linux system Lecture 02CS311 – Operating Systems 1 9

10 Lecture 02CS311 – Operating Systems 1 10 File Permissions Unix/Linux filesystem security is achieved via 2 mechanisms: – file ownership (use chown command to change ownership of the file) – file permissions (chmod command discussed in next slide) 10

11 Lecture 02CS311 – Operating Systems 1 11 File Permissions - chmod File permissions are set for users/owners, groups and the rest of the world (others) Represented as “user/group/other” Octal number representation – 4 for read, 2 for write, 1 for execute – 741 -> 111 100 001 -> rwx for owner, r for group, x for world chmod 754 myDir – owner can ls, add files, and search the directory for a file – group can ls and search the directory for a file – world can search the directory (but cannot ls to see what else is there) 11

12 File Permissions - chmod Text representation – “u” user (owner) – “g” group – “o” other – “+” add permission – “-” remove permission – “r” read, “w” write, “x” execute Example: chmod u+rwx,g+rw,o+r file – gives rwx permission for user, rw for group and r for others Lecture 02CS311 – Operating Systems 1 12

13 Meaning of rwx on files and directories ReadWriteExecute Files A user can open and read a file. A user can make changes to a file. A user can execute the file or search contents in the file Directories A user can move into the directory A user can change the contents of a directory A user can list directory contents or search files Lecture 02CS311 – Operating Systems 1 13

14 Search and replace utilities grep used only for searching patterns or keywords in files. To search and replace contents of file then sed and awk are powerful utilities. Problem with sed and awk – poor documentation Check out the Resource page in our class website to learn more about these two commands. Lecture 02CS311 – Operating Systems 1 14

15 Sed Acronym for “Stream Editor” Used to search and replace file contents and make modifications to a file programmatically. Usage of commands in sed – “s” for substitution. Format “s/find pattern/replace string/g” – Example: ‘sed “s/day/night/g” text’ replaces all patterns “day” with “night”. If “g” not provided in the command, only the first occurrence is replaced. – “p” for print. Example: ‘sed “s/day/night/g”p text’ prints the output to stdout. Look into the script for more details Lecture 02CS311 – Operating Systems 1 15

16 Awk Excellent filter and report generator tool Particularly useful to manipulate tables General form of awk awk ‘BEGIN{action} #action performed at start pattern{action} #pattern is optional END{action}’ < file #action after script ends We can also include C statements inside action Positional variables - $1, $2, $3, etc. refers to the nth field in a string Lecture 02CS311 – Operating Systems 1 16

17 Using vi editor vi is an in-built text editor in UNIX To start vi – “vi filename” Two modes of vi – Command mode (mode at startup) – Text mode To go to text mode we can use these commands – “i” – insert in front of cursor – “a” – append text after cursor – “r” – replace one character Lecture 02CS311 – Operating Systems 1 17

18 Using vi editor In command mode you can use these commands – Cursor keys or “h,j,k,l” to move cursor – ^ - move cursor to start of line – $ - move cursor to end of line – dd – delete a line – :w – save file – :q – quit editor if file is saved – :q! – quit without changing file – :wq – save and quit editor We can go to command mode from text mode by pressing “Esc” Lecture 02CS311 – Operating Systems 1 18


Download ppt "Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program."

Similar presentations


Ads by Google