#!/bin/bash filename="$1" # Check if the filename contains a period if [[ $filename == *.* ]]; then # Extract the filename without the extension name="${filename%.*}" # Extract the extension extension=".${filename##*.}" else # If there's no extension, the name is the whole filename name=$filename extension="" fi echo "Filename without extension: $name" echo "Extension (if any): $extension" generate_filename() { echo "${name}_${counter}${extension}" } if [ ! -f ${filename} ]; then echo ${filename} exit; fi counter=1 # Check if the file exists and increment the counter until a unique filename is # found while [ -f $(generate_filename) ]; do ((counter++)) done echo $(generate_filename)