1

I have one directory, folder1, that I have shared via samba. I have another directory, folder2, that is not shared, but contains a lot of subfolders that I do want to share. When a user connects to my shared folder1 I would like them to also see the subdirectories from folder2.

Currently the way I accomplish this is to open up folder2 in gnome, right-click on the directories there, and select Make Links. Then I take the symbolic links and manually cut and paste them into folder1.

I would like to have it so that when I create a new directory in folder2, a symbolic link to it is created inside folder1. How should I go about this? I'm using Ubuntu 14.04

1

There may be automagical ways to do this, but just writing a few shell scripts is probably the easiest way to address this.

First, instead of just creating a new subdirectory in folder2, do it with a script that also creates the symbolic link at the same time. The following untested script will do that once you fill in the real paths to your directories. The core script is really simple, but I added some error processing so it would have a chance in the real world.

#!/bin/bash
## create subdirectory and symbolic link
## Usage: script-name <name of new subdirectory to create and link>

PRIVATE="path to private directory" ## Put real path here
SHARED="path to shared directory" ## Put real path here
cd "${PRIVATE}"
if [ -z "${1}" ]
then
  echo "Aborting - no new directory name specified"
  exit 1
fi

if [ -e "${1}" ]
then
  echo "Aborting - file or directory with that name already exists"
  exit 1
fi

mkdir "${1}"
RC=$?
if ((RC))
then
  echo "Aborting - Failed to create new private subdirectory named [${1}] in [${PRIVATE}] with error code [${RC}]"
  exit ${RC}
fi

cd "${SHARED}"
ln -s "${PRIVATE}/${1}" .
RC=$?
if ((RC))
then
  echo "Aborting - Failed to create new symlink named [${1}] in [${SHARED}] with error code [${RC}]"
  exit ${RC}
fi

exit 0

Other concerns:

If you need to access this from a gui instead of the command line, just add a little code that calls yad to create a dialog box to ask for the name of the directory to create and then create an entry for your script in your menu system and, optionally, on your desktop or panel. Doing this is simple, but beyond the scope of this question.

Similarly, if something else creates the new subdirectories in folder1, you can write a script which scans for new subdirectories in folder1 and then symlinks them to folder2.

You could run this manually, or run it inside of a loop where it does its work, sleeps for awhile, and then runs again.

This "daemon" script could either be started manually or run from your .login script so it will start every time you login. It could also be started from cron if you make sure that any subdirectories and links it creates have the desired owner, group, and permissions. (You probably don't want them owned by root which is the user cron runs under.)

It all depends on what your exact requirements are.

If either folder1 or folder2 require elevated privileges for writing, then your script will need to either be run with those permissions or have a sudo or two to handle that, and possibly, an entry in sudoers so it doesn't have to ask for your password every time. Then, the script itself would have to have a secure owner and restricted permissions and be kept in a directory which also requires elevated privileges for writing - for security reasons.

You may also need to consider the case when subdirectories are deleted from folder1. Then, you will probably want to delete the broken symlinks from folder2. The approach/code would be very similar to the above.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.