CS114: Homework 3

Do the problems below on one of the CSUG Linux machines, i.e. csug01 through csug06.csuglab.cornell.edu. Email me your solutions (scripts) as separate files, named appropriately, AND turn in a hardcopy of your answers to both problems with your full name and netID, in class or in my mailbox (657 Rhodes) by 5 p.m. on the due date given on the main course page.

Problem 1: How big is my music collection? (musiclib.sh)

Suppose you have a music collection made up of mp3, mp4, and Ogg Vorbis files (shame on you if you use Windows Media Player format!). These files end with .mp3, .mp4, or .ogg extensions. Write a script that counts the number of files of each type that you have in the current directory and the total amount of disk space (kilobytes) the files of each type use, the total number and disk space usage of all files, and then outputs that information to stdout. Example output would be:

You have 10 music files that use 47513K of disk space:
* 3 Ogg Vorbis files (12553K)
* 7 mp3 files (34960K)
* 0 mp4 files (0K)

Your script should take one option, -R. If this option is passed to the script, then it should search subdirectories recursively (starting from the one from which the script was invoked), tallying up the information as above. Your script should check that it is given ONLY one option, and that it is the correct one. If it is given more than one option, or an incorrect option, then it should print an error message and exit with status 1. You may find the commands du and expr useful.

Problem 2: Backing up (backup.sh)

Write a script that takes up to 4 arguments: flag, num, basename, and target in that order. The flag argument is optional, and must be -c if given. The script should search for files that are older than num days, starting from the directory given by basename. Any such files that are found should be moved to a directory called target in the user's home directory. Once all of old files have been moved, the target directory should be renamed target.date (where date is the current date in yyyy.mm.dd format), archived using the tar command and gzipped with gzip. If the flag is given as -c, then the files should be copied rather than moved. Your script should check for the following:

Regardless of whether the script exits with normal or error status, it should clean up after itself by removing the target directory and its contents (but not the zipped archive!).