Chapter 3 - UNIX Utilities for Power Users

Quiz

1. Under what circumstances would you archive files using tar?
The tar command is especially useful when creating large archive files of directories (sometimes referred to as tarballs). Large single files like this can be more easily emailed or ftpd to other systems without having to send every file individually. While tar can be used to write the files to a tape device, many people prefer the cpio command for this as it provides more flexibility and options unique to tape devices.
2. How would you convert the contents of a file to uppercase?
Use the tr command to convert the contents of a file to upper case. The command would look like this:
tr [a-z] [A-Z] <input_file >output_file
3. What is the difference between cmp and diff?
The cmp command performs a byte-for-byte comparison of two files, and stops when a difference is found, reporting only that the files are not identical. The diff command compares two text files and reports all the differences between the two files.
4. Describe what it means to mount a file system.
To mount a file system is to connect, or splice, a separate file system, most likely on a different device than the main file system, into the hierarchy of the file system using the mount command.
5. Which process serves the crontab system?
The cron daemon (usually found in /etc or /usr/etc) is responsible for running crontab jobs.
6. What additional functionality does an extended regular expression have?
Additional functionality found in extended regular expressions include finding one or more occurrences of the preceding character (+), finding zero or one occurrences of the preceding character (?), the ability to specify more than a single character for a character group by enclosing the group in parentheses and then following it by ?, +, or *, and a logical OR ( | ) between two regular expressions.
7. What are the main differences between sed and awk?
The sed command is designed to edit lines of an input stream. The awk command is used to select lines out of an input stream and do some kind of processing on those lines. There is overlap between these commands, either one can do many functions, but one usually lends itself better to your solution. The sed command works best when you merely want to make a frequent modification to many lines in the input. On the other hand, awk works best if you will be rearranging the order of the input lines or the information on the lines (e.g. swapping columns).
8. How did awk get its name?
The awk command is named by creating a word from the first letter of the last names of its authors Aho, Weinberger, and Kernighan.
9. Under what circumstances would you use a symbolic link instead of a hard link?
You must use a symbolic link to link files that are on different devices (an inode cannot point to disk blocks on a different device). If the files are on the same device, you still might want to use a symbolic link rather than a hard link in some cases. A symbolic link can exist even if the file it points to does not. A symbolic link file might be used as a place holder for a file when the actual data file doesnt exist (in which case, a hard link would cause the data to still exist on the disk).
10. What are the drawbacks of using a symbolic link?
If you move (rename) the file to which a symbolic link points, the link will not be updated, it will still point to the old location (which will cause a "file not found" error when used).
11. What is meant by an incremental backup, and how would you perform one?
An incremental backup is one where only files that have been created or modified since the last backup was performed are backed up. In most cases, this is much smaller and much faster than doing a backup of all files on the system. You can use the dump or ufsdump to do an incremental backup. If you dont mind doing some of the work manually, you can also use find and cpio to do an incremental backup (by creating a bookmark file for each backup and using the -newer argument of the find command to get files modified since the bookmark file was created or modified).
12. What are some ways that Perl makes script programming easier to write than conventional shell scripts?
Mathematical operations and file input and output are easier in Perl than in most shell scripting languages. Perl is also more tightly typed than shell languages (you cannot mistakenly perform character operations on an integer variable).

Exercises

3.1 Perform some timing tests on grep and fgrep to determine the advantage of using fgreps speed. [level: easy]
This is merely an exercise. Since fgrep doesnt honor regular expressions, it can do its searching faster.
3.2 Ask the system administrator to demonstrate the use of tar to produce a backup tape of your files. [level: easy]
If you have a kind system administrator who isnt buried in things to do (which is rare) they should be able to pop a tape into a drive and type a single tar command to create an archive of a home directory. You will need to become the superuser in order to do this.
3.3 Use crontab to schedule a script that removes your old core files at the start of each day.  [level: medium]
Such a script uses find with the -name argument to find all files named core with -exec to remove them. When setting up the crontab file, use * for all specifications except hours and minutes so that it runs every day at the specified time.

Projects

1. Write a command pipeline that compresses the contents of a file and then encrypts it, using a known key, and writes the result to a new file.  What is the corresponding command pipeline to take this encrypted file and decrypt and uncompress it to produce the original file? [level: easy]
gzip <file | crypt key >file.encrypted
crypt key <file.encrypted | gunzip >file.new
2. Write a command pipeline to find files in a directory hierarchy (e. g. your home directory) that have not been accessed for 30 days and compress them. [level: medium]
find ~ -type f -atime +30 -exec gzip {} \;
or perhaps
gzip `find ~ -type f -atime +30`
3. Modify the loan Perl script so that you can pass a list of payments to it rather than using the same payment amount every month. [level: medium]
Read the list of payments into an array and then index through that to obtain each payment amount each month.