Iterating again and again
Table of contents
I sometimes find myself having to iterate across a bunch of directories.
Usually, I have something to do in those directories like running an arbitrary
command. There’s a lot of different ways to do things, but for the sake of this
example, we’ll use ls
to get the output of directories and iterate on them.
In this example, I have a file I need to copy across multiple directories. It’s
a configuration file named config.yml
that is adjacent to the directories I
want to place it in.
You could open a GUI window and copy / paste the file across. But instead of doing things manually, let’s have the computer do it for us. I’m going to break down my thought process of how I compose the loop to achieve this.
Let’s see what’s inside the target directory
With the -p
flag on ls
, we add a slash to any directories found. From the
output above, you can see that we are including the config.yml
file as well.
We want to make sure we only include the directories and not the files we find.
So let’s try this again.
Let’s use grep
Using grep
and a regular expression to match at least one character .+
followed by a literal /
. This gives us just the directories which we use ls -p
to give a slash at the end.
Let’s loop and copy
Now that we have a list of directories, we can use a for-loop
in Bash to
iterate over all the directories. Once in the loop, we can run any arbitrary
commands for the number of directories that exist. We also capture the current
directory being iterated on in the ${dir}
variable. Also, note that the
${dir}
variable contains a trailing /
character, so we omit the config.yml
from the destination in the cp
command.
This post was written by a human & not by artificial intelligence (AI) tools. I don't have anything against AI but I am interested in differenciating content created by people versus machines. To find out more about the Not by AI badge, please click it.
If you enjoyed this post, please explore other posts by the topics listed below.
Clean Git house