What you can do is use the dir step, if the directory doesn't exist, then the dir step will create the folders needed once you write a file or similar:

node {
    sh 'ls -l'
    dir ('foo') {
        writeFile file:'dummy', text:''
    }
    sh 'ls -l'
}

The sh steps is just there to show that the folder is created. The downside is that you will have a dummy file in the folder (the dummy write is not necessary if you're going to write other files). If I run this I get the following output:

Started by user jon
[Pipeline] node
Running on master in /var/lib/jenkins/workspace/pl
[Pipeline] {
[Pipeline] sh
[pl] Running shell script
+ ls -l
total 0
[Pipeline] dir
Running in /var/lib/jenkins/workspace/pl/foo
[Pipeline] {
[Pipeline] writeFile
[Pipeline] }
[Pipeline] // dir
[Pipeline] sh
[pl] Running shell script
+ ls -l
total 4
drwxr-xr-x 2 jenkins jenkins 4096 Mar  7 22:06 foo
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Answer from Jon S on Stack Overflow
🌐
Medium
medium.com › @dipali26N › day-39-creating-directories-in-jenkins-pipelines-278953b74f48
DAY:39 Creating Directories in Jenkins Pipelines | by Dipali Naygaonkar | Medium
July 23, 2025 - If the directory does not exist, Jenkins will create it automatically. writeFile file: 'dummy.txt', text: 'created' Creates a file named dummy.txt inside reports/test_output with the content "created".
Discussions

jenkins pipeline example for creating a folder with a description - Stack Overflow
I can do this manually in the console with a job dsl step, but can't figure out how to accomplish from a pipeline - declarative or scripted. More on stackoverflow.com
🌐 stackoverflow.com
Jenkins - Create Folder and Pipeline in Startup
Hi, I have a doubt, I’ve looked at several forums on the internet but I couldn’t find it. I’m uploading Jenkins in a Kubernetes environment using Helm and would like to know if there is any way to upload it with Folders and Pipelines being created in the initial Jenkins dashboard? More on community.jenkins.io
🌐 community.jenkins.io
0
0
September 1, 2023
How to use a directory across stages?
If the stages were on different nodes or workspaces... for non-large files and directories you could stash () ... unstash() More on reddit.com
🌐 r/jenkinsci
5
5
October 26, 2020
Create a temporary directory for specific steps in Jenkins pipeline - Stack Overflow
Is there a Jenkins pipeline step that will create and run a block of steps in a directory? I know that the dir step runs the steps in the block in a specific directory: // not in /tmp/jobDir dir ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Exchange
devops.stackexchange.com › questions › 15868 › create-a-folder-in-slave-node-using-jenkins-pipeline-groovy-script
git - Create a folder in slave node using Jenkins pipeline groovy script - DevOps Stack Exchange
April 26, 2022 - I am using the mkdir() to create the folder, but it is not being created. I have also tried the dir() method. But nothing seems to be working.
🌐
Jenkins
jenkins.io › doc › pipeline › steps › workflow-basic-steps
Pipeline: Basic Steps
Create stash even if no files are included. If false (default), an error is raised when the stash does not contain files. ... Optional set of Ant-style exclude patterns. Use a comma separated list to add more than one expression. If blank, no files will be excluded. ... Optional set of Ant-style include patterns. Use a comma separated list to add more than one expression. If blank, treated like **: all files. The current working directory is the base directory for the saved files, which will later be restored in the same relative locations, so if you want to use a subdirectory wrap this in dir.
🌐
DevOpsSchool.com
devopsschool.com › blog › jenkins-pipeline-tutorials-how-to-create-folder-and-subfolder
Jenkins Pipeline Tutorials: How to create folder and subfolder?
August 6, 2024 - To create folders and subfolders in Jenkins using pipeline code, you can use Jenkins Job DSL or Groovy script.
🌐
Stack Overflow
stackoverflow.com › questions › 68010833 › jenkins-pipeline-example-for-creating-a-folder-with-a-description
jenkins pipeline example for creating a folder with a description - Stack Overflow
You will likely want to look at your 'Snippet Generator' http://<your-jenkins-instance:8080>/pipeline-syntax/ and look at the 'jobDsl' Sample Step. Generated code from the Snippet Generator will look like this. jobDsl scriptText: '''folder(\'myFolder\'){ description(\'my description\') displayName(\'My Display Name\') }''' ... pipeline { agent any stages { stage('Add Folder') { steps { jobDsl scriptText: """folder('myFolder'){ description('my description') displayName('My Display Name') }""" } } } }
🌐
Jenkins
community.jenkins.io › using jenkins › ask a question
Jenkins - Create Folder and Pipeline in Startup - Ask a question - Jenkins
Hi, I have a doubt, I’ve looked at several forums on the internet but I couldn’t find it. I’m uploading Jenkins in a Kubernetes environment using Helm and would like to know if there is any way to upload it with Folders and Pipelines being created in the initial Jenkins dashboard?
Published   September 1, 2023
Find elsewhere
🌐
Google Groups
groups.google.com › g › jenkinsci-users › c › L-F6szbJ6R4
Jenkins Pipeline @script directory creation
Hello, We are currently running Jenkins 2.7.2 and have a pipeline job defined that utilizes the 'Pipeline script from SCM' SVN repo at the very start, as a result, we get the following folders upon execution:
🌐
Devopsroles
devopsroles.com › home › mastering jenkins pipeline groovy example
Mastering Jenkins pipeline groovy example - DevopsRoles.com Better 2026
May 13, 2024 - You can installed build pipeline plugin on Jenkins server. I will create 3 folder: app1,app-api,app2 and copy war file is app1.war,app-api.war,app2.war
🌐
Reddit
reddit.com › r/jenkinsci › how to use a directory across stages?
r/jenkinsci on Reddit: How to use a directory across stages?
October 26, 2020 -

Is there any way to create a directory in jenkins declarative pipeline and use it across stages?

i.e

node(){
checkout scm
sh "mkdir ${WORKSPACE/conan}" def String dockerImage = 'development-tools'
stage('Testing') {

parallel (
'Release Build': { docker.image(dockerImage).inside(dockerArgs) { 

sh "touch file.txt && mv file.txt  ${WORKSPACE/conan"
 }
 }
stage('Deploy')
 // use file.txt here again
}
🌐
GitHub
gist.github.com › rajeshkumarin › b9cc8c6f83df4114f1cd55bda6eac261
Jenkins Pipeline Tutorials: How to create folder and subfolder? · GitHub
Jenkins Pipeline Tutorials: How to create folder and subfolder? Raw · gistfile1.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
github.com › peterjenkins1 › jenkins-scripts › blob › master › add-folder.groovy
jenkins-scripts/add-folder.groovy at master · peterjenkins1/jenkins-scripts
Useful groovy scripts for configuring Jenkins via Ansible - jenkins-scripts/add-folder.groovy at master · peterjenkins1/jenkins-scripts
Author   peterjenkins1
🌐
Reddit
reddit.com › r/jenkinsci › jenkins creating new project directory each time prefixing with @(number)
r/jenkinsci on Reddit: Jenkins Creating New Project Directory Each Time Prefixing with @(number)
December 31, 2018 -

Hey all, does anyone have any experience with Jenkins creating new project directory when the build is run, when I run my Jenkins build instead of the dir being foobar its foobar@1, I cant seems to find a way to stop this, the original project directory still exists but Jenkins does not use it, I've ended up with several projects directory like this, foobar@1, @2, @3 ect.

Any help would be appreciated.

On further inspection and some googling, I noticed that even with concurrent builds disabled, if a process linking the old Dir is still running from an abort when the new build runs it will spawn a new dir appended with @n -> does anyone have any suggestion to combat this?

🌐
GitHub
github.com › tabeyti › jenkins-jack › issues › 2
how to create pipeline in a folder? · Issue #2 · tabeyti/jenkins-jack
May 21, 2019 - i have folder plugin installed in my jenkins,and this extension can't create pipeline in the folder.
Author   tabeyti
🌐
Jenkins
issues.jenkins.io › browse › JENKINS-46636
Loading...
I use declarative syntax with a very basic Jenkinsfile · pipeline { agent none stages { stage('Change working directory...') { agent { docker 'my-image' } steps { dir('new-dir') { sh 'pwd' } } } } } What it does is simply create a new folder called "new-dir" in the current working directory and ...
🌐
GitHub
github.com › jenkinsci › docker › issues › 966
mkdir: cannot create directory ‘/usr/share/jenkins/ref/plugins/Pipeline.lock’: File exists · Issue #966 · jenkinsci/docker
July 9, 2020 - mkdir: cannot create directory ‘/usr/share/jenkins/ref/plugins/Pipeline.lock’: File exists The command '/bin/sh -c /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt' returned a non-zero code: 1
Author   jenkinsci
🌐
Jenkinsci
jenkinsci.github.io › job-dsl-plugin
Jenkins Job DSL Plugin
We cannot provide a description for this page right now