LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   help with symlinks (https://www.linuxquestions.org/questions/linux-software-2/help-with-symlinks-931805/)

AnalBeard 02-28-2012 04:21 PM

help with symlinks
 
Ok, so i've got a directory with a large number of randomly-named subdirectories inside it, and i would like to symlink the subdirectories and their respective files to another folder.

i'm not very experienced with soft links, is it possible to have it create the folders named the same as they are in the original folder and then symlink the files into them so the program i use to scrape will recognise the file names?

Sorry if i'm not particularly clear in what i want.

For example:

originaldir/abcdef/file.txt

symlinkeddir/abcdef/file.txt

is there any easy way to automate creating folders named the same and then symlinking the files into it?

Dark_Helmet 02-28-2012 04:39 PM

What you describe doesn't make much sense.

What are you trying to do, precisely? As in, what task is this symlink approach supposed to help with?

EDIT:
just as a follow-up, symlinking will default to the same file/directory name as the target if no alternate name is supplied. For instance:
Code:

ln -s /path/to/fileA /alternate/path/
will create a symbolic link named and located at: /alternate/path/fileA

EDIT2:
For a directory containing one level of subdirectories and files within those subdirectories, this shell script will "mirror" the directory structure and populate the subdirectories with symlinks to the original files.

You would runn it with the source directory (the one you wish to mirror) as the first argument and the destination directory as the second argument.

The script will not actually do anything at the moment other than print the commands it would execute. To "turn it on," once you are satisfied that it's doing what you need, delete all the occurrences of the red echo in the script, save, and re-run.

Code:

#!/bin/bash

src=${1}
dst=${2}

if [ -z "${src}" -o -z "${dst}" ] ; then
  echo "You must supply a source and destination directory"
  exit 1
fi

if [ ! -d "${src}" ] ; then
  echo "Source directory (${src}) does not exist."
  exit 2
fi

echo mkdir -p "${dst}"

for filename in "${src}"/* ; do
  if [ -d "${filename}" ] ; then
    subdir=$(basename "${filename}")
    echo mkdir "${dst}/${subdir}"
    for srcFile in "${src}/${subdir}"/* ; do
      if [ "${srcFile}" != "${src}/${subdir}/*" ] ; then
        echo ln -s "${srcFile}" "${dst}/${subdir}/"
      fi
    done
  fi
done


sundialsvcs 02-28-2012 05:40 PM

Perhaps ... (dunno...) ... this notion will help.

"A symlink is a tiny little file with the name of a directory-path in it." That's it... that's all.

When you use a directory-path that, the shell realizes, contains a "symlink" in it, the shell reads the contents of "that tiny little file" and substitutes the directory-path therein contained into the "actual" search path that it subsequently tries to use.

After making that textual substitution, the shell then attempts to find the directory-path so named.

(A hard link is slightly more counter-intuitive: in this case, the "tiny little file" contains an iNode number. Blah, blah blah... pay no attention to the little man behind the curtain...)

AnalBeard 03-19-2012 04:06 AM

Ok, so I probably wasn't as clear as I should have been, having read the replies has made me re-think how to say what I'm trying to achieve. My overall goal was to have a script that you pointed at a directory, then it would go list all the subdirectories and then re-create them in a destination directory, basically creating a copy. It would then create a symlink to the files within the original subdirectories and place them in the newly created directories. Does that make sense? Sorry if it's a bit confusing to read, couldn't think of another way to describe it.

dln 03-19-2012 05:44 AM

Why not just symlink the parent dir?

AnalBeard 03-19-2012 06:05 AM

If I symlink the parent dir, does it recursively create symlinks for all files in the subdirectories? I'll be using a program to scrape the symlinked dirs and the file structure needs to stay in place. I've no idea with using symlinks.

catkin 03-19-2012 06:18 AM

A symlink is just a "signpost". If you create /link as a symlink to /my_dir then opening /link/a/b/c will actually open /my_dir/a/b/c

There are very few exceptions to this. I once found a Perl script that did not follow the above convention.

AnalBeard 03-19-2012 05:32 PM

Ok, so here's an extension on my previous post. Once the scraper had scraped all the folders, it would then deposit artwork in the new dirs. Now, where would the artwork files end up? in my new directories, or the ones they linked to?

Tinkster 03-19-2012 06:33 PM

OK ... you'll need to create a directory tree that mimics the original, and then
individually (via script) symlink the files to the targets, then do your "scraping"...

If you THEN place a new / replacement file in the target structure the new file will
actually live there.


What's the over-all task that you're trying to accomplish, what does the scraping do?


Cheers,
Tink

AnalBeard 03-20-2012 03:06 AM

Quote:

Originally Posted by Tinkster (Post 4631064)
OK ... you'll need to create a directory tree that mimics the original, and then
individually (via script) symlink the files to the targets, then do your "scraping"...

If you THEN place a new / replacement file in the target structure the new file will
actually live there.


What's the over-all task that you're trying to accomplish, what does the scraping do?


Cheers,
Tink

I'm slowly archiving my films, and I'm trying to avoid cluttering the directories up with all the cover art etc so i'll point the scraper at the symlinks instead, as it'll plonk any artwork in the scraped dir.

AnalBeard 03-20-2012 08:22 AM

Slightly off at a tangent, but I've been doing some testing and i think i've sorted how i'm going to recreate all the directories (but not the files), using the following command:

Quote:

rsync -a -f"+ */" -f"- *" source/ destination/
it seems to work fine on the test directories i've set up, but can someone confirm this is the best way to go about doing this?

edit: answering my own question, it does do the trick nicely! now how on earth do i write a script to loop through each folder in the source dir and to then create the symlinks in the newly created destination dirs?

in case anyone needs an answer to this, i have now solved the problem, the following should do the trick:

Quote:

#!/bin/bash

SOURCE="src/"
DEST="dest/"

cd -- "$SOURCE"

find -type f -exec ln -s -- {} "$DEST"/{} \;

exit


All times are GMT -5. The time now is 11:29 AM.