Find a file matching with certain pattern and giving that file name as value to a variable in shell script?

Let say your file is following this pattern file-1.2.0-SNAPSHOT.txt so it can be like file-1.2.0-SNAPSHOT.txt or file-1.3.0-SNAPSHOT.txt or file-1.5.1-SNAPSHOT.txt etc. then you can get the files using find command like this :-

find . -type f -iname "*SNAPSHOT.txt" 

It will give you all the files which ends with SNAPSHOT.txt and then you can use it to do your work.

Dot(.) in find can be a parent directory which should contains the file. Like as

find ~/my_files/ -type f -iname "*SNAPSHOT.txt" 

I think what you're trying to do is to copy only the last version.


cd "$oldlocation"

#Get the last version
file="$(ls  *SNAPSHOT.txt | sort -V | tail -n1)"

cp -v "$file" "$newlocation" 
echo "Everything is ok"

selection happens via the -name option and action is via the -exec option.

find . -type f -name '*-[0-9].[0-9].[0-9]-SNAPSHOT.txt' -exec sh -c '
  # do what you want with $file as many times as you want
' {} {} \;