Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-1
System Consultant1
UNIX Power User Training
Unix Courseware
Version 1.2
2
Introduction to UNIX
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-2
3
What is an Operating System?
• Software that manages (allocates and de-allocates)
system resources in an efficient and secure manner
• System resources consist of hardware (e.g.
terminals, printers, storage device, etc) and
software (e.g. application programs, language
libraries, etc.)
4
System Resources
System
Resources
Hardware
ComponentsSoftware
System
Software
Application
Software
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-3
5
Different Types of Operating Systems
• Single-user, single-process operating systems:
allow only one user at a time to use the computer
system. The user can execute/run only one
process at a time.
– Examples: DOS, Windows 3.1
• Single-user, multi-process operating systems:
allow a single user to use the computer system;
however, the user can run multiple processes at
the same time.
– Example: OS/2
6
Different Types of Operating Systems
• Multi-user, multi-process operating systems:
allow multiple users to use the computer system
simultaneously. Each user can run multiple
processes at the same time.
– Examples: UNIX, Windows XP
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-4
7
UNIX Operating System
• UNIX is a multi-user, multi-processing, portable(?)
operating system.
• UNIX is designed to facilitate programming, text
processing and communication
8
Components of UNIX: perspective 1
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-5
9
Components of UNIX: perspective 2
10
History of UNIX
• Invented by Ken Thompson at AT&T in 1969
• First version written in assembly language– single user system, no network capability
• Thompson, Dennis Ritchie, Brian Kernighan– rewrote Unix in C: processor/architecture independent
• Unix evolution:I. Bell Labs, USL, Novell, SCO
II. AIX, Ultrix, Irix, Solaris, …
III. BSD, FreeBSD, Mach, OS X
IV. Linux, Redhat, Suse, …
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-6
11
What Can UNIX Do for You?
• User’s Support Tools
– Text processing (vi, sed, awk)
– Filing system
– E-mail and networking
– Electronic databases
• Programmer’s Support Tools
– Programming languages & compilers (C, C++)
– Shell scripts
– Programming Workbench
• Source Code Control System (SCCS)
• Revision Control System (RCS)
12
Some Standard UNIX Shells
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-7
13
Command Source and Destination
14
Command Line Structure
mp% command [-options] [arguments]
Command
prompt
Command
name
Arguments can be:
1. More information
2. Object identifiers
3. Names of files
Notes:
•UNIX is case sensitive!!!!
•Example: the command “ls –l” is not the same as “LS –L”
• Must be a space between the command, options and arguments
• No space between the plus or minus sign and the option letter
• Option letters must be typed exactly as they are indicated,
uppercase or lowercase
•Fields enclosed in [ ] are optional
•Must press [Return] after you have completed entry of a command
Command modifier;
usually one character
preceded by + or - sign
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-8
15
Correcting Mistakes
Note: UNIX is case sensitive (use lowercase)
Key Pressed Result
Backspace, Ctrl-h Back up & erase last character
Ctrl-c Terminates the current command
Ctrl-r Redraws the current command line
Ctrl-s Stops scrolling of output on screen
(Ctrl-q to resume/start scrolling)
Ctrl-w Erases a word on command line
Ctrl-u Erases/deletes entire command line
16
Command Line Structure
• How do the results of the following commands
differ?
• According to the synopsis of the “sort” command,
what parts of the “sort” command are required?
sort list
sort –f list
sort –o sorted list
Command
name
Command
option
Option
argument
Command
argument
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-9
17
The UNIX File System
18
A Directory Hierarchy
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-10
19
Sample UNIX File Structure (NIU, CS)
UNIX supports “tree-like” directory structure
usretcdev home
uxmptty null skel bin local ucb
z036473
.cshrc .logoutcsci330
Root Directory
Home Directory
Root subdirectories
z036473
subdirectory
Files in the
z036473 directory
20
Directory Types
• Root Directory: /
– The first directory in any UNIX file structure
– Always begin with the forward slash (/)
• Home Directory: $HOME or ~
• Created by system administrator
• This is where you are when you first log in!
• Under $HOME, you may create your own directory structure
• Type: cd [Return] takes you $HOME
• Current Working Directory: .
– The Directory you are currently working in
– Also called Current Working Directory (cwd)
• Parent Directory: ..
– The directory immediately above your current working directory.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-11
21
File Types
- Root (/)
- Home (~)
- Working (.)
- Parent (..)
- Text
- Binary
22
Paths and Pathnames
Two ways of locating a file or a directory:
• By Using Absolute Pathname– Full pathname
– Traces a path from root to a file or a directory
– Always begins with the root (/) directory!
– Example: /home/ux/krush/unix/assignments/assign1.sp04
• By Using Relative Pathname– Traces a path from the ‘cwd’ to a file or a directory
– No initial forward slash (/)
– Two dots (..) goes up one level on file structure
– Dot (.) points to current working directory (cwd)
– Example: unix/assignments/assign1.sp04
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-12
23
Relative Pathnames for file3
Absolute Pathname:
/usr/staff/joan/file3
24
Directory Operations
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-13
25
Display Current Directory’s Full Pathname
• To determine the full pathname of the current
working directory, use the command named
“pwd”
• pwd stands for print working directory
Example: To display the full pathname of the current
working directory
ux% pwd
/home/ux/krush/unix
26
The ls Command
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-14
27
Long List Option
28
List ALL Contents of Current Directory
ux% ls -la
total 126
drwxr-xr-x 13 krush csci 1024 Apr 26 15:49 .
drwxr-xr-x 15 root root 512 Apr 24 15:18 ..
-rwx------ 1 krush csci 1120 Apr 12 13:11 .cshrc
-rwxr--r-- 1 krush csci 885 Dec 2 13:07 .login
-rw-r--r-- 1 krush csci 141 Mar 14 13:42 .logout
--rwx------ 1 krush csci 436 Apr 12 11:59 .profile
drwx------ 7 krush csci 512 May 17 14:11 330
drwx------ 3 krush csci 512 Mar 19 13:31 467
drwx------ 2 krush csci 512 Mar 31 10:16 Data
-rw-r--r-- 1 krush csci 80 Feb 27 12:23 crontab.cron
List contents of the current
directory in long format. Points to cwd,
.. Points to parent’s
dir.
File names
begins with
a dot (.) are
hidden files
Directories
Regular
text file
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-15
29
List Contents of a Specific Directory
ux% ls -l unix/grades
total 10
-rwxr-xr-x 3 krush csci 72 Jan 19 19:12 330assign-graderun
-rwxr-xr-x 1 krush csci 70 Jan 19 19:13 330exam-graderun
-rwxr-xr-x 2 krush csci 70 Jan 19 19:12 330quiz-graderun
-r-x------ 1 krush csci 468 Feb 1 11:55 test-330grade
-r-x------ 1 krush csci 664 Feb 1 11:55 test-330grade,v
Listing contents of a subdirectory named
“unix/grades”
30
File Name Expansion & Wildcards
Allows you to select files that satisfy a particular name pattern (wildcards)
Character Description Example
* Match zero or more char. ls *.c
? Match any single character ls conf.?
[list] Match any single character in list ls conf.[co]
[lower-upper] Match any character in range ls lib-id[3-7].o
str{str1,str2,…} Expand str with contents of { } ls c*.{700,300}
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-16
31
The mkdir Command
32
Creating a New Directory
• To create a directory, use the command named
“mkdir”
Example: To create two new directories called “csci330”
and “test-data”
ux% mkdir csci330 test-data
ux% mkdir /home/ux/krush/unix/demo
must already exist
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-17
33
Directory Names
• Use the following characters:
– Uppercase letters (A-Z)
– Lowercase letters (a-z)
– Numbers (0-9)
– Underscore ( _ )
– Period/dot ( . )
34
Directory Names
• When naming a directory, avoid the following
characters:
& * \ | [ ] {}
$ <> () # ? /
“ ‘ ; ^ ! ~
Space Tab
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-18
35
Example: Create a Directory Creation
Create a directory called Data under csci330
a) Using Absolute Pathname:
b) Using Relative Pathname:
usretcdev home
uxmptty null skel bin local ucb
z036473
.cshrc .logoutcsci330
You are here
Data
Temp
mkdir /home/mp/z036473/csci330/Data
mkdir csci330/Data
36
The cd Command
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-19
37
Changing Directory
In the Data directory, go to $HOME directory
a) Using Absolute Pathname:
b) Using Relative Pathname:
usretcdev home
uxmptty null skel bin local ucb
z036473
.cshrc .logoutcsci330
You are here Data
Temp
cd /home/mp/z036473
cd $home cd cd ~ cd ~z036473cd ../..
38
Remove Directories
• To remove an empty directory – a directory that does not contain user-created files, use the command named “rmdir”Example: To remove a directory called “test”, which
does not contain user-created files.
ux% rmdir test
• To remove a non-empty directory, use the command named “rm –r”Example: To remove a non-empty directory called “old-
data”
ux% rm –r old-data
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-20
39
Operations Common to Directories and
Regular Files
40
Copying Files
• To copy a file, use the command named “cp”
• Syntax: cp source-file new-file
• Commonly used options:
-i if “new-file” exists, the command cp prompts for
confirmation before overwriting
-p preserve permissions and modification times
-r recursively copy files and subdirectories
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-21
41
Copying Files
• “source-file” must have read permission.
• The directory that contains “source-file” must have
execute permission.
• The directory that contains “new-file” must have
write and execute permissions.
• Note that if “new-file” exists, you do not need the
write permission to the directory that contains it,
but you must have the write permission to “new-
file”.
42
Moving Files
• To move files from one directory to another
directory, or to re-name a file, use the command
named “mv”.
• The directory that contains the source file and the
destination directory must have write and execute
access permissions.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-22
43
Moving Files
• Syntax: mv source-file destination-file
• If the destination file exists, “mv” will not
overwrite exiting file.
Example: Move “assign1.txt” a different directory and
rename it to “assign1.save”
ux% mv assign1.txt $HOME/archive/assign1.save
ux% mv assign1.txt $HOME/archive
44
Moving a File
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-23
45
Rename Directories
• To change the name of an existing directory, use
the command named “mv”
Example: To rename the file called “unix” to
“csci330”
ux% mv unix csci330
• For the above example, what happens if “csci330”
already exists in the current directory and it is the
name of a directory?
46
The mv Command
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-24
47
Removing/Deleting Files
• You should remove un-needed files to free up disk space.
• To remove/delete files, use the command named “rm”.
• Syntax: rm file-list
• Commonly used options:-f force remove regardless of permissions for “file-list”
-i prompt for confirmation before removing
-r removes everything under the indicated directory
48
Removing/Deleting Files
• If “file-list” contains pathname, the directory
components of the pathname must have execute
permission.
• The last directory that contains the file to be
deleted must have execute and write permissions.
Example: Remove the file named “old-assign”
ux% rm unix/assign/old-assign
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-25
49
Recap: Common Operations on Files
50
Finding Files
• The command named “find” can be used to locate
a file or a directory.
• Syntax: find pathname-list expression
• “find” recursively descends through pathname-list
and applies expression to every file.
• For syntax of expression, see Course Notes pp. 13-
9
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-26
51
Finding Files
Example 1: Find all files, in your directory hierarchy, that
have a name ending with “.bak”.
ux% find $home –name “*.bak” –print
Example 2: Find all files, in your directory hierarchy, that
were modified yesterday.
ux% find $home –mtime –1 -print
52
The “ln” command
• Allows file to listed in multiple directories
• 2 types:
– Hard link
– Symbolic link
• First: understand Unix file storage and organization
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-27
53
Unix file organization
• Computer has one or more physical hard drives
• Hard drive is divided into partitions
• Partition holds file system
– File system is set of data blocks
– Data blocks contain
• general information
• actual file data
• directory information
54
Blocks in a file system
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-28
55
inode
• Index (or information) node: one inode per file
• Each inode has unique number
• contents:– File type, access permissions, link count
– UID, GID
– Date and time of the file’s last• Data access (read and execute)
• Data modification (written)
• I-node modification (permission change)
– Data blocks assigned to the file
56
Inodes in a filesystem
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-29
57
inode Contents: where is the file data ?
Inode may store:
– 10 addresses of data blocks that belong to file
– 1 address of a block that contains data block addresses
– 1 address of a block that contains addresses of blocks
that contain data block addresses
– 1 address of a block that contains addresses of blocks
that contain addresses of blocks that contain data
block addresses
58
I-node Structure
Access, Links, and
other information
1
2
.
.
9
10
11
12
13
I-node blocks
triple indirectblock
indirectblock
blocksblocks
blocks
double indirectblock
blocks
blocks
blocks
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-30
59
Directory representation
Directory is a file:
– Has inode like regular file, but different file type
– Data blocks of directory contains simple table:
Name Inode number
60
Example structure
. 1076
.. 2083
myfile 2764
Contents of dir12763
2764
2765
.
.
.
Data blocks
on disk
I-node
list
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-31
61
Example: user view vs. system view
62
Output: ls -li
ux% ls -li crontab.cron118282 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 crontab.cron
I-node
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-32
63
Howto share Files ?
• Duplicate shared files.
• Create common login name for members of the
team.
• Set appropriate access permissions on shared files.
• Create common group for members of the team.
• Share files via links.
64
Duplicate Shared Files
• Make copies of shared files and give them to all
members of the team.
• The simplest way to share files.
• Does not work well if members of the team work
on the files at the same time.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-33
65
Create Common Login Name
• All team members use a common login that gives
them access to shared files.
• This is a simple solution that works well if the size
of the team is small and stable.
• A disadvantage is team members have to use a
separate account, not their regular account.
66
Set Appropriate Access Permissions
• Team members put all shared files in one place
and set access permissions so all team members
can access them.
• This scheme works well if all team members are
in the same group (group permissions can be
used).
• A disadvantage is if the group used has other users
in it, they will also have access to the files.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-34
67
Create Common Group
• This solution is similar to setting appropriate
permissions except a new group is created that
contains just the members of the team.
• All team members have individual logins.
• This is an effective solution, especially if it is with
version control.
68
Linking Files
• To share a single file with multiple users, a link can
be used.
• A link is:
– A reference to a file stored elsewhere on the system.
– A way to establish a connection to a file to be shared.
• Two types:
– Hard link
– Symbolic link (a.k.a. “soft link”)
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-35
69
Hard Link
Advantages Disadvantages
Allow access to original file name
via the file name or the I-node
number
The original file continues to exist
as long as at least one directory
contains its I-node
Checks for the existence of the
original file
Cannot link to a file in a different
file system
Prevents owner from truly deleting
it, and it counts against his/her disk
quota
70
Hard Link
home
z036473
dir3
dir1 dir2
aa
bb
From dir3, link to the file ‘aa’ in dir1
name it ‘bb’:
% ln /home/z036473/dir1/aa bb
Syntax: ln shared-file link-name
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-36
71
The ln Command
72
A Hard Link
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-37
73
2406
2407
2408
.
.
.
Hard Link
home
z036473
dir3
dir1 dir2
aa
bb
. 1076
.. 2083
aa 2407
Contents of dir1
. 1070
.. 2050
bb 2407
Contents of dir3
74
Symbolic Link
Advantages Disadvantages
Allow access to original file name
Can use either relative or absolute
path to access the original file
Can cross partition and drives
Allows the creation of a link to a
directory
Created without checking the
existence of the shared file
Cannot access the shared file if its
path has restricted permissions
Can be circular linked to another
symbolic linked file
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-38
75
Symbolic Link
• A hard link may not be created for a file on a different file system
• Use symbolic link
• The linked files do not share the same I-node number
Syntax: ln –s shared-file link-name
Also called source-file Also called target-file
76
Symbolic Links to Different File Systems
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-39
77
User’s Disk Quota
• A disk quota is set for each user account
• The command: quota –vdisplays the user’s disk usage and limits
• 2 kinds of limits:– Soft limit: ex. 3MB
• Maybe exceeded for one week
• System will nag
– Hard limit: ex. 4MB• Cannot be exceeded
78
Operations Common to Directories and
Regular Files
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-40
79
Operations Unique to Regular Files
Create EditDisplay
ContentsPrint Others
80
Creating New Files
Create Regular
Files
vi picocat
Redirect
Command
Output
See Text Editors
Section 03
See the C shell
Section 06
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-41
81
Creating A File With cat
Example:
mp% cat > myfile
This is line 1 of input
Line 2 of input
^d
mp%
82
Editing Text Files
Editing Text
Files
pico sedvi awk
See Text Editors
Section 03
See course notes
section 10
See course notes
section 08
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-42
83
Displaying Contents of Text Files
Display Text
File contents
cat more less pg head tail
84
The cat Command
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-43
85
Viewing Contents of Text Files
• The command named “cat” can be used to
display/concatenate one or more files, displaying
the output all at once.
Example: Display the contents of a file called
“assign1.txt”.
ux% cat assign1.txt
86
Viewing Contents of Text Files
• The commands named “more” and “less” can be used to display the contents of one or more files one page at a time.Space bar – to advance to next page
b – to go back a page
Enter Key – to advance to next line
Example: Display the contents of a file called “assign1.txt” one page at a time.
ux% more assign1.txt
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-44
87
Viewing Contents of Text Files
• The command named “head” can be used to
display the beginning portion of indicated file(s);
the default head size is 10 lines.
Example: Display the first 20 lines of a file called
“assign1.txt”.
ux% head –20 assign1.txt
88
Viewing Contents of Text Files
• The command named “tail” can be used to display
the ending portion of indicated file(s); the default
tail size is 10 lines.
Example: Display the last 10 lines of a file called
“assign1.txt”.
ux% tail assign1.txt
ux% tail –10 assign1.txt
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-45
89
Other Operations Unique to Text Files
Other File
Operations
Combine
contents
Extract
contents
Compare
contents
File
size
Compress
contentssort
Unique
lines
Encrypt/
decrypt
90
Combining Contents of Files
• Method 1: To vertically concatenate the contents
of two or more files, use the command named
“cat” with output redirection (>).
• Syntax: cat file-1 file-2 file-3 > all-file
• “all-file” will contain the combined contents of
file-1, file-2, and file-3 in top-down (vertical)
fashion
• See demo
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-46
91
Combining Contents of Files
• Method 2: To horizontally concatenate contents
(columns/fields) of two or more files, use the
command named “paste”.
• Syntax: paste file-1 file-2
• See demo
92
Extracting Contents of Files
• To extract one or more fields in a file, use the
command named “cut”.
Example: Extract the month and year fields from the output
of the “date” command.
ux% date
Mon Feb 2 20:37:38 CST 2004
ux% date | cut -d' ' -f2,6
Feb CST
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-47
93
The cut Command
94
Comparing Files: comm
• The command named “comm” can be used to
compare lines that are common in two sorted files.
• Syntax: comm [options] file-1 file-2
• The output contains three columns:
– Column1 contains lines unique to file-1
– Column 2 contains lines unique to file-2
– Column 3 contains lines common to both files
• See Demo
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-48
95
Comparing Files: diff
• The command “diff” can be used to compare two
files line by line.
• Syntax: diff [options] file-1 file-2
• If file-1 and file-2 are the same, no output is
produced.
• If file-1 and file-2 are not the same, diff reports a
series of commands that can be used to convert the
first file to the second file.
• See Demo
96
Determining File Size
• Recall: The “ls” command with the option –l gives the file size in bytes.
• Use the command named “wc” to display the size of files as number of lines, words, and characters.
• Syntax: wc file-list
• Commonly used options:-l display the number of lines
-w display the number of words
-c display the number of characters
• See demo
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-49
97
The wc Command
98
Compress File Contents
• The command named “compress” can be used to
reduce the size of one or more files.
• Syntax: compress file-name
• To display compression percentage and the names
of compressed files, use the option named –v.
• The file extension .Z is automatically appended to
file-name.
• See Demo
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-50
99
Viewing a Compressed File
• The command named “zcat” can be used to
display the contents of a compressed file in a
readable format.
Example: Display the contents of a compressed file called
“customer-data.Z”.
ux% zcat customer-data.Z
100
Un-compress File Contents
• To uncompress one or more compressed files,
used the command named “uncompress”.
• Syntax: uncompress file-list
Example: uncompress a compressed file called
“customer-data.Z”.
ux% uncompress customer-data.Z
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-51
101
Sorting Files
• To sort a text file in ascending or descending order, use the command named “sort”.
• Syntax: sort [options] file-name
• Commonly used options:-r sort in reverse order
-n numeric sort
+x [-y] specify a field as the sort key; skipping x fields and start sorting with field y
-f consider lowercase and uppercase to be equivalent
102
The sort Command
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-52
103
Fields Within A Line
104
Field Specifier Examples
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-53
105
Global and Local Options
106
Removing Repeated Lines
• The command named “uniq” can be used to
remove repetitious lines from a sorted input file,
sending unique (unrepeated) lines to standard
output.
• Syntax: uniq sorted-file-name
• Commonly used options:
-c place a count of repeated lines at beginning of each
output line
-d display the repeated lines
-u display the lines that are not repeated
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-54
107
The unique Command
108
Encrypting Files
• Encryption is a process that transforms a file to an unreadable form.
• The transformed file is called an “encrypted” file.
• You encrypt files to prevent other users from reading their contents.
• The command named “crypt” can be used to encrypt files.
• Syntax: crypt key < original-file > encrypted file
• See Demo
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-55
109
Decrypting Files
• The process of transforming an encrypted file to its
original format is called “decryption”.
• To decrypt an encrypted file, use the command named
“crypt”.
• Syntax: crypt key < encrypted-file > original-file
• See Demo
110
UNIX Text Editors
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-56
111
Editor Concepts
• Editing – creating a new file and modifying an existing text file.
• An editor – a utility that makes the editing task possible.
• A text editor differs from a word processor in that it does not perform text formatting, such as bold, center, underline, etc.
• A line editor – a utility that applies changes to a line or group of lines; two common line editors: ex and sed.
• A screen editor – shows a whole screen of text at a time; we can move cursor or select part of text, search for text, etc.
112
The Vi Editor
• A screen editor available on most UNIX systems.
• When invoked, it copies the contents of a file to a memory space know as a work buffer.
• All editing are applied to the contents in the work buffer.
• If the file does not exist, an empty buffer is created.
• When we exit vi, the work buffer is erased.
• At exit time, we can do two things:1. Quit without saving – the original contents remain
unchanged.
2. Save the file – the original contents are replaced by the new version in the work buffer.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-57
113
Vi Buffers
• Work Buffer– This is where vi performs all editing
– Vi makes a copy of your file in the work buffer
– Makes all changes to the copied version
– Replaces original copy with edited copy when you save (:w)
• General Purpose Buffer– Stores recent deleted, or copied text
– Used by the ‘undo’ command to restore text
• Name Buffers– 26 named buffers, each named by a letter of the
alphabet
– Used to store a different block of text to recall later
114
The UNIX vi Text Editor
CommandMode
InputMode
Last-LineMode
Insert (i, I)Append (a, A),Open (o, O)Change (c),
Replace (r, R)
Esc
Return:
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-58
115
Vi Mode
• In vi, to find out what mode you are in, you need
to create a vi start-up file (.exrc)
• In your home directory or in your cwd directory,
create “.exrc” file with contents:
set showmode
116
File Names
• When naming a file, avoid the following
characters:
& * \ | [] {}
$ <> () # ? /
“ ‘ ; ^ ! ~
Space Tab
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-59
117
Commonly Used File Extensions
Extension File Type
.a An archive or library
.c C program source
.cc C++ program source
.csh C shell script
.f FORTRAN program source
.sh Bourne shell script
.bsh Bash shell script
.ksh Korn Shell script
118
Commonly Used File Extensions
Extension File Type
.o Object file of compiled program
.ps Postscript source
.shar Shell archive
.tar Tar archive
.txt ASCII text file
.Z Compressed file
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-60
119
Recovering Text After a Crash
• If the system crashes while you are editing a file
using vi, you may be able to recover your text
• The system sends you an e-mail telling you how to
recover your file
• To recover a file after a system crash:
% vi –r filename
120
Vi Demo
Demo
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-61
121
Access Permissions
122
Terminology
• A user – any one who has Unix account on the
system.
• Unix recognizes a user by a number called user id.
• A super user:
– has the maximum set of privileges in the system
– also know as system administrator
– can change the system
– must have a lot of experience and training
• Users can be organized into groups.
• One or more users can belong to multiple groups.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-62
123
Users
124
Terminology
• To find out group information, use the command
named: groups user-id
• Example: To find out what groups the user
z036473 belongs to.
ux% groups z036473
student csci467a csci330c
• Information about groups is stored in the Network
Information Service (NIS) file named
group.org_dir.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-63
125
Security Levels
• There are three levels of security in UNIX:
system, directory and file.
• System security – controlled by the system
administrator, a super user.
• Directory and file – controlled by the user who
owned them.
126
Security Levels
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-64
127
Access Permission Code
• The protection on a file is referred to as its file modes
• File modes are set with the “chmod” command
• UNIX supports three types of access permissions:r read
w write
x execute
- permission denied
128
Directory and File Permissions
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-65
129
Access Types
Access Type Meaning on File Meaning on Dir.
r (read) View file contents
(open, read)
List directory contents
w (write) Change file contents - Change directory contents
- Be careful !!!
x (execute) Run executable file - Make it your cwd
- Access files (by name) in it
- Permission denied Permission denied
130
Checking Permissions
• To check the permissions of an existing file or an
existing directory, use the command: ls –l
• Example:
ux% ls –l unix
total 387drwxr--r-- 1 z036473 student 862 Feb 7 19:22 unixgrades
-rw-r--r-- 1 z036473 student 0 Jun 24 2003 uv.nawk
-rw-r--r-- 1 z036473 student 0 Jun 24 2003 wx.nawk
-rw-r--r-- 1 z036473 student 0 Jun 24 2003 yz.nawk
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-66
131
Changing Permissions
132
The chmod Command
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-67
133
Changing Permissions: Symbolic Mode
134
Changing Permissions: Symbolic Mode
mp% chmod who operation permissions filename
u for user
g for group
o for others
a for all
+ for add
- for remove
= for assign
r for read
w for write
x for execute
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-68
135
ux% ls -li sort.c
118283 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 sort.c
Changing Permissions: Symbolic Mode
Example 1: To change the permissions on the file “sort.c” using
Symbolic mode, so that:
a) Everyone may read and execute it
b) Only the owner and group may write to it.
rwx|rwx|r-xWe want this using
symbolic mode:
Answer:chmod ug=rwx,o=rx sort.c
chmod ugo+rx,go+w sort.c
136
The chmod Command: Octal Mode
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-69
137
Changing Permissions: Octal Mode
ux% ls -li sort.c
118283 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 sort.c
Example 2: Ignoring the original permission settings, change the
permissions on the file “sort.c” using octal mode, so that:
a) Everyone may read and execute it
b) Only the owner and group may write to it.
rwx|rwx|r-xWe want this using
octal mode:
Answer: chmod 775 sort.c
138
Changing Permissions: Octal Mode
Step Perform… Settings
1 List the desired setting
2 Assign binary:
1 for access; 0 for no access
3 List octal values for the
corresponding binary 1’s
4 Convert the octal values to a 3-
digit number
5 Write the command
ux% ls -li sort.c
118283 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 sort.c
rwx|rwx|r-x
111|111|101
421|421|401
775
chmod 775 sort.c
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-70
139
Changing Permissions: exercise
Ignoring the original permission settings on a file
called “myfile”, assign:
a) Read, write, and execute permissions to owner
b) Read and execute permissions to group
c) Execute permission to others
1) Using Symbolic Mode: __________________________________
2) Using Octal Mode: __________________________________
We want: rwx|r-x|--x
chmod u=rwx,g=rx,o=x myfile
chmod 751 myfile
140
User Masks (default permissions)• The default permissions are initially set for a file or directory
using a three-digit octal system variable called user mask
(mask).
• This user mask was defined initially by the system
administrator when your account is created.
• Initially set by system administrator in a start-up file:
$HOME/.cshrc
• The user mask contains the octal settings for the permissions
to be removed from the default when a directory or file is
created.
• The default permissions are:
– 777 for a directory
– 666 for a file
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-71
141
The umask Command
142
User MaskUser mask
Value
Directory
Default: 777
File (666)
Default: 666
000 777 (rwx rwx rwx) 666 (rw- rw- rw-)
111 666 (rw- rw- rw-) 666 (rw- rw- rw-)
222 555 (r-x r-x r-x) 444 (r- - r- - r- -)
333 444 (r- - r- - r- -) 444 (r- - r- - r- -)
444 333 (-wx –wx –rx) 222 (-w- -w- -w-)
555 222 (-w- -w- -w-) 222 (-w- -w- -w-)
666 111 (- -x - -x - -x) 000 (--- --- --- )
777 000 (--- --- --- ) 000 (--- --- --- )
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-72
143
The command: umask
% umask 000
For regular files: rw-rw-rw- 666
For directories: rwxrwxrwx 777
In $HOME/.cshrc file: umask 022
For regular files: rw-r- - r- - 644
For directories: rwxr-xr-x 755
144
Default Access Permissions
• To prevent cheating, do not grant read and write
access permissions to group and others!
• Set default permissions for all files and directories
created in the future, so that only you/owner can
rw to it (rw- --- ---)
• In your $HOME/.cshrc file: umask 077
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-73
145
Default Access Permissions: exercise
Assume your $HOME/.cshrc file contains the
command: umask 002.
a) What permissions would be set for newly created
text files?
b) What permissions would be set for newly created
directories?
146
Special Permissions
• The regular file permissions (rwx) are used to
assign security to files and directories.
• Three additional special permissions can be
optionally used on files and directories.
– Set User Id (SUID)
– Set Group ID (SGID)
– Sticky bit
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-74
147
Special Permissions: SUID
• SUID enables regular users to execute a program file and become the owner of the file for the duration of execution.
• Example: The “passwd” command is used to change a user’s password. The file “/usr/bin/passwd” (owned by “root”) has this special permission set: r-sr-sr-x
• In theory, only the root user has the ability to change passwords. Since “/usr/bin/passwd” has the SUID permission on, when a regular user executes the “passwd” command, the user temporarily becomes the “root” user while the “passwd” command is executing.
148
Special Permissions: GUID
• Like SUID, SGID enables regular users to execute
a program file and become a member of the group
that is attached to the file.
• If a file is owned by the system group and also has
the SGID permission, then any user who executes
that file will be a member of the system group
during the execution.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-75
149
Special Permissions: Sticky Bit
• Sticky bit performs a useful function on
directories
• Recall: Write permission applied to a directory
enables you to add and remove any files to or from
that directory.
• If you had “write” permission to a certain
directory but no permissions for files within it,
you could delete all of those files.
150
Special Permissions: Sticky Bit
Scenario: A company provides a common directory
that gives all employees the ability to add and
remove files in it. This directory must give user
“write” permissions. Unfortunately, the “write”
permissions also gives all employees the ability to
delete all files and subdirectories within it,
including the ones that others have added to the
directory.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-76
151
Special Permissions: Sticky Bit
• If the “sticky” bit were applied to this common
directory, in addition to “write” permissions, then
employees may add files to the directory.
However, they may delete only files and
directories that they have added/created.
• Example:
ls –ld /tmpdrwxrwxrwt 3 root sys 1077 Jan 25 13:30 /tmp
Sticky Bit
152
Special Permissions
• The access permission status that is displayed
using the “ls –l” command does not have a section
for special permissions
• However, since special permissions required
“execute”, they mask the execute permission when
displayed using the “ls –l” command.
r w x r w x r w x
r w s r w s r w t
SUID SGID STICKY
BIT
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-77
153
Special Permissions
• All special permissions also required the “execute” permission
to work properly.
• Usually, the SUID and SGID apply to executable files.
• The sticky bit applies to directories, which must have execute
permission for access.
• If special permissions are set on a file or a directory without
“execute” permission, the special permissions are shown in
capital letters.
r w - r w - r w -
r w S r w S r w T
SUID SGID STICKY
BIT
154
Setting Special Permissions
suid sgid stb r w x r w x r w x
4 2 1 4 2 1 4 2 1 4 2 1
7 7 7 7
Special user group others
Use the “chmod” command with octal mode:
chmod 7777 filename
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-78
155
Special Permissions: Exercise
• Give the full UNIX command to enable all users
including others to add files to directory named
“dir1”, but only to delete files that they own in
that directory.
• Answer:
____________________________________chmod 1777 dir1
156
Regular Expressions
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-79
157
Regular Expression Metacharacters
• A pattern of special characters used to match
strings in a search
• Can be controlled by special characters called
metacharacters
• Metacharacters are used by UNIX pattern
matching editors and utilities:
– Editors: ed, ex, vi
– Utilities: grep, egrep, sed, and awk
158
RE Metacharacters: use with grep
RE Metacharacter Matches…
^ A character at beginning of line
$ A character at end of line
. Any one character, except new line
* Zero or more of preceding character
\char Escape the meaning of char following it
[a-z] Any one of the enclosed characters (e.g. a-z)
[^] One character not in the set
\< Beginning of word anchor
\> End of word anchor
\( \) Tags matched characters to be used later (max = 9)
x\{m\} Repetition of character x, m times (x,m = integer)
x\{m,\} Repetition of character x, at least m times
x\{m,n\} Repetition of character x between m and m times
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-80
159
RE Metacharacters: use with egrep
RE Metacharacter Matches…
^ A character at beginning of line
$ A character at end of line
. Any one character, except new line
* Zero or more of preceding character
\char Escape the meaning of char following it
[a-z] Any one of the enclosed characters (e.g. a-z)
[^] One character not in the set
+ One or more of the preceding characters
? Zero or one of the preceding characters
a|b Either a or b
( ) Groups characters
160
The grep Family
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-81
161
The Command: grep
• Searches the entire specified input file(s) for a pattern of characters.
• If pattern contains white space, it must be in quotes.
• All other words following RE-pattern are treated as files.
• Sends its output to stdout (default), a file, or a command pipe.
• “grep” does not change the original input file(s).
• “grep” can receive input from stdin (keyboard), a pipe, or file(s)
Syntax: grep [options] RE-pattern file(s)
162
Commonly Used Grep Options
Commonly used “grep” options:-c Print only a count of matched lines.
-i Ignore uppercase and lowercase distinctions.
-l List all files that contain the specified pattern.
-n Print matched lines and line numbers.
-s Work silently; display nothing except error messages. Useful
for checking the exit status.
-v Print lines that do not match the pattern.
-w Search for the expression as a word as if surrounded by \< \>
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-82
163
Regular Expression
An atom specifies what text is to be matched and
where it is to be found.
An operator combines regular expression atoms.
164
Atoms
An atom specifies what text is to be matched and where
it is to be found.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-83
165
Single-Character Atom
A single character matches itself.
166
Dot Atom
A dot matches any single character except for a new
line character (\n)
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-84
167
Class Atom A class matches only single character that can be any of
the characters defined in a set, e.g. [A-C] matches either
A, B, or C.
Notes:
1) A range of characters is indicated by a dash, e.g. [A-C]
2) Can specify characters to be excluded from the set, e.g.
[^0-9] matches any character other than a number.
168
Example: Classes
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-85
169
Anchors
Anchors tell where the next character in the pattern must
be located in the text data.
170
Back References: \n
• Used to retrieve saved text in one of nine buffers
• Can refer to the text in a saved buffer by using a
back reference; e.g. \1 \2 \3 ...\9.
• More on back references later in this section.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-86
171
Operators
no oper.
172
Sequence Operator
In a sequence operator, if a series of atoms are shown in
a regular expression, there is no operator between them.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-87
173
Alternation Operator: |
The alternation operator ( | ) is used to defined one or more
alternatives, e.g. A | B matches A or B.
174
Repetition Operator: \{…\}
The repetition operator specifies that the atom or
expression immediately before the repetition may be
repeated.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-88
175
Basic Repetition Forms
176
Short Form Repetition Operators: * + ?
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-89
177
Group Operator
In the group operator, when a group of characters is
enclosed in parentheses, the next operator applies to the
whole group, not only the previous characters.
178
Save Operator: \(..\)
• Copies a matched text string to one of nine buffers for later reference.
• Back references can then be used to retrieve saved text in one of nine buffers.
• Can refer to the text in a saved buffer by using a back reference; e.g. \1 \2 \3 ...\9.
• Example: To reverse first name and last name in the following data set; also.John DeVries
Bob Flowers
s/^\([A-Z].*\) \([A-Z].*\)$/\2 \1/DeVries John
Flowers Bob
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-90
179
More Examples
• The following slides contain examples of using
regular expressions with grep, egrep and fgrep.
• Review them on your own time.
• Ask your instructor or T.A. if you have any
questions.
180
Example: Grep with RE: ^
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
% grep '^n' grep-datafile
northwest NW Charles Main 300000.00
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
Print all lines beginning
with the letter n.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-91
181
Example: Grep with RE: $
% grep '\.00$' grep-datafile
northwest NW Charles Main 300000.00
southeast SE Patricia Hemenway 400000.00
Extra [A-Z]****[0-9]..$5.00
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00Print all lines ending with a
period and exactly two
zero numbers.
182
Example: Grep with RE: \char
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
% grep '5\..' grep-datafile
Extra [A-Z]****[0-9]..$5.00
Print all lines containing the
number 5, followed by a
literal period and any single
character. The dot
metacharacter represents a
single character, unless it is
escaped with a backslash.
When escaped, the period is
no longer a special character,
but represents itself, a literal
period.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-92
183
Example: Grep with RE: [ ]
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
% grep '^[we]' grep-datafile
western WE Sharon Gray 53000.89
eastern EA TB Savage 440500.45
Print all lines
beginning with either
a “w” or an “e”.
184
Example: Grep with RE: [^]
% cat grep-datafile
northwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
% grep '\.[^0][^0]$' grep-datafilewestern WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
eastern EA TB Savage 440500.45
Print all lines ending
with a period and
exactly two non-zero
numbers.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-93
185
Example: Grep with RE: x\{m\}
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
% grep '[0-9]\{6\}\.' grep-datafilenorthwest NW Charles Main 300000.00
southwest SW Lewis Dalsass 290000.73
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Print all lines where there
are at least six consecutive
numbers followed by a
period.
186
Example: Grep with RE: \<
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
% grep '\<north' grep-datafile
northwest NW Charles Main 300000.00
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
Print all lines
containing a word
starting with “north”.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-94
187
Example: Grep with RE: \< \>
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
% grep '\<north\>' grep-datafile
north NO Ann Stephens 455000.50
Print the line if it
contains the word
“north”.
188
Example: grep with pipe
% ls -l | grep '^d'
drwxr-xr-x 2 krush csci 512 Feb 8 22:12 assignments
drwxr-xr-x 2 krush csci 512 Feb 5 07:43 feb3
drwxr-xr-x 2 krush csci 512 Feb 5 14:48 feb5
drwxr-xr-x 2 krush csci 512 Dec 18 14:29 grades
drwxr-xr-x 2 krush csci 512 Jan 18 13:41 jan13
drwxr-xr-x 2 krush csci 512 Jan 18 13:17 jan15
drwxr-xr-x 2 krush csci 512 Jan 18 13:43 jan20
drwxr-xr-x 2 krush csci 512 Jan 24 19:37 jan22
drwxr-xr-x 4 krush csci 512 Jan 30 17:00 jan27
drwxr-xr-x 2 krush csci 512 Jan 29 15:03 jan29
% ls -l | grep -c '^d'
12
Pipe the output of the
“ls –l” command to
grep and list/select
only directory entries.
Display the number of
lines where the pattern
was found. This does
not mean the number
of occurrences of the
pattern.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-95
189
Example: egrep with RE: a|b
% egrep 'NW|EA' grep-datafile
northwest NW Charles Main 300000.00
eastern EA TB Savage 440500.45
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
Prints the line if it
contains either the
expression “NW” or the
expression “EA”.
190
Example: egrep with RE: +
% egrep '3+' grep-datafile
northwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00
Print all lines containing
one or more 3's.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-96
191
Example: egrep with RE: ?
% egrep '2\.?[0-9]' grep-datafile
southwest SW Lewis Dalsass 290000.73
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00Print all lines containing
a 2, followed by zero or
one period, followed by a
number.
192
Example: egrep with RE: ( )
% egrep '(no)+' grep-datafile
northwest NW Charles Main 300000.00
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00Print all lines containing
one or more consecutive
occurrences of the
pattern “no”.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-97
193
Example: egrep with RE: (a|b)
% egrep 'S(h|u)' grep-datafile
western WE Sharon Gray 53000.89
southern SO Suan Chin 54500.10
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00Print all lines containing
the uppercase letter “S”,
followed by either “h” or
“u”.
194
Example: fgrep
% fgrep '[A-Z]****[0-9]..$5.00' grep-datafile
Extra [A-Z]****[0-9]..$5.00
% cat grep-datafilenorthwest NW Charles Main 300000.00
western WE Sharon Gray 53000.89
southwest SW Lewis Dalsass 290000.73
southern SO Suan Chin 54500.10
southeast SE Patricia Hemenway 400000.00
eastern EA TB Savage 440500.45
northeast NE AM Main Jr. 57800.10
north NO Ann Stephens 455000.50
central CT KRush 575500.70
Extra [A-Z]****[0-9]..$5.00Find all lines in the file
containing the literal string
“[A-Z]****[0-9]..$5.00”.
All characters are treated as
themselves. There are no
special characters.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-98
195
The C Shell
196
Standard UNIX Shell Overview
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-99
197
Objectives
• C Shell Environment
– Startup and logout files
– Shell variables
– History
– Command substitution
– Command sequences
– Aliases
– Directory Stacks
– Redirections and pipe
• C Shell Programming
198
Password File
ux% nismatch z036473 passwd.org_dirz036473:e6qhWRw.YeK0E:1432:202:rush kaisone:/home/mp/z036473:/bin/csh:12068::
User login id Encrypted
passworduid
gid
User
name
Home directory
Login shell
Number of days between 1/1/1970 and the last
day that the user changed their password.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-100
199
C Shell Startup
init
getty
login
csh
Login:
Login: z036473
Passwd:
Login: z036473
Passwd:
%
200
NIS Password File
• Network Information Service password file named
“passwd.org_dir” contains user account
information.
• The command named “niscat” displays the entire
contents of the “passwd.org_dir” file.
• The command named “nismatch” displays account
information for a specified user.
Examples: Display contents of the password file.
ux% niscat passwd.org_dir
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-101
201
Login At NIU
Default shell: /bin/csh
Can be customized via startup scripts
1) /etc/.login (system shell)
2) $HOME/.cshrc (if C shell is the login shell)
3) $HOME/.login
202
Startup File: $HOME/.cshrc
• Executed once:
– at login and
– each time a C shell script is executed
(unless #!/bin/csh –f was used)
• Located in your $HOME directory
• Contains settings such as:
– Command search path
– Initial permission setting (umask)
– Terminal setting
– Mail box, etc.
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-102
203
Useful .cshrc content
• Define command aliasesalias ll “ls –al”
alias ap “man –k”
• customize common behavior:– set filec
when typing a file/directory name, if you press the esc key, it will complete the rest of the name for you.
– set ignoreeof
prevents accidental logout if you type ctrl-d at the command line.
– set noclobber
prevents overwritten of files by output direction.
204
Startup File: $HOME/.login
• Executed once only during login process
• Located in your $HOME directory
• Contains settings such as:
– Terminal setting:
special keyboard character setting
stty erase ‘^h’
• Mail directory
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-103
205
Logout File: $HOME/.logout
• Executed once when you log out
• Located in your $HOME directory
• useful content:
ps –f –u $user
displays the user’s active processes
206
C Shell Prompts
• C shell has two prompts:
– Primary prompt: %
– Secondary prompt: ?
• Primary prompt (%)
– Can be reset
– Use the command named “set prompt” to customize
your command prompt.
Example: ux% set prompt = “$USER > “
z036473 >New prompt
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-104
207
C Shell Prompts
• Secondary prompt
– Appears when writing on-line script at the command
prompt
% foreach student (z036473 z036474)
? mail $student < memo
? ^d (type ctrl-d or type end)
%
– Once the command is entered, and [Return] is pressed,
you cannot go back to previous lines.
– C shell history does not save commands typed at the
secondary prompt.
208
Variables
A shell variable is a location in memory where
values can be stored
Variables
Pre-defined
Shell Environment
User-defined
String Arithmetic
Special
Built-in Pathname
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-105
209
Pre-defined Variables
• Two types:
– Pre-defined shell variables
– Pre-defined environment variables
• Pre-defined shell variables are used to configure the shell.
– Example: history – which contains the size of the history list.
• Pre-defined environmental variables are used to configure
the shell environment.
– Example: HOME – which contains the full pathname of your
home directory.
210
Some Predefined Shell Variables
Name Contents
cwd The current working directory
history The size of the history list
ignoreeof Prevents the shell from terminating when pressing
Control-D. Use the “logout” or “exit” command.
noclobber Prevents existing files from being overridden by output
redirection (>), and non-existent files from being
appended by append (>>)
prompt The shell primary prompt
savehist The number of commands to save in the history file
“$HOME/.history”
status The exit code of the last command
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-106
211
Predefined Environment Variables
Name Meaning
HOME The full pathname of your home directory
PATH A List of directories to search for commands
MAIL The full pathname of your mailbox
USER Your user id
SHELL The full pathname of your login shell
TERM The type of your terminal
212
Command Search Path
setenv PATH /usr/local/bin:/usr/bin:/usr/ucb:$home/bin:
bin
bin
.logout
usretcdev home
uxmptty null skel bin local ucb
z036473
.cshrc .logincsci330
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-107
213
Creating Environment Variables
• Create C shell environment variable and initialize it to a
value
• If varname does not exist, it is created; otherwise, it is
overwritten
• An Environment variable always holds exactly one value
• Example:
% setenv TERM vt100
% echo $TERM
vt100
% setenv EDITOR pico
Syntax: setenv varname value
214
User-Defined Variables
• Created by the user• Syntax: set varname=string
• Create a shell variable and initialize it to a string of characters
• varname:– 1-20 characters long
– Letters, digits, and underscore
– First character cannot be a digit
– Should not be the same as one of the pre-defined variables
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-108
215
Storing Values in Variables
Command Result Comment
set x = 123 x contains “123” x contains a number, but it is
stored as string of digits
set x = Hello x contains “Hello” Storing a character string
set name = Jane Black x contains “Jane” Only “Jane” is stored
set name = “Jane Black” x contains
“Jane Black”
When a string contains spaces
it must be quoted
set x = “Go Dons!” x contains
“Go Dons!”
When a string contains spaces
and special characters, it must
be quoted
216
Accessing the Values of a Variable
• The name of the variable must be preceded by a dollar sign
• Variable values can be used anywhere in a string
• Examples:
% set count = 7
% echo $count is greater than 6 and less than 8
7 is greater than 6 and less than 8
% echo The value of count is $count as expected
The value of count is 7 as expected
% echo My lucky number is $count
My lucky number is 7
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-109
217
Accessing the Values of a Variable
• Variable values can also be stored in another
variable
• Example:
% set count = 5
% set number = $count
% echo count contains: $count
count contains: 5
% echo number contains: $number
number contains: 5
218
Unsetting a Variables
• We can clear a variable by assigning a null value to it - examples: set z = “ “; set y =
• Another method is to use the “unset” command
• Example:% set x = 1
% echo "(x contains:" $x")"
(x contains: 1)
% unset x
% echo "(x contains:" $x")"
x: Undefined variable
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-110
219
Command Substitution
• A command surrounded by backticks (` `) is
replaced by its standard output
• Any newline in the output are replaced by spaces
Demo
220
Command Sequence (;)
• Allows you to enter a series of commands all at once
• Commands are separated by a semicolon (;)
• Useful for typing an entire sequence of commands at once
Example: To execute these commands in sequence: date, pwd, and ls
% date;pwd;ls
Mon Jul 2 11:07:10 CDT 2003
/home/ux/krush/330/Summer-03/Quizzes
message.rickert quiz1-sum03 quiz2.sum03
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-111
221
C Shell History
• C shell keeps a record of previously entered cmds
so that they can later be:
– Re-executed
– Edited
• Entered cmds are stored in the current C shell’s
history buffer ($HOME/.history)
• Commands are saved
– Per session
– Per user
222
C Shell History
• Each previously run command gets a sequential
event number
• To view the history buffer:
– Syntax: history [-rh] [count]
• If no options are supplied, list all
– Useful options:
-r displays history list in reverse order
-h inhibits the display of event numbers
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-112
223
C Shell History
• You can re-execute history events:
1) By the event number
% !5
1) By the number relative to current event
% !-3
1) By the text it contains
% !ls
224
C Shell History
How big a list ?
1) On the command line, type:
% set history = 20 (per session only)
2) Include in your $HOME/.logout file:
set savehist = 30
At logout, C shell saves the last 30 cmds you executed in
the $HOME/.history file
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-113
225
C Shell History
Two ways to display event number as part of your cmd prompt:
1) On the command line, type:% set prompt = “\! %” (per session only)
1 % echo csci 330
csci 330
2 % _
2) Include in your $HOME/.cshrc file:set prompt = “\! %” (per each login)
226
C Shell History
For more listing of history commands, see Course Notes pp. 13-7
Command Action
!! Re-execute the last command
!N Invoke event N (integer)
!str Invoke most recent event beginning with str (string)
!?str? Invoke most recent event containing str
^str1^str2 Substitute str2 for str1 in the previous cmd
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-114
227
C Shell History: Words in Event
• Can use any word from any event on the history list
• C shell numbers the words in each command starting at 0
• Example:grep ‘^[0-9]\{5\}’ | sort –t: | cut –d: -f1
Word: 0 1 2 3 4 5 6 7 8
• Can easily identify “words” since space separate them
228
C Shell History: Words in Event
• Can refer to individual words with :n ( n is the word number)
• Example:100 % ls –l assign1 assign2 assign3
101 % chmod a-w !ls:2-$
chmod a-w assign1 assign2 assign3
102 % ls –l !ls:$
ls -l assign3
-r-------- 1 krush csci 0 Feb 14 12:35 assign3
• The modifier * refers to words 1 through last (I.e. everything except the command name)103 % rm !100:2*
rm assign1 assign2 assign3
$ refers to the last
word
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-115
229
C Shell History
Demo
230
Command Substitution
• A command surrounded by backticks (` `) is
replaced by its standard output
• Any newline in the output are replaced by spaces
Demo
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-116
231
C Shell Aliases
• Important feature of C shell
• Allows you to assign a name, or alias to
command(s)
• Can use alias like any other command
• Can rename existing commands
• Can reference shell variables
• Aliases defined at the parent shell’s command line
are not inherited by subshells
232
C Shell Aliases
• C shell user-defined abbreviation for a command
• Useful if a command syntax is difficult to
remember
• Define aliases at the command line or in
“$HOME/.cshrc” file
• Alternative: create a file name “.myalias” in your
$HOME and include all alias definitions.
– Include this line in your “$HOME/.cshrc” file:
source $HOME/.myalias
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-117
233
Creating Aliases
Use the command: alias
Syntax: alias name ‘command-list’
User-defined
nickname for the
command
•The command(s) that will be
executed when the alias is invoked
•Multiple commands are separated
by a semicolon
•Metacharacters are surrounded by
single quotes
234
Passing Arguments into Aliases
• C shell assumes all arguments come at the end of
the alias definition, unless you specified otherwise
• Two common argument destinations:
!* words 1 through the last word of the current
command/event
!^ word 1 of the current command/event
• Recall:
grep ‘^[0-9]\{5\}’ demand | sort –t: | cut –d: -f1
Word: 0 1 2 3 4 5 6 7 8 9
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-118
235
Example: Creating an Alias
% alias cd ‘cd \!*; set prompt = “$cwd >”’
Note:
• \ prevents the history mechanism from evaluating the !*
• !* represents the argument from the most recent current
command in the history list (i.e. words/fields 1 through the
last word of the recent command)
• \!* allows you to pass any number of arguments to the
alias
236
Issue a Command to the C Shell
• What happens when you issue a command to the C shell?
• After history substitution, examine each word that could be a command in the following sequential order:
1. Alias Substitution
2. Built-in command (commands inside the C shell; e.g. echo)
3. Commands in your search path ($PATH)
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-119
237
In-Class Exercise: aliases
1. Write an alias named “h” to list the history of
previously executed commands.
2. Write an alias named “c” to clear the contents of the
screen.
3. Write an alias named “rm” to prompt the user for a
delete confirmation message before removing file(s).
alias h history
alias c clear
alias rm ‘rm –i \!*’
238
In-Class Exercise: aliases
4. Write an alias named “cx” to give the owner the execute permission on more or more file/directory.
5. Write an alias named “loc” to locate/find any file in your directory hierarchy.
6. Write an alias named “ldir” to display long listing of only directory names (i.e. not ordinary files).
alias cx ‘chmod u+x \!*’
alias loc ‘find $home –name “\!:1” –print’
alias ldir ‘ls –l \!* | grep “^d”’
alias fdir ‘find \!:1 –type d –print’
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-120
239
Output
• The output statement of the C shell is the “echo” command
• Syntax: echo [option] arg1 arg2 …argN
• Its arguments can be strings or variables
• Example:
% set time = “2:00pm.”
% echo "It is now $time."
It is now 2:00pm.
% echo It is now $time.
It is now 2:00pm.
% echo 'It is now $time.'
It is now $time.
The variable cannot be
embedded in single
quotes.
240
Output Redirection (>)
• Syntax: command > file
• Sends output of command to file, instead of to
terminal
• Examples:
% du > status
% (date; du) > status
Calls the disk usage command for the
current directory and redirects the
output to a file called ‘status’
( ) indicates command groups. Use it
to combine the output of multiple
commands. In this example, we place
time and date in front of the disk
usage
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-121
241
Examples: Output Redirection (>)
% date; du > usage-status
Output of ‘date’ goes to? __________________
Output of ‘du’ goes to?___________________
% date > usage-status; du > usage-status
The file “usage-status” contains ?
___________________________________
242
Input Redirection (<)
• Syntax: Command < file
• Command will read (take input) from file, instead
of from terminal
• Example:
tr “[A-Z]” “[a-z]” < report.input
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-122
243
Examples: Output / Input
• Redirecting input and output:
% tr “[A-Z]” “[a-z]” < report.input > report.output
• Output of one command becomes input of next:
% ls > temp.txt; wc < temp.txt
• Eliminate the middleman: pipe
% ls | wc
244
Appending Output
• Syntax: command >> file
• Places the stdout of command (or command group) at the end of file
• If file does not exist, C shell creates it given that the variable “noclobber” is not set
• Examples:% date >> usage-status
% ls –l >> usage-status
% du –s >> usage-status
% date > usage-status
Build the file ‘usage-status’ from
the output of the ‘date’, ‘ls’, and
‘du’ commands
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-123
245
Using “set noclobber”
% who > current-users
% date >> usage-status
% set noclobber
– Does not allow output redirection if the file already exist
– Does not allow appending if the file does not already exist
% who > current-users
current-users: File exists
% date >> usage-statis
statis: No such file or directory
Specified incorrect
filename!
246
Override the Extra Protection
• Add ! To the redirection
• ! Means “do what I mean!”
• Example:
% set noclobber
% who >! current-users
Allows redirection even if ‘current-users’ exists
% date >>! usage-statis
Creates the file named “usage-statis” even if it does not already
exist
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-124
247
Handling Standard Errors
• Sometimes a command has special output to
inform you of problems; for example:
% gcc gets.c > compile.out
“gets.c”:8: syntax error
• Syntax: command >& file
– Redirect stdout and stderr to file
• Syntax: command >>& file
– Append stdout and stderr to file
248
Examples: Redirect/Append Stderr
% gcc gets.c >& compile.out
% cat compile.out
“gets.c”:8: syntax error
% (date; gcc gets.c –o gets) >>& compile-log
Add to the file ‘compile-log’ a running record with the
date and time of the compilation of the file gets.c
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-125
249
Separating Stdout and Stderr
% cat calendar logfile >& save– Saves both stdout and stderr in file ‘save’
% cat save
July 11 Planning Meeting
July 18Budget Meeting
Cat: can’t open logfile
% (cat calendar logfile > save) >& errfile– Redirects stdout to ‘save’
– Redirects stderr to ‘errfile’
– Order is important, > goes inside ( ), and >& goes outside
stdout
stderr
250
Sending Stdout and Stderr Through a Pipe
• Syntax: command1 |& command2
Sends stdout and stderr of command1 to be stdin to
command2
• Example:
% gcc gets.c |& more
– Sends stdout and stderr to ‘more’ for convenient
viewing
% gcc gets.c |& lpr –Plpcsl
– Produces a hardcopy of the messages
Introduction to UNIX
Copyright Department of Computer Science,
Northern Illinois University, 2004 01-126
251
Summary: Redirections and Pipe
Command Syntax Meaning
command < file Redirect input from file to command
command > file Redirect output from command to file
command >& file Redirect output and errors to file
command >> file Redirect output of command and appends
it to file
command1 | command2 Take/pipe output of command1 as input
to command2
command1 |& command2 Take/pipe output and errors of
command1 as input to command2