Wednesday, October 26, 2011

Set up shared directory in Linux

Here is how to set up a shared directory or project among linux users. The scenario is:
  • The name of the shared directory: /home/projects
  • The shared group name: developers
  • Users: john, joe, and jane

First, log in as super user, create the projects directory.
$ mkdir /home/projects

Second, create a shared group.
$ groupadd developers

Set up directory for group sharing.
# Change group on the directory
$ chgrp developers /home/projects

# Set full permission for group on the directory
$ chmod -R 775 /home/projects

# Set SGID bit on the directory so that new files under this directory will inherit the directory's group instead of the user's (creator) group.
$ chmod -R g+s /home/projects

# OR combine the last two commands into one
$ chmod -R 2775 /home/projects

# Set umask on the directory so that the 'group write' permission will be inherited on new files and directories
$ umask 002


Add users to group as supplementary group.
$ useradd -G developers john
$ useradd -G developers joe
$ useradd -G developers jane

# Check their groups
$ id john

# Add a user to multiple supplementary groups (with no space after commas):
$ useradd -G wheel,ftp,www,developers john

# Add existing user to an existing group
$ usermod -a -G wheel

# Change user's primary group
$ usermod -g developers john

You can also use this command to set the primary group for a user
$ useradd -g developers john

Saturday, October 1, 2011

htaccess Directory Listing Configurations

To prevent people from browsing your directoy, put this in your .htaccess file.
Option -indexes
OR:
IndexIgnore *

To allow people to see everything in your directory:
Option +indexes


To allow people to see everything except image files:
IndexIgnore *.gif *.jpg

How to Merge Tables

This is an example of how to insert all the missing rows from one table, tbl_backup, into another table, tbl_target. Since the primary_key is auto incremented, we will use col2 and col3 to identify unique rows.

INSERT IGNORE INTO `tbl_target`
  SELECT * FROM `tbl_backup` AS bkup
  WHERE ROW(bkup.col1, bkup.col2) NOT IN
    (SELECT orig.col1, orig.col2 FROM `tbl_target` AS orig);