shelljenkinsjenkins-groovy

List directories using shell and iterate using groovy


Can someone help on providing solution on this.

Im trying to list folders under a location(/root/var/) using shell script inside a groovy file. Based on number of folders retrieved I have to iterate through the folder names alone(not the files inside the folders) using a For loop in groovy.

Below is my shell script in groovy file.

def folder location = "/root/var/*/" List l = sh(script: "ls -d ${folderlocation}", returnStdout: true).trim().split('\n')

for(fldr in l){ Print(l) }

The output I'm seeing for l is absolute path rather than the folder names.

Ex : /root/var/ contains lib as subdirectory.

I'm seeing l printed as /root/var/lib/

I'm expecting l to contain just lib not the absolute path or any other folder names if any.

Can someone pls help me.


Solution

  • If you use cd /root/var && ls -d ./* this will list all the the folders within /root/var without the leading path.