Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
"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...)
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.
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.
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?
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?
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.