Bash-在多个目录上执行相同的命令

时间:2021-07-12 22:09:14

I want to create a script that will delete any files older than 7 days on a specified list of directories, but wondering what would be the best way to go about it...

我想创建一个脚本,删除指定目录列表中超过7天的任何文件,但想知道最好的方法是什么...

I want to perform the following command on all directories specified:

我想在指定的所有目录上执行以下命令:

find DIRECTORY_PATH -type f -mtime +7 -exec rm {} \;

Maybe an array holding a list of directories, and loop through each element in the array performing the find command on that?

也许是一个包含目录列表的数组,并循环遍历数组中执行find命令的每个元素?

Any help/advice would be appreciated.

任何帮助/建议将不胜感激。

1 个解决方案

#1


1  

You can directly store all the directories in a file, say dirs.txt and loop through it:

您可以直接将所有目录存储在文件中,例如dirs.txt并循环遍历它:

while read dir
do
  find "$dir" -type f -mtime +7 -exec rm {} \;
done < dirs.txt

#1


1  

You can directly store all the directories in a file, say dirs.txt and loop through it:

您可以直接将所有目录存储在文件中,例如dirs.txt并循环遍历它:

while read dir
do
  find "$dir" -type f -mtime +7 -exec rm {} \;
done < dirs.txt