Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

ln - Using find on subdirectories and create symlinks to all files

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about using find?

cd -- "$SOURCEDIR"

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...