Register
It is currently Sun Nov 23, 2014 4:15 pm

Script to Modify File Permissions Based on Directory Name


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri Nov 09, 2007 9:45 am   

Joined: Wed Oct 24, 2007 2:12 pm
Posts: 1
I'm sure it's not the most elegant way to do it but it seems to work well enough.
I was looking all over for something that could be stuck in a cron job to modify the permissions on any directory in a tree with the words "Do Not Modify" in the name. The idea is that it should make all of the files in those directories read only but preserve the permissions of the Directory itself and anything not in that "Do Not Modify" directory.
Here's what I came up with. Have at thee.

Code:
#!/bin/bash
STARTDIR="/change/me/to/your/top/level/directory/containing the DNM directories/"   
GREPSTRING=Do\.Not\.Modify
declare -a DNMDIRS;
num=0
IFS=$'\n'

cd "$STARTDIR"
for i in `find . -type d |grep $GREPSTRING`; do
   DNMDIRS[$num]=$i
   for i in `find ${DNMDIRS[$num]} -type f`; do
      chmod 444 $i
   done
   num=$(($num + 1))
done


Top
 Profile  
 PostPosted: Mon Feb 11, 2008 4:03 am   

Joined: Tue Feb 05, 2008 8:24 am
Posts: 14
jambino wrote:
I was looking all over for something that could be stuck in a cron job to modify the permissions on any directory in a tree with the words "Do Not Modify" in the name. The idea is that it should make all of the files in those directories read only but preserve the permissions of the Directory itself and anything not in that "Do Not Modify" directory.


I think almost the same may be achieved with a shorter command.
Code:
cd /path/to/toplevel/dir && find . -wholename '*DNM*/*' -type f -print0 | xargs -0 chmod a-w


It assumes that any files in directories with DNM in their names and their subdirectories are to become read-only. Files in other directories (even files with DNM in their names) will be untouched. As a bonus, this command works also if there are spaces in file names.

This is a small test. I marked with ★ the files affected by the command:
Code:
[dir] DNM 1
       |--------- [dir] Subdir
       |              |
       |              +-- z ★
       +-- a ★
       |
       +-- b ★
       |
       +-- c ★

[dir] MODIFYME 2
       |
       +-- DNM_the_file_unchanged
       |
       +-- d
       |
       +-- e
       |
       +-- f



Top
 Profile WWW  
 PostPosted: Thu Mar 26, 2009 8:05 pm   

Joined: Thu Mar 26, 2009 7:59 pm
Posts: 1
Location: Toronto, Ontario
Does anyone know if it is possible to modify this script so that

1) It only changes permissions if the permissions are 644
2) It changes permissions in the DNM to 444, otherwise change the permissions to 755

I can see how it is possible to do the the second (I think) not so much the first.


Top
 Profile  
 PostPosted: Fri Mar 27, 2009 3:01 am   

Joined: Tue Feb 05, 2008 8:24 am
Posts: 14
mark_dh wrote:
Does anyone know if it is possible to modify this script so that

1) It only changes permissions if the permissions are 644


> find . -wholename '*DNM*/*' -type f -perm 644 -print0 | ...

mark_dh wrote:
2) It changes permissions in the DNM to 444, otherwise change the permissions to 755


I don't know how to implement “otherwiseâ€￾ in a bash filter. I see two ways:

(a) change everything to 755, then fix perms in the DNM to 444 (bad: there is a moment when some files in DNM become 755)

(b) write the second command with an inversed predicate in find, something like

> find . -not -wholename '*DNM*/*' -type f -print0 | xargs -0 chmod 755


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], popeye and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP