Input/Output redirection with the Utilities Practice



CIS 118 Intro to LINUX Class Exercise

Week 4

 

Standard I/O and redirection

1. Standard I/O:

o STDIN (keyboard) filehandle 0

o STDOUT (display) filehandle 1

o STDERR (display) filehandle 2

2. Redirecting output. When you redirect standard output, the data that would normally appear on the screen is stored in a file instead. Redirect output to a file using > or >>

o ls -l > flist 

stores the output from ls in flist; if flist already exists, it is overwritten

o ls -l >> flist

appends the output from ls to the file flist; if flist does not exist, it is created

3. Redirecting input. When you redirect standard input, any data that would normally be entered from the keyboard is obtained from a file instead. Redirect input from a file using < or |

o mail rjtaylor@oakton.edu < jokes

o cat jokes | rjtaylor@oakton.edu

4. Redirecting standard error. Redirect standard error using 2>

o find / -name jokes –print 2> /dev/null 

looks for a file named jokes; sends any "permission denied" messages to /dev/null

Pipes and Filters

5. Pipes - Takes the output from one command and uses it as input to the next command.  

o ls  -l  /etc  |   more

o  

6. The second command in a pipe is a filter used modify the output from other commands to answer a question. Common filters: grep, sort, wc, uniq

 

7. wc: Count bytes, words and lines

o wc /etc/passwd Count the number of bytes, words and lines in /etc/passwd

o wc -l /etc/passwd Count the number of lines in /etc/passwd

o wc -w /etc/login.defs Count the number of words in /etc/passwd

 

8. sort: sort lines in a file

o sort /etc/group Sort the contents of /etc/group

o sort  -r  /etc/group Sort the contents of /etc/group in reverse order

o ls -l | sort -k5n Sort ls -l on the size of the files. starting on field 5 in numerical order

 

9. uniq: remove consecutive duplicate lines

10. grep: search for text within a file.

Exercises:

1. Find all jokes in the system

find / -name jokes –print 2> /dev/null 

2. Place a directory listing

ls -l /etc > ~/etc.filelist

3. Capture who is logged on

who > ~/whoson

4. Record all root processes:

ps –ef | grep root > root.proc

5. Get a list of everyone currently logged on and save the list in a file called "users "in your own home directory.

 

6. Get a list of all of the processes that are running and add this list to the end of the "users" file.

7. Typewriter

cat –

This is a test

This is a test

Ctrl D

8. Copy the below file into ~/professions

grep for all Engineers

grep for all non-Teachers

grep for all Teachers

John Smith Teacher 301-989-9283 25000

Pete Jones Engineer 301-854-2938 65000

Fred McDuff Teacher 301-993-2938 35000

Annie Tubs Teacher 301-039-9283 37000

Jeff Tolaho Engineer 301-394-0293 45000

Finlay Toms Engineer 301-392-2039 32980

Tom MacMark Teacher 301-029-2039 39482

Carl Banks Teacher 301-392-0392 39202

Warren Tu Teacher 301-039-9329 89000

Mark Zulona Teacher 220-029-1029 59343

Mike Thoman Engineer 220-923-9283 48578

Joni Tobs Engineer 220-039-9283 85968

Jean Tobb Teacher 220-203-2039 78293

Jeff Thoman Engineer 301-033-2039 38493

Jonathan Wu Teacher 220-153-2930 29102

Jenny Watasa Teacher 220-309-1029 47584

Jill Tom Teacher 220-394-2938 49302

9. Count the number of files in the directory var/spool/mail?

  

ls /var/spool/mail | wc -w

 

10. The number of users whose login shell is /bin/bash?

  

grep /bin/bash$ /etc/passwd | wc -l

 

11. What command will display a list of everyone who is logged on sorted by their usernames?

  

12. more /etc/services This file contains a list of the port numbers used for IP network services.

 

13. How many lines are in the file.

 wc –l /etc/services

14. Display all of the lines for tcp services.

grep tcp /etc/services

Count the number of lines for tcp services.

grep tcp /etc/services | wc -l 

15. Display all of the lines for udp services.

grep tcp /etc/services

Count the number of lines for udp services.

grep tcp /etc/services | wc -l 

 

16. Use the "last" command to display the record of all login sessions.

What command counts the number of login sessions for floopy?

 

17. Display a long directory listing for the /etc directory.

Pipe the output of the long directory listing through a grep command that will only display the entries for directories in /etc.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download