File-related operations cannot be performed on the Jenkins slave, and all operations will be performed on the master.
It can be solved by bash shell. For example, you want to detect whether a folder exists and whether there are files in it.
def folder="/test" def isFolderEmpty=sh(returnStdout: true, script: ''' if [ -d '''+folder+''' ]; then [ "$(ls -A '''+folder+''')" ] && echo "true" || echo "false" be''') if(isFolderEmpty.startsWith("true")){ echo "Folder exists and contains files." }else{ echo "Folder does not exists or does not contain any file." }