Saturday, May 28, 2011

Lock a folder(/directory)

Sometime it happens in few cases that you are sharing a common pc and an user account with others. It may happen that you want few of your data to keep away the reach of other users.

use the below code with some modification(where specified) to lock your folder:

dir_lock.sh
#! /bin/bash

# Use the bash shell to run the script

cd <path to folder>

i=$(ls -al | grep "<folder name>")

j=${i:0:10}

echo "$j"

if [ "$j" == 'drwxr-xr-x' ]; then

chmod 000 <folder name>

elif [ "$j" == 'd---------' ]; then

chmod 755 <folder name>

fi

i=$(ls -al |grep '<folder name>')

echo " -> ${i:0:10}"
Now, place the above file dir_lock.sh on the same directory which you want to lock.

now type(in terminal):

1. $ cd <path to directory where your dir_lock.sh file exists>

2. $ chmod 500 dir_lock.sh

3. $ ./dir_lock.sh

from next time use $ ./dir_lock.sh to lock and unlock your directory.

No comments:

Post a Comment