This help article assumes that you understand the command-line interface, and general bash commands.


A for-loop is simply a way for you to apply the same action to many objects without having to repeat yourself. Take this example:

for i in $(find /home/*/public_html/* -type f | grep readme.html); do grep -m 1 -i version $i; done

What I’m saying there is “for each bit of output from the command inside the parentheses (defined as the variable ‘i’), run these commands, and once you have run these commands on each bit of output, you are done.” Inline, it would look like this:

for [variable name] in $(command you use to find all of thing objects requiring action); do [command as you would execute it, using your variable in the place you would normally put the object name];done

ALWAYS check your code before you run it, and make sure you are positive about what each part of your for-loop will do before you run it.

For example, here are a couple of my favorite for-loops:

Unzip all of the zip files in my current directory. When setting up a new WordPress install, I have a base of plugins and themes I like to install. This makes installing all of those a whole lot easier than uploading them all manually through the WordPress interface.

for i in $(ls ./*.zip); do unzip $i;done

Find all of the files in any public_html folder, and grep out the first occurrence of the word ‘version’ (useful for finding wordpress and other software installations that are out of date)

for i in $(find /home/*/public_html/* -type f | grep readme.html); do echo $i; grep -m 1 -i version $i; done