Last active
May 20, 2016 18:40
-
-
Save Jeff-Russ/5f4faf3edbe38d0e1979411553d9ca8a to your computer and use it in GitHub Desktop.
This shell script will copy all file and directories recursively but not overwrite anything unless specified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# treeclone - pretty much just a wrapper around cp but only for cloning the | |
# CONTENTS of a directory (via /. ) recursively (-a) and non-destructive (-n) | |
# by default. Useful for preventing disaster from forgetting -n | |
if [ "$#" -eq 1 ]; then TO=$CALLER | |
elif [ "$#" -lt 4 ]; then TO=$2 | |
else | |
echo "incorrect number of args: treeclone takes at 1 - 3 args" | |
echo "This will copy all file and directories recursively but not overwrite anything:" | |
echo " $ treeclone <from-dir> <to-dir>" | |
echo "this will overwrite:" | |
echo " $ treeclone <from-dir> <to-dir> -f" | |
echo "Both paths can be relative and if there is only one argument, it defines" | |
echo "the source with the desination assumed to be the current directory." | |
exit 1 | |
fi | |
if [ "$3" == "-f" ]; then | |
cp -a $1/. $TO/ | |
else | |
cp -an $1/. $TO/ | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# treeclone | |
# THIS VERSION HAS TWO ISSUES: | |
# 1. It does not copy file at root of source. 2. It does not copy hidden items anywhere. | |
if [ "$#" -eq 1 ]; then TO=$(pwd) | |
elif [ "$#" -lt 4 ]; then TO=`cd "$2"; pwd` | |
else | |
echo "incorrect number of args: treeclone takes at 1 - 3 args" | |
echo "This will copy all file and directories recursively but not overwrite anything:" | |
echo " $ treeclone <from-dir> <to-dir>" | |
echo "this will overwrite:" | |
echo " $ treeclone <from-dir> <to-dir> -f" | |
echo "Both paths can be relative and if there is only one argument, it defines" | |
echo "the source with the desination assumed to be the current directory." | |
exit 1 | |
fi | |
if [ "$3" == "-f" ] | |
then FORCE=true | |
else FORCE=false | |
fi | |
FROM=`cd "$1"; pwd` | |
cd $FROM | |
for dir in */ | |
do | |
dir=${dir%*/} | |
dir=${dir##*/} | |
echo $dir | |
if $FORCE | |
then cp -R $FROM/$dir $TO | |
else cp -Rn $FROM/$dir $TO | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment