通过子目录bash脚本来循环并创建符号链接的所有文件 [英] bash script to loop through subdirectories and create symlinks to all files

查看:274
本文介绍了通过子目录bash脚本来循环并创建符号链接的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我一直在试图让我解决这个头,但我挣扎。

在premise是这样的:我有很多子目录(其中一些还含有较多的子目录)目录,我有一个不同的共享它模仿布局源目录另一个单独的目录。我现在需要的是通过源目录循环,在子目录发现的文件,然后在目标目录创建符号链接到他们的一种方式。

在这种情况下,不说清楚,这篇文章描述了它相当好,但这个问题是针对符号链接显示目录,而不是文件本身。

编辑:刚刚发现什么Kerrek渐渐的,忘了,包括此链接:<一个href=\"http://stackoverflow.com/questions/1331138/bash-script-to-automatically-create-symlinks-to-subdirectories-in-a-tree\">Bash脚本在一棵树上自动创建符号链接到子目录

好了,到目前为止,我有这个基础了Kerrek的回答:

 #!/斌/庆典SOURCE =/家/西蒙/ TESTDIR / src目录
DEST =/家/西蒙/ TESTDIR /目标CD $ DEST找到$源型的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:文件已存在

然而,实际上它并不在目标目录创建符号链接。

编辑:应该指出的是,上述文件的创建只是为了测试这一点

最后编辑:这似乎已经完成了帽子戏法,感谢Kerrek:

 #!/斌/庆典SOURCE =/家/西蒙/ TESTDIR / src目录
DEST =/家/西蒙/ TESTDIR /目标CD - $ SOURCE发现型的F -exec LN -s - {}$ DEST/ {​​} \\;出口


解决方案

如何使用找到

  CD  - $ SOURCEDIR找到型Ð-exec的mkdir --parents  - $ DESTDIR/ {} \\;发现型的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.

edit: should point out that the files above were created just to test this out.

final edit: this seems to have done the trick, thanks Kerrek:

#!/bin/bash

SOURCE="/home/simon/testdir/src"
DEST="/home/simon/testdir/dest"

cd -- "$SOURCE"

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

exit

解决方案

How about using find?

cd -- "$SOURCEDIR"

find -type d -exec mkdir --parents -- "$DESTDIR"/{} \;

find -type f -exec ln --symbolic -- "$SOURCEDIR"/{} "$DESTDIR"/{} \;

这篇关于通过子目录bash脚本来循环并创建符号链接的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆