简体   繁体   English

在保存到文件的目录列表中查找

[英]find in list of directories saved to a file

I am using a small Bash script to periodically purge old backups: 我正在使用一个小的Bash脚本定期清除旧备份:

FILE_LIST=`mktemp --suffix=.list`
find . -maxdepth 1 -type d -mtime +30 -print0 > $FILE_LIST
xargs --no-run-if-empty --null --arg-file $FILE_LIST rm -rvf
rm -f $FILE_LIST

The problem, however, is that files are not guaranteed to be user-writable, so rm may fail to delete them. 但是,问题是不能保证用户可以写文件,因此rm可能无法删除它们。 I need therefore to run chmod before rm in order to change its permissions: 因此,我需要在rm之前运行chmod才能更改其权限:

xargs --no-run-if-empty --null --arg-file $FILE_LIST chmod -vR 700

But this is inefficient, as it calls chmod on all the files in the found directories, when the vast majority of them probably don not need to be chmoded. 但这是低效的,因为它会在找到的目录中的所有文件上都调用chmod时,大多数情况下可能不需要chmod。 Ideally, I would want to call find again, this time with ! -perm -u+w 理想情况下,这次我要再次致电find ! -perm -u+w ! -perm -u+w and searching only in the directories contained in $FILE_LIST . ! -perm -u+w并仅在$FILE_LIST包含的目录中搜索。

What would be the best way to achieve this? 实现这一目标的最佳方法是什么? I am basically looking for something to replicate the functionality of xargs 's --arg-file option. 我基本上是在寻找一些东西来复制xargs--arg-file选项的功能。

find . -maxdepth 1 -type d -mtime +30 -writable -exec rm -rf {} \;

and then 接着

find . -maxdepth 1 -type d -mtime +30 -exec chmod u+w {} \; -exec rm -rf {} \;

I hope this solves the purpose 我希望这可以解决目的

There is a way to do it — we can chain xargs - find - xargs - chmod : 有一种方法可以做到—我们可以链接xargs find xargs - chmod

# Find unwritable files and chmod them
xargs -I {} -0r -a $FILE_LIST find {} ! -perm -u+w -print0 | xargs -0r chmod -R 700 -v

The full Bash script, thus, would be: 因此,完整的Bash脚本为:

FILE_LIST=`mktemp --suffix=.list`
find ~/udit65_backup/ -maxdepth 1 -iname "$PREFIX-*" -type d -mtime +$NDAYS -print0 > $FILE_LIST
# Make unwritable files... writable
xargs -I {} -0r -a $FILE_LIST find {} ! -perm -u+w -print0 | xargs -0r chmod -R 700 -v
xargs -0r -a $FILE_LIST rm -rf -v
rm -f $FILE_LIST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM