linux - Bash 帮助删除嵌套目录
问题描述
好的,这听起来可能很奇怪,但我有一个目录 /PDB/ 我想扫描所有包含的目录。所有这些目录都包含几个文件和一个子目录名称 /pockets/,它可能为空,也可能不为空。我想删除每个父目录及其包含空 /pockets/ 子目录的所有内容。到目前为止,我有这个代码:
cd /PDB/
for D in */
do
find -maxdepth 1 -type d -empty -exec rm -r $D +
done
这当前不执行,给出错误 find: missing argument to '-exec'
之前我使用的是 {} 而不是 $D ,但这只是删除了空子目录。
解决方案
我根本不会find
在这里使用。考虑:
#!/usr/bin/env bash
# ^^^^ - NOT /bin/sh; using bash-only features here
shopt -s nullglob # if no matches exist, expand to an empty list
for d in /PDB/*; do # iterate over subdirectories
set -- "$d"/pockets/* # set argument list to contents of pockets/ subdirectory
(( "$#" )) && continue # if the glob matched anything, we aren't empty
rm -rf -- "${d%/pockets/}" # if we *are* empty, delete the parent directory
done
...或者,如果您真的想使用find
:
find /PDB -type d -name pockets -empty -exec bash -c '
for arg; do
rm -rf -- "${arg%/*}"
done
' _ {} +
推荐阅读
- java - XML 文件中的自定义正则表达式字符串模式
- php - Symfony 3:如何使用 Doctrine 同步用户
- ruby-on-rails - 下拉菜单项链接在 rails 和 bootstrap4 中不起作用
- python - kivy 布局:希望将组件的高度比例设置为 5:1
- ionic-framework - 滑动列表可以保存类别和子类别吗?
- python-3.x - 无法在 kaggle 网站中使用 request.post 登录
- python - Networkx:有条件地改变边缘的颜色
- c# - 在 web api 中执行 POST 请求时出现内部服务器错误 500
- javascript - 如何清除单选按钮/将所有选定的单选按钮清除为错误状态。reactjs - 引导
- codeigniter - 代码点火器:导航(视图)到模型:严重性:通知消息:未定义的属性: