2016-04-02 33 views
1

比方说,我有以下的目录层次结构创建符号链接,同时保留子树

|/home 
|  |/john 
|  |  |/app 
|  |  | |/folder1 
|  |  | |  |/data1.dat 
|  |  | |  |/... 
|  |  | |/folder2 
|  |  | |  |/... 
|  |  | |/cfg  
|  |  | |  |/settings.cfg 
|  |  | |  |/... 
|  |  | |/start.sh 
|  |/deer        <-- I'm here 
|  |  |/app 

我需要符号链接(空间原因)都在/ home/JOHN /应用程序中的文件排除下的文件/家/ john/app/cfg(它们是为每个用户定制的)/ home/deer/app,同时保留app文件夹中的子目录树。

我该如何做到这一点?我尝试使用rsync(重新创建子文件夹树)和find(列出cfg中没有的文件)的组合,但我无法告诉ln在正确的子文件夹内创建符号链接。

rsync -a -f"+ */" -f"- *" /home/john/app/ app/ 
find /home/john/app/* -type f -exec ln -s '{}' app/ \; # I'm stuck here 

在此先感谢。

+0

没有必要链接*每*下一个 '/ home/JOHN /应用程序' 文件。只需链接每个目录,但'cfg'和'./app'下的每个常规文件。 – agc

+0

这是一个简化,实际上我有另一个dir'plugins'在名为'addons'的第四个目录下,我想排除它,所以要做到这一点,我必须用maxdepth逐级遍历层次结构,更让我感到困惑。 – Robotex

+0

啊,第二个排除,在一个子目录中......我在发布一个没有解释的答案后读到这个。嗯...... – agc

回答

0

我设法实现我的“的hackish”的方式想通过改变工作目录,并与find获取相对路径,这里就是我所做的

# Some variables 
basedir="/home/john" 
currentdir=$(pwd) 

# Duplicate directory tree 
rsync -a -f"+ */" -f"- *" ${basedir}/app/ app/ 

# Create links 
(cd ${basedir}; find * -type f -path "app/*" -not -path "*/cfg*" -exec ln -s ${basedir}/'{}' ${currentdir}/'{}' \;) 

括号是有取消的cd效果

不过,我欢迎任何更漂亮的解决方案。

0

之前(一特设规范的副本在OP):

% tree /home/ 
/home/ 
├── deer 
│   └── app 
└── john 
    └── app 
     ├── cfg 
     │   └── settings.cfg 
     ├── folder1 
     │   └── data1.dat 
     ├── folder2 
     └── start.h 

8 directories, 3 files 

代码,没有rsync的,具有什么人LN调用的的 “第二形式” LN语法'ln TARGET'(在当前目录中创建TARGET的链接); (也LN * -SR相对符号链接):

cd /home/deer/app/ 
find /home/john/app/ -maxdepth 1 -not -path "*/cfg" -not -path "*/app/" -exec ln -sr '{}' \; ; 
cd - > /dev/null 

...之后:

% tree /home/ 
/home/ 
├── deer 
│   └── app 
│    ├── folder1 -> ../../john/app/folder1 
│    ├── folder2 -> ../../john/app/folder2 
│    └── start.h -> ../../john/app/start.h 
└── john 
    └── app 
     ├── cfg 
     │   └── settings.cfg 
     ├── folder1 
     │   └── data1.dat 
     ├── folder2 
     └── start.h 

9 directories, 4 files