WORK_DIR=/tmp/todisc-work # what todisc symlinks are in /tmp suffix=( /tmp/todisc-work[0-9]* ) # just get the numerical suffix suffix=( ${suffix[@]##\/tmp\/todisc-work} ) # loop through and find the largest, and add 1 to it new_suffix=0 for s in ${suffix[@]}; do ((s > new_suffix)) && new_suffix=$((s+1)) done [[ -e /tmp/todisc-work ]] && WORK_DIR=/tmp/todisc-work${new_suffix}