Saturday, August 16, 2008

Linux: Share files between two or more users

We need to maintain the situation where all files in a given directory satisfy these requirements:
1. Owned by a common group , say "team".
2. Permissions - 770 or thereabouts.

I have faced this requirement many times, and many others have asked questions about this. So I decided to create a script for this.

The following script fixes the owner of all files in the given directory to root. Group owner is forced to be "team", and file permissions to be 770. It uses inotify to fix a file as soon as it is created, or its permissions are modified. You need to install inotify-tools package of your distribution. Tested with Fedora 9 linux, and eeebuntu on a eeepc.

If you save this as, you have to call it as

sudo nice /path/to/shared/directory/ team


#remove trailing slash

dirRoot=`echo $1 | sed 's/\(.*\)\/$/\1/'`

echo $dirRoot


chown root:$groupname "$dirRoot"

inotifywait -c -e ATTRIB -e CREATE -r -m "$dirRoot" | while read line


dirname=`echo $line | awk -F"," '{print $1}'`

filename=`echo $line | awk -F"," '{print $NF}'`


perm=`ls -ld "$filepath" | awk '{print $1}' | sed 's/.\(.*\)/\1/'`

#remove first letter from permissions string. This is d for a

#directory, and - for a file. Not useful for us.

owner=`stat -c '%U' "$filepath"`

groupowner=`stat -c '%G' "$filepath"`

if [ "rwxrwx---" != "$perm" ]; then

chmod 770 "$filepath"


if [ "root" != "$owner" ]; then

chown root "$filepath"


if [ "$groupname" != "$groupowner" ]; then

chgrp "$groupname" "$filepath"



Known Limitations: Following characters in filename, or directory path not supported. Such files will be silently ignored.
  1. Comma (,)
  2. BackSlash (\)
  3. Double-quote (")
  4. Single-quote (')

This is somewhat crude currently, but it will be modified as I get more feedback.

1 comment:

Phases said...

Great script! Works well on initial use for me. Appreciate your work.

Ubuntu 8.04