LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-28-2012, 04:21 PM   #1
AnalBeard
Member
 
Registered: Aug 2010
Distribution: Ubuntu 13.04 Server
Posts: 31

Rep: Reputation: 0
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?
 
Old 02-28-2012, 04:39 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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

Last edited by Dark_Helmet; 02-28-2012 at 05:35 PM.
 
Old 02-28-2012, 05:40 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
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...)
 
Old 03-19-2012, 04:06 AM   #4
AnalBeard
Member
 
Registered: Aug 2010
Distribution: Ubuntu 13.04 Server
Posts: 31

Original Poster
Rep: Reputation: 0
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.
 
Old 03-19-2012, 05:44 AM   #5
dln
LQ Newbie
 
Registered: Mar 2012
Posts: 6

Rep: Reputation: Disabled
Why not just symlink the parent dir?
 
Old 03-19-2012, 06:05 AM   #6
AnalBeard
Member
 
Registered: Aug 2010
Distribution: Ubuntu 13.04 Server
Posts: 31

Original Poster
Rep: Reputation: 0
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.
 
Old 03-19-2012, 06:18 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 03-19-2012, 05:32 PM   #8
AnalBeard
Member
 
Registered: Aug 2010
Distribution: Ubuntu 13.04 Server
Posts: 31

Original Poster
Rep: Reputation: 0
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?
 
Old 03-19-2012, 06:33 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 03-20-2012, 03:06 AM   #10
AnalBeard
Member
 
Registered: Aug 2010
Distribution: Ubuntu 13.04 Server
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
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.
 
Old 03-20-2012, 08:22 AM   #11
AnalBeard
Member
 
Registered: Aug 2010
Distribution: Ubuntu 13.04 Server
Posts: 31

Original Poster
Rep: Reputation: 0
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

Last edited by AnalBeard; 03-20-2012 at 05:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Symlinks Zycus Linux - Software 1 08-24-2011 01:16 PM
? on symlinks eroica Linux - Software 1 09-21-2004 09:45 AM
symlinks.. NSKL Slackware 3 05-26-2002 05:35 PM
Symlinks b0b0 Linux - General 2 11-08-2001 08:43 AM
symlinks tstuhr Linux - General 3 10-05-2001 10:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration