Created
February 20, 2017 16:26
-
-
Save matthieuheitz/f9747a214d8fb5718a91de1a7eec5c27 to your computer and use it in GitHub Desktop.
Bash - Avoid overwriting a folder and its content by creating indexed folders
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
# Avoid overwriting on output folder | |
if [ ! -d "$OUT_FOLDER_PATH" ]; then # If folder doesn't exist, create it | |
mkdir $OUT_FOLDER_PATH | |
else | |
cpt=0 | |
OUT_FOLDER_PATH_NEW=$OUT_FOLDER_PATH | |
while [ "$(ls -A $OUT_FOLDER_PATH_NEW 2> /dev/null)" ] ; # while folder exist and is not empty | |
do | |
let cpt=cpt+1 | |
echo $cpt | |
OUT_FOLDER_PATH_NEW=${OUT_FOLDER_PATH}-$cpt | |
done | |
OUT_FOLDER_PATH=$OUT_FOLDER_PATH_NEW | |
if [ ! -d "$OUT_FOLDER_PATH_NEW" ]; then | |
mkdir $OUT_FOLDER_PATH | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment