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 '17 at 11:26
  • @Yaron Example added. I hope my question is clear now. Please help me out – Ejjagiri Venkatesh Apr 27 '17 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 '17 at 11:48
  • 1
    Just copy SNAPSHOT ? – Spike Apr 27 '17 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. – don_crissti Apr 27 '17 at 12:08

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 '17 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. – George Vasiliou Apr 27 '17 at 12:08

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
' {} {} \;
  • 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. – Ejjagiri Venkatesh Apr 28 '17 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? – Ejjagiri Venkatesh Apr 28 '17 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 – Ejjagiri Venkatesh Apr 28 '17 at 3:53
  • earlier the cp command worked, but now this variable is not getting assigned. – Ejjagiri Venkatesh Apr 28 '17 at 3:56

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. – Luciano Andress Martini Apr 27 '17 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). – George Vasiliou Apr 27 '17 at 12:10
  • Sorry dont have a good english, can you explain what is "Is"? – Luciano Andress Martini Apr 27 '17 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). – George Vasiliou Apr 27 '17 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. – Luciano Andress Martini Apr 27 '17 at 12:15

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. – George Vasiliou Apr 28 '17 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.