如何删除目录中除文件和目录列表之外的所有内容?
how to remove everything inside directory except a list of files and directories?
我需要删除 dist/
文件夹中除 manifest.json
、index.html
和 assets/
之外的所有内容。它将 运行 作为 package.json
.
中的脚本
我尝试用 find
解决
"clean": "find dist ! -name manifest.json -type f -delete | find . -type d -empty -delete"
但我无法让它与多个参数一起工作。
编辑:
所以我想保留:
dist/assets/*
资产中的所有内容。包括像 dist/assets/map/dog.ts
这样的子目录
dist/manifest.json
dist/index.html
并删除其余部分,例如:
dist/pages/
删除目录、子目录和文件
dist/examples/
删除目录、子目录和文件
dist/randomfile.txt
删除文件
dist/hello.js
删除文件
dist/*
等等等等
根据current version的问题你要保留
dist/assets/
(以及里面的所有内容)
dist/manifest.json
dist/index.html
并删除 dist
.
中的所有其他内容
为了测试,我使用了这个 find
命令。
find dist -mindepth 1 -maxdepth 1 \
-type f \( -name manifest.json -o -name index.html -o -print \) -o \
-type d \( -name assets -o -print \)
如果输出列出了应该在 dist
中删除的所有内容,您可以 运行 实际删除数据的版本。
find dist -mindepth 1 -maxdepth 1 \
-type f \( -name manifest.json -o -name index.html -o -print -delete \) -o \
-type d \( -name assets -o -print -exec rm -rf "{}" \; \)
解释:
- 全局选项
-mindepth 1 -maxdepth 1
将搜索限制在 dist
. 正下方的所有文件和目录中
-type f \( actions1 \) -o -type d \( actions2 \)
使用AND(-a
or nothing)和OR(-o
)操作对文件执行actions1
,对目录执行actions2
,使用转义括号是因为 AND 运算的优先级高于 OR。 (如果 dist
中有任何其他对象(例如符号 link、命名管道等),您可能必须添加条件和一些操作来捕获其他情况。
-name manifest.json -o -name index.html -o -print -delete
OR 操作:如果第一个表达式(匹配名称)之一为真,则不执行最后一个(-print -delete
)
-name assets -o -print -exec rm -rf "{}" \;
同上,使用rm -rf
进行递归移除,因为-delete
不会移除非空目录。
备选方案:
- 重命名
dist
- 新建一个
dist
- 将要保留的文件和目录移动到新的
dist
- 递归删除旧的重命名目录。
mv dist dist.tmp && mkdir dist &&
mv dist.tmp/assets dist.tmp/manifest.json dist.tmp/index.html dist &&
rm -rf dist.tmp
我需要删除 dist/
文件夹中除 manifest.json
、index.html
和 assets/
之外的所有内容。它将 运行 作为 package.json
.
我尝试用 find
解决"clean": "find dist ! -name manifest.json -type f -delete | find . -type d -empty -delete"
但我无法让它与多个参数一起工作。
编辑:
所以我想保留:
dist/assets/*
资产中的所有内容。包括像 dist/assets/map/dog.ts
这样的子目录
dist/manifest.json
dist/index.html
并删除其余部分,例如:
dist/pages/
删除目录、子目录和文件
dist/examples/
删除目录、子目录和文件
dist/randomfile.txt
删除文件
dist/hello.js
删除文件
dist/*
等等等等
根据current version的问题你要保留
dist/assets/
(以及里面的所有内容)dist/manifest.json
dist/index.html
并删除 dist
.
为了测试,我使用了这个 find
命令。
find dist -mindepth 1 -maxdepth 1 \
-type f \( -name manifest.json -o -name index.html -o -print \) -o \
-type d \( -name assets -o -print \)
如果输出列出了应该在 dist
中删除的所有内容,您可以 运行 实际删除数据的版本。
find dist -mindepth 1 -maxdepth 1 \
-type f \( -name manifest.json -o -name index.html -o -print -delete \) -o \
-type d \( -name assets -o -print -exec rm -rf "{}" \; \)
解释:
- 全局选项
-mindepth 1 -maxdepth 1
将搜索限制在dist
. 正下方的所有文件和目录中
-type f \( actions1 \) -o -type d \( actions2 \)
使用AND(-a
or nothing)和OR(-o
)操作对文件执行actions1
,对目录执行actions2
,使用转义括号是因为 AND 运算的优先级高于 OR。 (如果dist
中有任何其他对象(例如符号 link、命名管道等),您可能必须添加条件和一些操作来捕获其他情况。-name manifest.json -o -name index.html -o -print -delete
OR 操作:如果第一个表达式(匹配名称)之一为真,则不执行最后一个(-print -delete
)-name assets -o -print -exec rm -rf "{}" \;
同上,使用rm -rf
进行递归移除,因为-delete
不会移除非空目录。
备选方案:
- 重命名
dist
- 新建一个
dist
- 将要保留的文件和目录移动到新的
dist
- 递归删除旧的重命名目录。
mv dist dist.tmp && mkdir dist &&
mv dist.tmp/assets dist.tmp/manifest.json dist.tmp/index.html dist &&
rm -rf dist.tmp