Skip to content

Instantly share code, notes, and snippets.

@DinoChiesa
Created July 20, 2016 03:25
Show Gist options
  • Save DinoChiesa/a59f57e20c557b152aba66d33e7dc01b to your computer and use it in GitHub Desktop.
Save DinoChiesa/a59f57e20c557b152aba66d33e7dc01b to your computer and use it in GitHub Desktop.
#!/bin/bash
# -*- mode:shell-script; coding:utf-8; -*-
#
# pcopy is "preserve copy"? copy a file while preserving the directory structure.
#
#
# Created: <Fri Jun 24 13:07:52 2016>
# Last Updated: <2016-July-19 20:25:05>
#
# example:
# pcopy gateway/sw/steps/oauthV2/main/java/com/somebody/steps/oauth/v2/ErrorCodes.java ~/dev/wacapps/apirt-916
pcopy() {
local last=${@: -1}
local FILE all_but_last orig_dir
if [[ $# -ge 2 ]] ; then
#echo "all: $@"
if ! [[ "$last" == */ ]] ; then
last="${last}/"
fi
if [ ! -d "$last" ] ; then
mkdir -p "$last"
fi
all_but_last=(${@:0:$#})
orig_dir=$(pwd)
#echo "all but last: ${all_but_last[@]}"
for FILE in "${all_but_last[@]}" ; do
cd "$last"
mkdir -p "${FILE%/*}"
cd "$orig_dir"
echo "$FILE"
cp "$FILE" "${last}${FILE}"
done
else
echo "pcopy: copy, preserving directory structure"
echo "pcopy: Usage: cp-p SOURCE-WITH-PATH DEST_ROOT/"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment