Skip to content

Instantly share code, notes, and snippets.

@halloleo
Last active August 29, 2015 13:59
Show Gist options
  • Save halloleo/10700468 to your computer and use it in GitHub Desktop.
Save halloleo/10700468 to your computer and use it in GitHub Desktop.
create unique new day-stamped file from a given file stem and extension
#!/bin/bash
# bash snippet:
# create unique new day-stamped file from a given file stem and extension
#
# note the for-loop fto find the first unique (=non-existent) file with
# a given name
#
fnstem="a-file" # here goes the file stem before unifiying
fnext="txt" # here goes the file extenmstion before unifiying
fnstem="${fnstem}_`date +%Y%m%d`"
foundpostfix=false
for postfix in "" "_1" "_2" "_3" "_4" "_5" "_6" "_7" "_8" "_9"; do
fn="${fnstem}$postfix.$fnext"
if [ ! -f "$fn" ]; then
foundpostfix=true
break
fi
done
if [ ! "$foundpostfix" = true ]; then
echo "error: could not generate a free filename (last try \"$fn\")" >&2
exit 98
fi
echo "$fn" # use the unique name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment