Jump to content

Permissions Script


Recommended Posts

Okay, I am trying to write a script to help with changing permissions. What I would like it to do is look at a directory and all sub directories and change the permissions on files with a given extension. For instance if there have been 30 .doc files sent to the server all being in different directories I would like to be able to change their permissions in one fatal swoop.

 

Thanks. :D

Link to post
Share on other sites

## Here is a very simple way to do that, you may need to add a step to verify the file such as `grep [A-Z]*[a-z]*[0-9].doc` and you would of course have to be root. This will also only work if the file has no spaces in the name. In that case you would have to convert the file to remove the spaces before the chmod.

 

updatedb && locate *.doc |xargs chmod 644

Link to post
Share on other sites

After looking around i found a great way to do this:

 

find . -type f -name "*.doc" -exec chmod 644 {} \;

 

If you don't know what any of this does:

 

The . (dot) tells find were to start looking. A . (dot) starts looking from the dir you execute the command from. /home/user (a 'hard' path) is also legal.

 

-type f tells find to look for files and not directories, which would be -type d.

-name "*doc" here the name, or part of it can be given. This one looks for files that end with doc.

 

-exec chmod 644 {} \; find enables you to execute other unix command from within its structure. chmod 644 is excuted on every file found.

 

Please be carefull if you are not experienced with the -exec {} \; structure.

One 'trick' to test if all is well: -exec echo "" {} \;

Instead of executing the command it will be echoed.

 

For details: man find and google.

Link to post
Share on other sites
  • 1 year later...
find . -type f -name "*.doc" -exec chmod 644 {} \;

 

This is always a little bit of a discussion. Using the -exec option with find means that everytime find discovers a file it will execute the chmod. So if you have 1000 files it will run chmod 1000 times.

 

There is a better way around this though and grep420 sort of shows it above:

 

find . -type f -name '*.doc' -print0 | xargs -0 chmod 644

 

Now what happens is that find runs and just prints out all the files that match *.doc. Now in comes xargs, what this does is collects the list of filenames and builds up the maximum allowed files to be passed on the command line.

 

This means that the chmod will, instead of 1000 times, run possibly only once.

 

This will speed up the execution no end, when ever you think of using -exec with find thing again and look to see how xargs might help. :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...