Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create a Bash script to go into every user's home folder, seek out a wp-content folder, create a directory uploads under it, and then chmod 0756 uploads.

How do I achieve this?

I imagine I need to use find with a regexp/regex, and then tell it to run another bash script on the results.

share|improve this question
up vote 2 down vote accepted

Something like this should work (I haven't tested it)

dirs=`find /home -type d -name "wp-content"` 

for dir in $dirs; do
    if [ ! -e $dir/uploads ]; then 
        mkdir $dir/uploads
        chmod 0765 $dir/uploads
share|improve this answer
That fails if there's a regular file named "uploads". – Dennis Williamson Mar 23 '10 at 0:56
A space is required after the first bracket and before the next. Doublequotes are probably best around $dir/uploads. I only know this because I tested, got errors, and googled. – ServerChecker Mar 23 '10 at 1:36
If we use dirs=$(locate -r 'wp-content$' | grep -i '/home'), it's probably going to run faster than find, I found out. The only catch is that one needs to ensure that updatedb has been run in the past 24 hours. – ServerChecker Mar 23 '10 at 1:41
@Volomike: locate requires the filenames to be in a database that's updated by updatedb which is typically run once a day by cron. Files and directories that have been created since the last run will be missed by your script if you use locate. – Dennis Williamson Mar 23 '10 at 1:46
Another problem here is that find is recursive and will find directories named "wp-content" below the level you intend. You should use the -maxdepth option. – Dennis Williamson Mar 23 '10 at 1:49

The above answer is a better one, but here is a primitive, but functional, alternative:

for i in user1 user2;do mkdir $i/wp-content;chmod 0765 $i/wp-content;done

This assumes you are in the parent directory of all your users, and they are in the same directory.

This will also fail if there is a file named "uploads", but will continue on.

Good luck,


share|improve this answer
You forgot to include the "uploads" directory in your command. You should use && instead of ; between the mkdir and the chmod. And what if there are hundreds (or more) users? – Dennis Williamson Mar 23 '10 at 1:52
All excellent points, although I might not use && because if the directory already exists, I still might want to chmod it (not sure, the poster wasn't specific). If there are hundreds of users, I'd probably use the answer from the previous poster, which I mentioned is a better answer. But if there were just a few users, and I wanted to quickly get this out of the way without bothering to create a script, make it executable, then run it, I'd use my admittedly primitive one-liner. Thanks, --jed – Jed Daniels Mar 23 '10 at 3:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.