LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2006, 09:44 AM   #1
RaelOM
Member
 
Registered: Dec 2004
Posts: 110

Rep: Reputation: 16
Advanced 'find' against specific groups and perms


I need to search all files in directory X that do not belong to $GROUP and then -exec chgrp {} \; and then I need to find all files in directory X that do not have group rw (If they are a file) or rwx (if it's a directory) and do -exec chmod g+rw (or g+rwx) {} \;

I'm having trouble understanding how I can get find to lcoate these files that aren't belonging to $GROUP. -nogroup won't work since that just checks against non existant groups in the /etc/group file.

Help me obie-wan-ka-newbie... You're meh only hope!
 
Old 10-11-2006, 10:20 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

find . -type f ! -group users searches for files that do not have users as group. The ! (or -not) is used to exclude something.
You can also group things like this:

find . -type f -not \( -group users -o -group visitor \)

All file having users or visitor as group will be excluded from the hits.

Hope this clears things up a bit.
 
Old 10-11-2006, 11:49 AM   #3
RaelOM
Member
 
Registered: Dec 2004
Posts: 110

Original Poster
Rep: Reputation: 16
Yea, I figured that out...

Here's what I got but it's not working as I expected...WTF?!

Code:
# Project Ownership & Group permissions.
for i in `ls -d /proj/*`;
do
	# Set $GROUP Variable
		GROUP=`ls -ld $i | awk '{print $4}'`;
	# Find DIRECTORIES under <project> that do not have group rwx and chmod
		find $i -type d ! -perm -0070 -exec chmod g+rwx '{}' \;
	# Find FILES under <project> that do not have group rw and chmod
		find $i -type f ! -perm -0060 -exec chmod g+rw '{}' \;
	# Find EVERYTHING under <project> that have the incorrect group set
		find $i ! -group $GROUP -exec chgrp -h $GROUP '{}' \;
	# Set Project ROOT /proj/* to 2751
		chmod 2751 $i;
	# Set Project ROOT Group ownership
		chgrp $GROUP $i;
done
 
Old 10-11-2006, 01:14 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

What's it doing, or not doing?
Do you get any error messages?

I'll take a look, but it would be easier if you gave a bit more information
 
Old 10-12-2006, 06:41 AM   #5
RaelOM
Member
 
Registered: Dec 2004
Posts: 110

Original Poster
Rep: Reputation: 16
As it's running through the for loop it's changing everything under /proj/* to the first group it gets assigned. WTF?
 
Old 10-12-2006, 09:40 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This I can understand:
# Set $GROUP Variable
GROUP=`ls -ld $i | awk '{print $4}'`;


But this seems to be unneeded, it always sets it to a group that it already is:
# Set Project ROOT Group ownership
chgrp $GROUP $i;


The following could do something you didn't think of:
# Set Project ROOT /proj/* to 2751
chmod 2751 $i;

All files and directories in /proj/ (not recursive) will get 2751 as permission. Do you also want this to be true for files??

I don't seem to understand your problem: it's changing everything under /proj/* to the first group it gets assigned.
If /proj/dirone has internet as group, all dirs and files under /proj/dirone will have internet as group after you run the script.
Code:
Original filestructure:

drwxr-xr-x  5 druuna internet 4096 Oct 12 16:31 dirone
drwxr-xr-x  5 druuna users    4096 Oct 12 16:31 dirthree
drwxr-xr-x  5 druuna visitor  4096 Oct 12 16:31 dirtwo
-rw-r--r--  1 druuna internet    0 Oct 12 16:31 fileone
-rw-r--r--  1 druuna users       0 Oct 12 16:31 filethree
-rw-r--r--  1 druuna visitor     0 Oct 12 16:31 filetwo

dirone:
total 12
drwxr-xr-x  2 druuna internet 4096 Oct 12 16:31 dironeone
drwxr-xr-x  2 druuna users    4096 Oct 12 16:31 dironethree
drwxr-xr-x  2 druuna visitor  4096 Oct 12 16:31 dironetwo
-rw-r--r--  1 druuna internet    0 Oct 12 16:31 fileone
-rw-r--r--  1 druuna users       0 Oct 12 16:31 filethree
-rw-r--r--  1 druuna visitor     0 Oct 12 16:31 filetwo

dirtwo:
total 12
drwxr-xr-x  2 druuna internet 4096 Oct 12 16:31 dirtwoone
drwxr-xr-x  2 druuna users    4096 Oct 12 16:31 dirtwothree
drwxr-xr-x  2 druuna visitor  4096 Oct 12 16:31 dirtwotwo
-rw-r--r--  1 druuna internet    0 Oct 12 16:31 fileone
-rw-r--r--  1 druuna users       0 Oct 12 16:31 filethree
-rw-r--r--  1 druuna visitor     0 Oct 12 16:31 filetwo

Dirstructure after running the script:

drwxr-s--x  5 druuna internet 4096 Oct 12 16:31 dirone
drwxr-s--x  5 druuna users    4096 Oct 12 16:31 dirthree
drwxr-s--x  5 druuna visitor  4096 Oct 12 16:31 dirtwo
-rwxr-s--x  1 druuna internet    0 Oct 12 16:33 fileone
-rwxr-s--x  1 druuna users       0 Oct 12 16:33 filethree
-rwxr-s--x  1 druuna visitor     0 Oct 12 16:33 filetwo

dirone:
total 12
drwxrwxr-x  2 druuna internet 4096 Oct 12 16:31 dironeone
drwxrwxr-x  2 druuna internet 4096 Oct 12 16:31 dironethree
drwxrwxr-x  2 druuna internet 4096 Oct 12 16:31 dironetwo
-rw-rw-r--  1 druuna internet    0 Oct 12 16:33 fileone
-rw-rw-r--  1 druuna internet    0 Oct 12 16:33 filethree
-rw-rw-r--  1 druuna internet    0 Oct 12 16:33 filetwo

dirtwo:
total 12
drwxrwxr-x  2 druuna visitor 4096 Oct 12 16:31 dirtwoone
drwxrwxr-x  2 druuna visitor 4096 Oct 12 16:31 dirtwothree
drwxrwxr-x  2 druuna visitor 4096 Oct 12 16:31 dirtwotwo
-rw-rw-r--  1 druuna visitor    0 Oct 12 16:33 fileone
-rw-rw-r--  1 druuna visitor    0 Oct 12 16:33 filethree
-rw-rw-r--  1 druuna visitor    0 Oct 12 16:33 filetwo
As you can see, all files in dirone have internet as group after running the script and all files and dirs in dirtwo have visitor as group. Script does exactly what it is supposed to do.
 
Old 10-13-2006, 07:02 AM   #7
RaelOM
Member
 
Registered: Dec 2004
Posts: 110

Original Poster
Rep: Reputation: 16
This is the completed script. basically it will create a list of directory names only and then chgrp rwx on directories and chgrp +rw on files so all group members will always have access to the files, then it extracts the group name of the root (This relies that the /proj/X group is correct) and applies that group ownership recursively throughout the directory structure for that instance of $GROUP, then does a final chmod 2751.

Code:
#!/bin/bash
#
# Author: Rael Mussell
# Date: 1/18/2006
#
# Purpose:  This script is designed to change the group ownership of all
#                  project files to their respective project groups.  This will ensure
#                  that no subwebs ever exist and also allow all developers on the
#                  project to access the files at any time.
#

# Project Ownership & Group permissions.
for i in `ls -d /proj/*`;
do
        # Set $GROUP Variable
                GROUP=`ls -ld $i | awk '{print $4}'`;
                #echo DEBUG: group=$GROUP
        # Find DIRECTORIES under <project> that do not have group rwx and chmod
                #echo find: $i -type d ! -perm -0070 -exec chmod g+rwx '{}' \;
                find $i -type d ! -perm -0070 -exec chmod g+rwx '{}' \;
        # Find FILES under <project> that do not have group rw and chmod
                #echo find: $i -type f ! -perm -0060 -exec chmod g+rw '{}' \;
                find $i -type f ! -perm -0060 -exec chmod g+rw '{}' \;
        # Find EVERYTHING under <project> that has the incorrect group set
                #echo find: $i ! -group $GROUP -exec chgrp -h $GROUP '{}' \;
                find $i ! -group $GROUP -exec chgrp -h $GROUP '{}' \;
        # Set Project ROOT /proj/* to 2751
                #echo chmod 2751 $i;
                chmod 2751 $i;
        # Set Project ROOT Group ownership
                #echo chgrp $GROUP $i;
                chgrp $GROUP $i;
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
root can access a partition and see owners/groups/perms, user gets question marks lurko Debian 2 10-05-2006 11:30 AM
Permissions: giving specific groups write access to a directory kinetik Linux - General 3 07-06-2006 08:30 AM
LXer: Advanced techniques for using the UNIX find command LXer Syndicated Linux News 0 05-04-2006 04:21 AM
howto folder/file sharing with specific groups kmhui Linux - Newbie 5 09-18-2004 07:05 PM
Users, groups, permissions - specific task question Doctafonk Linux - General 2 04-21-2004 09:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration