1 uli101 introduction to unix/linux and the internet more linux commands seneca college of applied...
TRANSCRIPT
1
ULI101
Introduction to UNIX/Linux and the Internet
More Linux Commands
Seneca College of Applied Technology
1
File related commands
grep - print lines matching a pattern
head - output the first part of files
tail - output the last part of files
sort - sort lines of text files
diff - find differences between two files
file - determine file type
1
Utility commands● who – show who is logged in ● date – print or set the system date and time● which – show the full path of (shell) commands● quota – display disk usage and limits● finger – user information lookup program● mail – send and receive mail
1
Print commands
● lpr – print files ● lpq – show print queue status● lprm – cancel print jobs
1
grep
Print lines matching a pattern
grep takes a pattern, read standard input or a list of files, and outputs the lines containing matches for the pattern.
Example:
grep foo *
Print lines in any of the files in the current directory that contain the pattern “foo”.
1
grep examples● grep -r foo .
Print all the lines in all the files in the current directory and all its subdirectories that contains the pattern “foo”.
● grep -lr foo .
Similar as above but only print the names of the files that contains the pattern “foo”
1
Contents of the sample file[uli@seneca misc]$ cat barThe name of this file is called bar.This file has only five line.This line contains the word foo and bar.Do you like to play football or basket ball?This is the end of the file.
[uli@seneca misc]$ nl bar 1 The name of this file is called bar. 2 This file has only five line. 3 This line contains the word foo and bar. 4 Do you like to play football or basket ball? 5 This is the end of the file.
1
More grep examples
Print all the lines in the file “bar” that contains the pattern “foo”[uli@seneca misc]$ grep foo barThis line contains the word foo and bar.Do you like to play football or basket ball?
Same as above but prefix each line of output with the line number within the file “bar”
[uli@seneca misc]$ grep -n foo bar 3:This line contains the word foo and bar. 4:Do you like to play football or basket ball?
1
More grep examples
Print all the lines in the file “bar” that contains the word “foo”[uli@seneca misc]$ grep -w foo barThis line contains the word foo and bar.
Print all the lines in the file “bar” that does not contain the pattern “foo”
[uli@seneca misc]$ grep -v foo bar The name of this file is called bar. This file has only five line. This is the end of the file.
1
grep options
Major options for grep– “-l” display name of the file that has matching line– “-r” search all the files in the current directory and all
its subdirectory for the given pattern– “-n” prefix each output with line number – “-w” search for matching word– “-v” output lines that do not contain the given pattern
1
head & tail● head bar
– Display the first 10 line of the file “bar”● head -5 bar
– Display the first 5 lines of the file “bar”● tail bar
– Display the last 10 lines of the file “bar”● tail -5 bar
– Display the last 5 lines of the file “bar”
1
sort
Sort line of text file[root] cat numbers
2314 5678345 2231101 9844842 654398 11001
[root] sort numbers101 9842314 5678345 22314842 654398 11001
[root] sort -n numbers98 11001101 984345 22312314 56784842 6543
Numeric order
Stringorder
1
More sort examples[root] cat numbers
2314 5678345 2231101 9844842 654398 11001
[root] sort numbers101 9842314 5678345 22314842 654398 11001
[root] sort -k2 numbers98 11001345 22312314 56784842 6543101 984
Sort by the 1st field
Sort bythe 2nd field
1
More sort examples[root] cat numbers
2314 5678345 2231101 9844842 654398 11001
[root] sort numbers101 9842314 5678345 22314842 654398 11001
[root] sort -r numbers98 110014842 6543345 22312314 5678101 984
Sort inreverseorder
1
diff
Display the differences between two files
Syntax:● diff [options] file1 file2
useful options:
“-y” or “--side-by-side” use the side by side output format
“-W n” or “--width=n” use an output width of columns n in side by side format
1
diff default output● When using “diff” without any options, it
produces a series of lines containing– Add (a)– Delete (d), and– Change (c) instructions
Each of these lines is followed by the lines from the file that you need to add, delete, or change to make the files the same.
1
diff examples$cat file1blueredwhiteyelloworange
$cat file2blueyellowblackredorange
$diff file1 file22,3d1< red< white4a3,4> black> red
1. Delete line 2 through 3 from file1
2. Append lines 3 through 4 from file2 after line 4 in file1
Steps to convert file1 to file2:
1
diff examples$cat file1blueredwhiteyelloworange
$cat file2blueyellowblackredorange
$diff -y -W 30 file1 file2blue bluered <white <yellow yellow > black > redorange orange
1
diff examples$cat file1blueredwhiteyelloworange
$cat file2blueyellowblackredorange
$diff --side-by-side --width=30 file1 file2
1
file
Display the classification of a file● Syntax: file [option] file-list
option: -L
reports on the files that symbolic links point to
option: -f file
reads the names of the files to be examined from file
option: -i
Causes the file command to output mime type strings
1
file examples[uli@seneca notes]$ file *introunix.html: exported SGML document textlinks.sxi: Zip archive data, at least v2.0 to extractmisc-commands.sxi: Zip archive data, at least v2.0 to extracttemplate.sxi: Zip archive data, at least v2.0 to extractULI01LabLing02.ppt: Microsoft Office DocumentULI01LabLing-fast.ppt: Microsoft Office Document
[ray@localhost notes]$ file -i *introunix.html: text/html; charset=iso-8859-1links.sxi: application/x-zipmisc-commands.sxi: application/x-ziptemplate.sxi: application/x-zipULI01LabLing02.ppt: application/mswordULI01LabLing-fast.ppt: application/msword
1
More file examples[ray@localhost week8]$ ls -l mydir
lrwxrwxrwx 1 ray ray 7 Oct 29 15:41 mydir -> courses
[ray@localhost week8]$ file mydir
mydir: symbolic link to courses
[ray@localhost week8]$ file -L mydir
mydir: directory
1
Recap
grep
head
tail
sort
diff
file
1
who
Show who is logged on Phobos: /home/rchan>$ whorchan pts/0 Oct 30 02:08 (toronto-hse-ppp3)sslui pts/1 Oct 30 01:11 (CPE00112f0fe590-)Phobos: /home/rchan>$ who -HName Line Time Hostnamerchan pts/0 Oct 30 02:08 (toronto-hse-ppp3)sslui pts/1 Oct 30 01:11 (CPE00112f0fe590-)Phobos: /home/rchan>$ who -qHName Hostnamerchan (toronto-hse-ppp3)sslui (CPE00112f0fe590-)Total users: 2
1
date
Display the system time and date[ray@localhost week8]$ dateSun Oct 30 01:48:10 EST 2005[ray@localhost week8]$ date +"%D"10/30/05[ray@localhost week8]$ date +"%T"01:54:05[ray@localhost week8]$ date +"%D %T"10/30/05 01:54:13
Refer to the man page for more formatting codes
1
which
Shows the full path of (shell) commands[ray@localhost week8]$ which mkdir
/bin/mkdir
[ray@localhost week8]$ which type
/usr/bin/which: no type in (/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/ray/bin)
1
quota
Display disk usage and limitsPhobos: /home/rchan>$ quotaDisk quotas for user rchan (uid 1628): Filesystem blocks quota limit grace files quota limit grace /home_sharedfs 8296 10000 12001 97 10000 12001/public_sharedfs 4 5000 5001 2 5000 5001 /mail_sharedfs 4 5000 5001 1 5000 5001
1
finger
User information lookup programPhobos: /home/rchan>$ finger rchanLogin name: rchan In real life: Raymond ChanDirectory: /home/rchan Shell: /usr/bin/kshOn since Oct 30 02:08:55 on pts/0 from toronto-hse-ppp3 (messages off)No Plan.
1
Send and receive mail
To read your mail on phobos, type the “mail” command by itself:
Phobos: /home/rchan>$ mail
Mail [5.2 UCB] [AIX 4.1] Type ? for help.
"/var/spool/mail/rchan": 1 message 1 new
>N 1 rchan Wed Oct 26 00:24 10/340 "Mail testing"
1
Sending mail
To send a file called “letter” through email on phobos to the user “rchan”:
Phobos: /home/rchan>$ mail -s “subject” rchan < letter
1
Recap
who
date
which
quota
finger
1
lpr, lpq, lprm● lpr – submit file for printing
lpr [ -P printer-name ] [ -# copies ] file-name
[ -P printer-name] : send files to the named printer
[ -# copies ] :sets the number of copies to print
between 1 and 100
file-name : name of file to be printed
1
Show printer queue status● lpq - show printer queue status
lpq [ -P printer-name] [ -a ] [ -l]
[ -P printer-name] : show status on the named printer
[ -a ] : reports jobs on all printers
[ -l ] : display more verbose (long) format
1
Cancel Print Jobs● lprm – cancel print jobs
lprm [ - ] [ -P printer-name] [ job ID(s)]
[ - ] : all print jobs
[ -P printer-name] : print jobs on the named printer
[ job ID(s) ]: jobs to be cancel
1
Recap
grep who lpr head date lpq tail which lprm sort quota diff finger
1
Reference● Man Pages● A practical Guide to Linux by Mark G. Sobell
1
Questions and Answers● How do you print all the lines in all the files in
the current directory and all its subdirectories that contains the <title> tag?
● How do you print all the lines in the file called “index.html” that contains the word “seneca”?
● How do you print the last line of the file called “task.html”?
● How do you print the classification of each file in the currently directory?
1
Questions and Answers● What does the command “who” do?● What does the command “who -q” do?● How do you find out your disk usage and limits
on phobos? ● How do you read your mail on Phobos?● How do you cancel a print job?