I’ve prepared an exercise for you all. It’s intended to target tools and techniques you’ll need for program 1. This is not a required assignment and is not worth any points.
This exercise asks you to write a bash shell script that counts the number of lines of all files in the current directory and any subdirectories.
Suggested tools: wc, ls, cd, for loops, if statements
Many programs, such as cp, have a -r recursive option. You will be creating a very specialized version of that option for wc. Your script should add the lines of all files in the current directory. If any of these files are directories, it should also add the lines of any files in those directories, and then any directores in there, and so on and so forth. Finally, your program should send this result to stdout. This is generally a recursive process. You could try to implement a queue or stack in bash, but I would not recommend it. You should not use any of the tools banned in program 1.
I’ve provided a directory set up you can use for testing. (It is named dir.)
Example usage(using instructor given solution, mine is slightly different):
$ wc -l *
wc: dir1: Is a directory
wc: dir2: Is a directory
wc: dir4: Is a directory
$ wc -l dir1/*
wc: dir1/dir3: Is a directory
$ wc -l dir2/*
$ wc -l dir1/dir3/*
83 # Notice this total is the sum of all previous totals
The solution given is named allLines(instructor’s solution) inside of dir.