wanderors
wanderors

Reputation: 2292

Get the file name without providing the filename in bash script

I have a jenkins pipeline script which generates an html with a random name. I need to get the file name. The file will be HTML file. I tried by giving like this -

 sh """FILE=`basename /customer/files/html-files/*.html`"""
 echo "${FILE}"

Is there any way i can just get the html file name alone. Please help

So if there is a file- sample.html in that loctaion. The echo output will be sample

Upvotes: 0

Views: 1836

Answers (2)

nandilov
nandilov

Reputation: 719

This works if you have just one file on that directory.

def filename = sh (script: 'basename customer/files/html-files/*html',returnStdout:true).trim()
def filenameWithoutExtension = filename.take(filename.lastIndexOf('.'))                    

Upvotes: 2

Saboteur
Saboteur

Reputation: 1428

basename has option for this:

sh """
FILE=`basename /customer/files/html-files/*.html .html`
echo ${FILE}
"""

Upvotes: 2

Related Questions