Skip to content

Instantly share code, notes, and snippets.

@adjam
Created April 27, 2023 17:32
Show Gist options
  • Save adjam/dc33161d58d81d6ff20b5ae014748cf9 to your computer and use it in GitHub Desktop.
Save adjam/dc33161d58d81d6ff20b5ae014748cf9 to your computer and use it in GitHub Desktop.
Bash function to create a unique filename
unique_name() {
local base=$1
local ext=$2
local name="${base}.${ext}"
if [[ -e $name || -L $name ]]; then
i=1
while [[ -e "${base}-${i}.${ext}" || -L "${base}-${i}.${ext}" ]]; do
let i++
done
name="${base}-${i}.${ext}"
fi
echo $name;
}
# filename=$(unique_name "foo-$(date +%Y-%m-%d)" "tgz")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment