Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3355458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:23:53+00:00 2026-05-18T02:23:53+00:00

I wrote this script to find all files/directories to which $WWWUSER has write permissions.

  • 0

I wrote this script to find all files/directories to which $WWWUSER has write permissions. At first I stored the remaining, matching items in a temporary file. I new there must be a way without using files, so this is my “solution”. It works, but it’s pretty slow. Any tips?

Update:
On a directory structure containing about 7k directories and 30k files (~8k whitelistings) the script takes about 15 minutes… (ext3 filesystem, UW320 SCSI harddisk).

#!/usr/bin/env bash
# Checks the webroot for files owned by www daemon and
# writable at the same time. This is only needed by some files
# So we'll check with a whitelist

WWWROOT=/var/www
WWWUSER=www-data
WHITELIST=(/wp-content/uploads
/wp-content/cache
/sitemap.xml
)
OLDIFS=$IFS
IFS=$'\n'

LIST=($(find $WWWROOT -perm /u+w -user $WWWUSER -o -perm /g+w -group $WWWUSER))
IFS=$OLDIFS

arraycount=-1
whitelist_matches=0

for matchedentry in "${LIST[@]}"; do
        arraycount=$(($arraycount+1))

        for whitelistedentry in "${WHITELIST[@]}"; do
                if [ $(echo $matchedentry | grep -c "$whitelistedentry") -gt 0 ]; then
                        unset LIST[$arraycount]
                        whitelist_matches=$(($whitelist_matches+1))
                fi
        done
LISTCOUNT=${#LIST[@]}
done

if [ $(echo $LISTCOUNT) -gt 0 ]; then
        for item in "${LIST[@]}"; do
                echo -e "$item\r"
        done
        echo "$LISTCOUNT items are writable by '$WWWUSER' ($whitelist_matches whitelisted)."
else
        echo "No writable items found ($whitelist_matches whitelisted)."
fi
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:23:54+00:00Added an answer on May 18, 2026 at 2:23 am

    (I don’t have a setup handy to test this on, but it should work…)

    #!/usr/bin/env bash
    # Checks the webroot for files owned by www daemon and
    # writable at the same time. This is only needed by some files
    # So we'll check with a whitelist
    
    WWWROOT=/var/www
    WWWUSER=www-data
    WHITELIST="(/wp-content/uploads|/wp-content/cache|/sitemap.xml)"
    
    listcount=0
    whitelist_matches=0
    
    while IFS="" read -r matchedentry; do
        if [[ "$matchedentry" =~ $WHITELIST ]]; then
            ((whitelist_matches++))
        else
            echo -e "$matchedentry\r"
            ((listcount++))
        fi
    done < <(find "$WWWROOT" -perm /u+w -user $WWWUSER -o -perm /g+w -group $WWWUSER)
    
    if (( $listcount > 0 )); then
            echo "$listcount items are writable by '$WWWUSER' ($whitelist_matches whitelisted)."
    else
            echo "No writable items found ($whitelist_matches whitelisted)."
    fi
    

    Edit: I’ve incorporated Dennis Williamson’s suggestions on the math; also, here’s a way to build the WHITELIST pattern starting from an array:

    WHITELIST_ARRAY=(/wp-content/uploads
    /wp-content/cache
    /sitemap.xml
    )
    
    WHITELIST=""
    for entry in "${WHITELIST_ARRAY[@]}"; do
        WHITELIST+="|$entry"
    done
    WHITELIST="(${WHITELIST#|})"  # this removes the stray "|" from the front, and adds parens
    

    Edit2: Sorpigal’s comment about eliminating new processes got me thinking — I suspect most of the speedup in this version comes from not running ~40 invocations of grep per scanned file, and just a little bit from removing the array manipulation, but it occurred to me that if you don’t need the totals at the end, you could remove the main while loop and replace it with this:

    find "$WWWROOT" -perm /u+w -user $WWWUSER -o -perm /g+w -group $WWWUSER | grep -v "$WHITELIST"
    

    …which does run grep, but only once (and runs the entire file list through that single instance), and once it’s started grep‘ll be able to scan the list of files faster than a bash loop…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a bash script that looks at a directory full of
I'm writing a python script to loop through a directory of CSS files and
i am trying to execute all tests in subfolders of my test directory. This
I would like to find some way of Viewing a Directory in the default
i use zend framework 1.10 and i have a script under scripts library. i
I'm trying to set an Alias in Apache to use for PHP files where
I'm writing a Chrome extension, and want to write one JS file, that provides
I have virtual machine that has windows 7 + visual studio 2010 setup, and
I'm working on a project to create a CMS, which will entail importing a
I've the following problem, the following code: $link = $_GET['link']; $id = $_GET['block']; echo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.