shell - Linux命令删除特定目录以外的目录在管道的shell脚本中失败
问题描述
在/home/oracle/jenkins/workspace/test/
位置,我有多个目录。我想删除所有目录,除了test1
我使用终端中的以下内容 -
rm -rf /home/oracle/jenkins/workspace/test/!("test1")
同样,我想通过 Jenkins 管道实现,因此编写了方法 -
def cleanWorkspaceDir() {
echo "Cleaning workspace"
sh '''rm -rf /home/oracle/jenkins/workspace/test/!("test1")
'''
}
但它给出了错误 - /home/oracle/jenkins/workspace/RedmineAndReviewboardProject/SVNCheckout@tmp/durable-810bac2b/script.sh: line 1: syntax error near unexpected token
('`
你能帮我解决这个问题吗?
解决方案
您可以尝试以下方法:
def cleanWorkspaceDir() {
echo "Cleaning workspace"
sh '''find test/ -mindepth 1 '!' -name test1 -type d -exec rm -rf '{}' +
'''
}
pipeline {
agent { label 'slave' }
stages {
stage('Hello') {
steps {
sh 'mkdir -p test/{a/{p,q},b,c/{r,s},test1,test2}'
sh 'ls -lR'
cleanWorkspaceDir()
}
}
}
}
将上面的命令替换find
为以下任何内容:
find /home/oracle/jenkins/workspace/test/ -mindepth 1 -type d -not -name test1 -delete
find /home/oracle/jenkins/workspace/test/ -mindepth 1 ! -name 'test1' -type d -exec rm -rf {} +
推荐阅读
- sql - 雪花如何拆分具有用逗号分隔的值的字段并将它们分别添加到目标表中的每个值在一行中?
- javascript - 如何在 Options API 中使用从 Composition API 返回的 ref?
- datastage - 如何使用自定义窗口在 Datastage 中添加第三方应用程序
- knex.js - 在 knex 中,如何在没有 raw 的情况下访问连接中的列值?
- python - 当您需要遍历行时,Pandas Dataframe 是不是最好的 ETL 代码解决方案?
- c++ - 转发引用不是被推导出为 r 值引用吗?
- delphi - 从 INI 文件加载 TSQLConnection 参数时出现“找不到元数据”错误
- spring-boot - Nginx/SpringBoot/Kubernetes - 客户端 IP 的 X-Forwarded-For 标头
- node.js - 获取对节点中本机绑定的所有调用
- video - 如何使用 ffmpeg 复制移动设备视频压缩