linux - 删除目录中的所有文件,列表中提到的文件除外
问题描述
我有一个名为 a00 的目录,其中包含 3000 个扩展名为 .SAC 的文件。我有一个名为 gd.list 的文本文件,其中包含这 3000 个文件中的 88 个的名称。我正在尝试编写一个代码来删除所有 .SAC 文件,除了 gd.list 中提到的文件
如何使用 shell/bash 做到这一点?
解决方案
该rm
命令已被注释掉,以便您可以检查并验证它是否按需要工作。然后取消注释该行。
该check directory
部分将确保您不会意外地从错误的目录运行脚本并破坏错误的文件。
您可以删除该echo deleting
行以静默运行。
#!/bin/bash
cd /home/me/myfolder2tocleanup/
# Exit if the directory isn't found.
if (($?>0)); then
echo "Can't find work dir... exiting"
exit
fi
for i in *; do
if ! grep -qxFe "$i" filelist.txt; then
echo "Deleting: $i"
# the next line is commented out. Test it. Then uncomment to removed the files
# rm "$i"
fi
done
你可以在这里找到答案https://askubuntu.com/questions/830776/remove-file-but-exclude-all-files-in-a-list by LD James
推荐阅读
- r - 网页抓取需要一段时间才能使用 R、rvest、Rslenium 加载的页面
- html - CSS 左对齐 div 关于中心对齐父 div
- reactjs - TransitionGroup 组件滞后于退出活动
- java-8 - Akka 循环路由器不考虑实例数量
- c# - Read text file from relative path within project
- mysqli - 安装创建核心表失败 | 安装 Revive Adserver 5.2.0
- pentaho - 在 Pentaho 中将批量 .xlsx 文件转换为 .csv (UTF-8)
- salesforce - Salesforce:如果客户或公司名称如此,则自动将潜在客户所有者分配给特定用户的有效方法
- mysql - 一张表用于多个数据库
- python - 在 Python,pandas 中,当我将列从十六进制转换为十进制时,如何忽略 python 中的无效值?