Last update: 2/1/2017 -


Exercise #2 (we'll work through this together in class)

The objective of this exercise includes the following:

 

Step 1 develop ex2.sh

 

Step 2 create a number of linked files

 

Step 3 pipes, the script/scriptreplay commands

 

Step 4 Shell pipelines, regex, and filters - using find, xargs : example script design process

An illustrative Bash question that will drive this example: find and count all files that have been modified within a specified amount of time. Step 5 articulates a more detailed script example (and points to several solutions). For this step, we want to use this example to introduce the find and xargs commands and to provide a very real pipeline example.

As in true with any programming task in any environment or language, there will be many design options and directions. The suggestion I give to students learning Bash - try to learn a small (but comprehensive) set of basics and then try to stick to designs that come from your 'working set' of knowledge. As you get more experience, your working set (i.e., expertise) will grow quickly.

In the problem statement, we see some keywords that potentially provide helpful guidance: ' find' , 'count', 'modified within a specific amount of time' . Notice that the problem did not specify the type of file - so we should assume all ordinary and special files are of interest.

Choices to obtain recursive lists of files-

 

Another possibility is the du command -

 

Step 5 This is meant to be a practice Bash script. This was a part of a homework assignment given to CPSC4240/6240 in the past. Two solutions have been posted.

Create a bash script that identifies all files that have been modified within a specified amount of time.

HINT: the find command with the –mtime option will provide the data, your script should then process and present the data as required.

Syntax: filesUpdated.sh param1 param2 param3

Param1: the starting directory. Your script should start here and step into all subdirectories

Param2: amount of time that is of interest. We will define this to be the mtime parameter in find (n*24 hours). So the granularity of the time of interest is in units of days.

Param3: script mode. The output of the script depends on the setting of this parameter:

Value ‘1’ : summary mode: indicate how many files per user (i.e., unique number of owners), sorted in descending order based on number of files modified.
Value ‘2’ : verbose mode: For each user (i.e., owner), list the files that were modified. The format for each file should be the results of ‘ls –lt filename’

Example output for script mode 1 :
Owner Number of files changes
jjm 3
root 1

Example output for mode 2

jjm
----------
w------- 1 jjm jjm 1998 Feb 8 20:30 plotPDF.m
-rw-rw-r-- 1 jjm jjm 45000 Feb 8 20:29 ex2.1
-rw-rw-r-- 1 jjm jjm 45000 Feb 8 20:29 ex2.2

Root
----------
w-r--r-- 1 root root 3798 Feb 5 20:37 boot.log