Last update: 2/1/2017 -
Exercise #2 (we'll work through this together in class)
The objective of this exercise includes the following:
- learning about linking files with ln (and using ls, stat) and named and unamed pipes
- learning how to record a term session and replay it back (script/scriptreplay)
- Bash script
- Introduction to find, xargs, and filters xxx
Step 1 develop ex2.sh
- Please review the course material on UnixFiles.
- On your VM, create a subdirectory ex2 and then: cd ./ex2; wget https://people.cs.clemson.edu/~jmarty/courses/Spring-2017/CPSC424/exercises/ex2.sh
- Do a simple extension to ex2.sh -
- input three arguments - each specifies the number of dummy data files to place in each subdirectory.Name files in each subdirectory using the convention: Fxy.txt where x is {A,B,C} and y is {1, 2, .. .parameterNumberFiles}. For example, two files in myDirC would be names FC1.txt and FC2.txt.
- The contents of each file should be 1 or more lines of text (any string but include the line number). We want some files to be the same size but most files to be different in size.
- Run ex2.sh so that 3 files are created in myDirA, 2 files in myDirB, and 5 files in ./myDirB/myDirC.
Step 2 create a number of linked files
- Using man, learn more about options for the ln, ls and the stat commands.
- In ./ex2 create 4 linked files (add these to the ex2.sh script)
- ln ./myDirA/FA1.txt myFA1.txt; ln -s ./myDirB/FB1.txt myFB1.txt
- ln ./myDirB/myDirC/FC1.txt myFC1.txt; ln -s ./myDirB/myDirC/FC2.txt myFC2.txt;
Step 3 pipes, the script/scriptreplay commands
- Do a 'man mkfifo' and learn about named pipes.
- The exercise is to have two unrelated programs (ls and cat ) running in separate shells communicate using a named pipe (i.e., : ls -ilt | cat )
- In two term sessions (term1 and term2) (cd ./ex2 in both)
- term1: >mkfifo pipe
- term2: >cat < pipe
- term1: ls -ilt > pipe
- term2 >CNT-C
- term1: >rm ./pipe
-
- read the man pages on script and scriptreplay.
- In term1,
- script -a session.txt --timing=timing.txt
- ls
- exit
- replay to make sure it looks ok : >scriptreplay --timing=timing.txt session.txt
- cd ./ex2;
- wget https://people.cs.clemson.edu/~jmarty/courses/Spring-2017/CPSC424/exercises/ex2Timing.txt
- wget https://people.cs.clemson.edu/~jmarty/courses/Spring-2017/CPSC424/exercises/ex2Script.txt
- replay our script...>scriptreplay --timing=ex2Timing.txt ex2Script.txt
- Back to pipes....we will use pipe + script to allow another term to see in real time what is happening on term1
- term1: mkfifo pipe
- term1: script -f pipe
- term2: cat foo
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-
- ls - The man page shows many options that might be helpful,
- Example
- ls -Rlrtc --time-style=full-iso ~ | tail -10
- drwxrwxr-x 2 jjm jjm 4096 2017-01-30 01:44:36.475281410 -0500 myDirA
- drwxrwxr-x 2 jjm jjm 4096 2017-01-30 01:44:48.035281410 -0500 myDirB
- /home/jjm/tmp/lnEx/myDirA:
- total 4
- -rw-rw-r-- 1 jjm jjm 7 2017-01-30 01:44:36.475281410 -0500 f1.txt
- /home/jjm/tmp/lnEx/myDirB:
- total 4
- -rw-rw-r-- 1 jjm jjm 7 2017-01-30 01:44:48.035281410 -0500 f2.txt
- To get the number, pipe the above command to wc
- Unless there is an ls option for limiting the displayed based on last access (or modified) time, the script would have to do this ... which is certainly possible.
- find is a more natural choice as it has quite a bit of support for specifying the type of file we are looking for :
- Example : find all files of a given size
- sudo find /home -size +2M -ls
- sudo find /home -size +2M | xargs
- sudo find /home -size +2M | ls -lt
- sudo find /home -size +2M | xargs ls -lt
- Example
: find all files modified by a given time
- find . -newermt "2016-01-01 00:00:00" ! -newermt "2017-01-02 00:00:00"
-ls | wc
- find ~ -mtime -10 -mtime +4 -ls | wc
- find has + and - operator, Also *time : mtime, atime and ctime :
atime == Acccess Time
mtime == Modified Time
ctime == Create Time
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