COP 3344-01 Introduction to Unix: Lecture 4
Learning objectives
After this class, you should be able to:
- Use the following Unix commands:
cmp
,cut
,diff
,gunzip
,gzip
,head
,sort
,tail
,tar
, andwc
.- Compare two (possibly binary) files to determine if they are different using
cmp
.- Find out how two files differ using
diff
.- Extract some text from each line of a file using
cut
.- Read the first few lines or last few lines of a file using
head
ortail
respectively.- Sort a file using
sort
.- Count the number of words and lines in a file using
wc
.- Archive files using
tar
, and extract files from the archive.- Compress and uncompress files using
gz
.- Redirect standard input, standard output (including appending to a file), and standard error.
- Combine commands using pipes.
- Group commands using
'('
,')'
, and';'
.
Reading assignment
- JEA: Chapters 10-11, 23.9-23.14, Single Unix Specification for definitions of the commands.
- Lecture slides: Lecture4.
Exercises and review questions
- Questions on current lecture's material
- Create a file called
PracticeFile
which contains three columns of text, separated by tabs.- Output the first few lines and the last few lines of the file into another file, called
file1
, using features we have learned, such as I/O redirection. Next, output the contents offile1
.- Output only the second and third columns of the file
PracticeFile
using features we have learned.- Output lines
4
to8
of the filePracticeFile
using pipes and commands we have learned (you may want to check documentation of some options tohead
andtail
).- Sort the contents of
PracticeFile
and output into a file calledSortedPracticeFile
.- Check if the number of lines in
PracticeFile
andSortedPracticeFile
are identical, using a command that we have learned.- Check if the files
PracticeFile
andSortedPracticeFile
are identical, usingcmp
.- Modify the file
PracticeFile
by adding a few lines, changing a few lines, and deleting a few lines, and store it in a file calledModifiedPracticeFile
. Usediff
to find all the differences between the two files.- Create a compressed archive of all the files you have created in this exercise, using
tar
andgzip
. Move the compressed archive to a different directory, and extract the files. Check if you got the same files back. Check some of the files, against the originals, to ensure that the contents of the files have not changed.- Questions on next lecture's material
- Write a simple shell script called
info
, which gives the date using thedate
command, and prints the current working directory.