This week at work, I had been tasked to copy a directory in Linux 6 times, all with different names.
This, of course, is not that directory, but let’s pretend it is.
Easy enough, just cp -r the directory 6 times, right? Well, you could, but we always strive to be as clever and efficient as possible.
Let’s say for this example we need a copy of this directory for all 6 employees: David, Mark, Jeremy, Clyde, Warren, and Sam.
There’s a lot of things that surprisingly won’t work, mostly because the directories don’t exist. We could make them, or, as my boss showed me, we could use a little for loop magic.
Is it really all there? Yes! This is truncated, but you can see these users have a clone of the original directory.
So how do I use this?
Well, the items you want to iterate through go after
for x in
The command you want to execute goes after
do and you use the variable
$x to iterate through the items.
If we wanted to create 10 files named each letter of the alphabet, we would use
and you will have those files. You can remove them with an equally simple line: