Created
December 18, 2020 15:56
-
-
Save murgo/1147b6a84f0d11536fab87085a44a8b5 to your computer and use it in GitHub Desktop.
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
#!/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