Taking BASH automation deeper down the rabbit hole

The BASH (or rather, any Linux) shell is like a programming environment in itself: you can do pretty simple stuff with it, and you can do increasingly crazy stuff with it if you wish to venture there… Here’s one way to do some simple automating of stuff using the for-do-done construct in BASH:

Here we have for our example three folders, each containing three files.

A simple loop to do something (like echoing out their names here) on each of the items in the working folder. This is the basic structure of a for-do-done loop in BASH:

$ for i in *;
> do echo $i;
> done
folder1
folder2
folder3

This can be condensed into a single line for easy copy-and-pasting:

$ for i in *; do echo $i; done
folder1
folder2
folder3

Now, let’s go a little deeper and work with each file within each subdirectory here. For that we just nest another for loop within the outer one. Note that there’re commands to change the working directory in and out of the subdirectory each round: