symlink-copying a directory hierarchy

前端 未结 7 1657
孤独总比滥情好
孤独总比滥情好 2020-12-13 19:38

What\'s the simplest way on Linux to \"copy\" a directory hierarchy so that a new hierarchy of directories are created while all \"files\" are just symlinks pointing back to

7条回答
  •  盖世英雄少女心
    2020-12-13 20:00

    If you feel like getting your hands dirty Here is a trick that will automatically create the destination folder, subfolders and symlink all files recursively.

    In the folder where the files you want to symlink and sub folders are:

    1. create a file shell.sh:

      nano shell.sh

    2. copy and paste this charmer:

    #!/bin/bash

    export DESTINATION=/your/destination/folder/
    export TARGET=/your/target/folder/
    
    find . -type d -print0 | xargs -0 bash -c 'for DIR in "$@"; 
    do
      echo "${DESTINATION}${DIR}"
      mkdir -p "${DESTINATION}${DIR}"        
      done' -
    
    
    find . -type f -print0 |  xargs -0 bash -c 'for file in "$@"; 
    do
      ln -s  "${TARGET}${file}"  "${DESTINATION}${file}"
       done' -
    
    1. save the file ctrl+O
    2. close the file ctrl+X
    3. Make your script executable chmod 777 shell.sh

    4. Run your script ./shell.sh

    Happy hacking!

提交回复
热议问题