guice
guice

Reputation: 1094

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.

Running on OS X server (10.8, Mountain Lion)--not all standard GNU commands, like cp -rs, are supported.

What I mean by recursively:

data is a list of persistent directories between Laravel releases:

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).

Note: we also have other sites other than Laravel. data is going to be our standard, and we're going to match directory restructure depending on what the site requires for persistence. It won't always be data/storage.

Upvotes: 4

Views: 25716

Answers (3)

Bruce Edge
Bruce Edge

Reputation: 2189

GNU 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. If a dit doesn't exist it'll symlink the dir, but if it does, it'll symlink the contents.

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. All based on sparse tree symlink population, of which stow is the swiss army knife.

Upvotes: 4

Mihir Luthra
Mihir Luthra

Reputation: 6769

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.

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.

Upvotes: 3

user149341
user149341

Reputation:

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.

Upvotes: 6

Related Questions