Skip to content

Instantly share code, notes, and snippets.

@Jeff-Russ
Last active May 20, 2016 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeff-Russ/5f4faf3edbe38d0e1979411553d9ca8a to your computer and use it in GitHub Desktop.
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
#!/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
#!/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