简体   繁体   English

递归符号链接目录树

[英]Recursively symlink directory tree

I'm trying to run a recursive symlink from one directory into another:我正在尝试将递归符号链接从一个目录运行到另一个目录:

find data/* -type d -exec ln -s {} current/{} \;

With one addition: I need to strip off data/ from the prefix.添加一项:我需要从前缀中去除data/

Running on OS X server (10.8, Mountain Lion)--not all standard GNU commands, like cp -rs , are supported.在 OS X 服务器(10.8,Mountain Lion)上运行——并非所有标准的 GNU 命令,如cp -rs ,都受支持。

What I mean by recursively:我的意思是递归:

data is a list of persistent directories between Laravel releases: data是 Laravel 版本之间的持久目录列表:

data/
    \ storage/
        - framework/
            - session/
        - app/
        \ logs/

They need to map to:他们需要映射到:

current/
    \ storage/
        - framework
            - session/
        - app/
        - logs/
      # Also in storage, but we do NOT want to persist
        - debugbar/
        - framework/
            - cache/
            - views/

Our data directory is will be persistent storage between application launches, as we update our site, while retaining previous versions of the site in the event of rollbacks ( current happens to be a softlink to the latest release).我们的data目录将在应用程序启动之间永久存储,因为我们更新我们的站点,同时在回滚的情况下保留站点的先前版本( current恰好是最新版本的软链接)。

Note: we also have other sites other than Laravel.注意:除了 Laravel,我们还有其他网站。 data is going to be our standard, and we're going to match directory restructure depending on what the site requires for persistence. data将成为我们的标准,我们将根据站点对持久性的要求来匹配目录重组。 It won't always be data/storage .它不会总是data/storage

You don't need to do any recursion.你不需要做任何递归。 Once you've linked to a directory, all of the files and directories under it are accessible through the link.链接到目录后,可以通过该链接访问该目录下的所有文件和目录。

The only links you need are to the directories at the top level…您需要的唯一链接是顶级目录......

cd current
ln -s ../data/*/ .

Or, depending on your requirements, making current a link to data might even be sufficient.或者,根据您的要求,使current链接到data甚至可能就足够了。

I am answering about how to recursively sylink all files from one directory to another.我正在回答如何将所有文件从一个目录递归链接到另一个目录。

I needed this and couldn't find any good solutions around for macOS .我需要这个,但找不到任何适用于macOS好的解决方案。

Following is the best solution if available:如果可用,以下是最佳解决方案:

cp -Rs <src_dir> <dest_dir>

Otherwise, what I did is:否则,我所做的是:

#! /usr/bin/env bash

symlink_recursive_copy() {
    local src_dir=$1 ; shift
    local target_dir=$1
    local basename
    local cdir
    local full_path_cdir
    local full_path_target_dir
    local opwd=$PWD

    if [ -e "$target_dir" ]
    then
        >&2 echo "$target_dir already exists"
        return 1
    fi

    target_dir="$opwd/${target_dir##*/}"

    cd "$src_dir"

    while IFS= read -r -d '' file
    do
        basename="${file##*/}"
        cdir="${file%/*}"
        cd "$cdir" ; full_path_cdir=$PWD ; cd "$OLDPWD"
        mkdir -p "$target_dir/$cdir"
        ln -s "$full_path_cdir/$basename" "$target_dir/$cdir/$basename"
    done < <( find . -type f -print0 )

    cd "$opwd"
}

symlink_recursive_copy "$@"

The above should work with any kind of files containing newlines, spaces, quotes, trailing newlines etc.以上应该适用于包含换行符、空格、引号、尾随换行符等的任何类型的文件。

GNU stow. GNU斯托。 https://www.gnu.org/software/stow/ https://www.gnu.org/software/stow/

Try:尝试:

# cd to dir above ./data
mkdir datalinks
stow -d datalinks data

Stow will intelligently work around existing files in the source dir as well. Stow 也将智能地处理源目录中的现有文件。 If a dit doesn't exist it'll symlink the dir, but if it does, it'll symlink the contents.如果 dit 不存在,它将对目录进行符号链接,但如果存在,它将对内容进行符号链接。

Takes a while to get one's head around it but once you do, it opens up a new way of thinking about file/symlink mgmt.需要一段时间才能理解它,但是一旦你做到了,它就开辟了一种思考文件/符号链接管理的新方式。 Think about a sparsely populated tree where only new files are actual files, and everything else is a symlink to what was there before - and you have Apple's time machine.想想一个人烟稀少的树,其中只有新文件是实际文件,而其他所有文件都是之前文件的符号链接——你有苹果的时间机器。 If you've ever browser a time machine backup from a shell, you'll see it's identical to an rsnapshot backup.如果您曾经浏览过 shell 中的时间机器备份,您会发现它与 rsnapshot 备份相同。 All based on sparse tree symlink population, of which stow is the swiss army knife.全部基于稀疏树符号链接种群,其中stow是瑞士军刀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM