Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

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
    fi
done
share|improve this answer
2  
That fails if there's a regular file named "uploads". –  Dennis Williamson Mar 23 '10 at 0:56
1  
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,

--jed

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

 
discard

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.