问题描述
前提是:我有一个有很多目录的目录的子目录(其中一些还包含更多的子目录),我有另一个单独的目录在不同的共享模仿布局中的源目录。我现在需要的是循环访问源目录,发现子目录中的文件,然后在目标目录中创建符号链接。
如果这个是不是很清楚,这个帖子描述它相当好,除了这个问题的目的是symlinking dirs,而不是文件本身。
编辑:刚注意到Kerrek忘了包含这个链接:
好吧,到目前为止,我有这个基于Kerrek的答案:
pre $ $ $ $ $
$ =/ home / simon / testdir / dest
cd $ DEST
find $ SOURCE -type f -exec ln -s - {}{} \;
退出
给出以下内容:
ln:创建符号链接`/home/simon/testdir/src/new.dir/a':文件存在
ln:创建符号链接`/home/simon/testdir/src/new.dir/b':文件存在
ln:创建符号链接`/home/simon/testdir/src/new.dir/c':文件存在
但是,它实际上并不在目标目录中创建符号链接。
查找?
<$ p $ $ SOURCEDIR
find -type d -exec mkdir --parents - $ DESTDIR/ {} \;
find -type f -exec ln --symbolic - $ SOURCEDIR/ {}$ DESTDIR/ {} \;
Ok, so I've been trying to get my head around this, but I'm struggling.
The premise is this: I have a directory with lots of subdirectories (some of which also contain more subdirectories), and I've got another separate directory on a different share which mimics the source directory in the layout. What I need now is a way of looping through the source directory, discovering the files in the subdirs, and then creating symlinks to them in the destination dir.
In case this isn't that clear, this post describes it fairly well, except that that question is aimed at symlinking dirs, rather than the files themselves.
edit: just noticed what Kerrek was getting at, forgot to include this link: Bash script to automatically create symlinks to subdirectories in a tree
Ok, so so far I have this, based off of Kerrek's answer:
#!/bin/bash
SOURCE="/home/simon/testdir/src"
DEST="/home/simon/testdir/dest"
cd $DEST
find $SOURCE -type f -exec ln -s -- "{}" "{}" \;
exit
which gives the following:
ln: creating symbolic link `/home/simon/testdir/src/new.dir/a': File exists
ln: creating symbolic link `/home/simon/testdir/src/new.dir/b': File exists
ln: creating symbolic link `/home/simon/testdir/src/new.dir/c': File exists
however, it doesn't actually create the symlinks in the destination dir.
How about using find
?
cd -- "$SOURCEDIR"
find -type d -exec mkdir --parents -- "$DESTDIR"/{} \;
find -type f -exec ln --symbolic -- "$SOURCEDIR"/{} "$DESTDIR"/{} \;
这篇关于使用查找子目录并创建符号链接到所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!