I am finding a way to get the filename assigned to a variable in my shell script. But my file has naming format as file-1.2.0-SNAPSHOT.txt.

Here the numbers may change sometimes, now how can i assign this filename to a variable. Any regex can be used? or grep? or find? or file?

My directory consists of following files:


My script sc.sh:

cp $var $newLocation

Now the file version changes sometimes. My script should work for any version number.

How can I assign the matched filename to variable? Help me out. TIA

  • 1
    please clarify, what already exists, what do you want to happen, please give an example.
    – Yaron
    Apr 27, 2017 at 11:26
  • @Yaron Example added. I hope my question is clear now. Please help me out Apr 27, 2017 at 11:44
  • Will you at any point in time end up with multiple SNAPSHOT.txt files? If so, which one should be picked? If you want to pick the latest one, do the filenames sort properly?
    – Kusalananda
    Apr 27, 2017 at 11:48
  • 1
    Just copy SNAPSHOT ?
    – RBB
    Apr 27, 2017 at 11:48
  • 1
    You don't need to. As Spike suggests just copy *SNAPSHOT e.g. cp /file-path/*SNAPSHOT.txt /new-path as the glob will always expand to the right file name. Apr 27, 2017 at 12:08

4 Answers 4


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" 
  • This will fail if more than one *SNAPSHOT.txt is found
    – Philippos
    Apr 27, 2017 at 12:07
  • This is a 50% solution. You need to advise how to handle each result that will be returned by find in order to execute the real copy. Apr 27, 2017 at 12:08

Finally, I got the solution after many trial and error methods:

cd $file_path && fVar=$(find -type f -name 'file-[0-9].[0-9].[0-9]-SNAPSHOT.txt');

echo $fVar    # output is like ./file-1.2.0-SNAPSHOT.txt

fT=${fVar:2}  # removing first two characters'./'

echo "$fT"    # output is file-1.2.0-SNAPSHOT.txt

Thanks Rakesh for contributing your answer, it helped me.

  • Giving to find the optiion -printf %f\\n will return the results without ./ in front. Apr 28, 2017 at 8:03

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"
  • Just added sort -V that is working fine in all tests that i do. Apr 27, 2017 at 12:08
  • With almost 2K Rating, i am sure you have received comments before why is not good to parse output of ls (i just don't have the correct link to redirect you there). Apr 27, 2017 at 12:10
  • Sorry dont have a good english, can you explain what is "Is"? Apr 27, 2017 at 12:12
  • I talk about output of ls command that you use in your solution. Breaks easily (i.e in case of spaces in filename). Apr 27, 2017 at 12:14
  • I think not in this case. Sort and tail will threat the output as lines, spaces does not matter. Also the name of the file never changes, but only the version. Maybe im wrong, but considering this case the script will works. Apr 27, 2017 at 12:15

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
' {} {} \;
  • 1
    How can i assign the filename matching to this pattern to a variable, so that i have multiple operations to do with that variable which is having the matched filename. Apr 28, 2017 at 3:28
  • i may have files like: file-1.2.0-SNAPSHOT.txt, newFIle-1.3.0-SNAPSHOT.txt I need to figure it out a way to get the filename into var, sometimes it may be for file-1.2.0 or for sometimes it may for newFile-1.3.0. There must be a pattern for getting it. Right? Apr 28, 2017 at 3:37
  • fileVar=""; find . -type f -name '*-[0-9].[0-9].[0-9]-SNAPSHOT.txt' -exec sh -c 'fileVar=$1' {} {} \; echo "$fileVar"; but its printing nothing Apr 28, 2017 at 3:53
  • earlier the cp command worked, but now this variable is not getting assigned. Apr 28, 2017 at 3:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .