Issue
How to make file expressions work with Jenkinsfile?
I want to run simple command rm -rv !(_deps)
to remove all populated files except _deps
directory. What I've tried so far:
sh '''rm -rv !(_deps)'''
which causedscript.sh: line 1: syntax error near unexpected token `('
sh 'rm -rv !\\(_deps\\)'
which causedrm: cannot remove '!(_deps)': No such file or directory
(but it DOES exist)sh 'rm -rv !\(_deps\)'
which causedunexpected char: '\'
sh 'rm -rv !(_deps)'
which causedsyntax error near unexpected token `('
Solution
In Bash to use pattern matching you have to enable extglob:
So the following will work. Make sure your shell executor is set to bash.
sh """
shopt -s extglob
rm -rv !(_deps)
"""
If you don't want to enable extglob you can use a different command to get the directories deleted. Following is one option.
ls | grep -v _deps | xargs rm -rv
In Jenkins
sh """
ls | grep -v _deps | xargs rm -rv
"""
Answered By - ycr
Answer Checked By - Cary Denson (JavaFixing Admin)