Skip to content

Instantly share code, notes, and snippets.

@murgo
Created December 18, 2020 15:56
Show Gist options
  • Save murgo/1147b6a84f0d11536fab87085a44a8b5 to your computer and use it in GitHub Desktop.
Save murgo/1147b6a84f0d11536fab87085a44a8b5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
die () {
echo >&2 "$@"
echo
echo "Usage:"
echo "duplicatefile.sh <originalFileLocation> <targetPath> <copyCount>"
echo
echo "Example usage: ./duplicatefile.sh file.txt . 5"
echo
echo "Output files would be named as file_1.txt, file_2.txt, file_3.txt, file_4.txt, file_5.txt."
echo "Output folder will also contain files.zip that has all the files contained in the folder."
echo
echo "CAUTION: ANY FILES WITH THESE NAMES WILL BE OVERWRITTEN!"
echo
exit 1
}
[ "$#" -ne 3 ] && die "3 arguments required, $# provided."
ORIGINALFILELOCATION=$1
TARGETPATH=$2
COPYCOUNT=$3
echo $COPYCOUNT | grep -E -q '^[0-9]+$' || die "Numeric argument required for count, $COPYCOUNT provided."
test -f $ORIGINALFILELOCATION || die "File $ORIGINALFILELOCATION not found."
test -d $TARGETPATH || die "Path $TARGETPATH not found."
#echo "Making $COPYCOUNT copies of file $ORIGINALFILELOCATION into $TARGETPATH"
for (( i=1; i<=$COPYCOUNT; i++ ))
do
ORIGINALFILENAME=$(basename -- "$ORIGINALFILELOCATION")
EXTENSION="${ORIGINALFILENAME##*.}"
FILENAMEWITHOUTEXTENSION="${ORIGINALFILENAME%.*}"
NEWFILENAME="${FILENAMEWITHOUTEXTENSION}-$i"
[ "$FILENAMEWITHOUTEXTENSION" != "$EXTENSION" ] && NEWFILENAME="${NEWFILENAME}.${EXTENSION}"
NEWPATH=$TARGETPATH/$NEWFILENAME
cp $ORIGINALFILELOCATION $NEWPATH
done
ZIPNAME="$TARGETPATH/files.zip"
#echo "Creating a zip from all the files in path $TARGETPATH into $ZIPNAME"
test -f $ZIPNAME || rm $ZIPNAME
zip -j -q $ZIPNAME $TARGETPATH/*
#echo "All done."
#echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment