linux - 循环的 bash 脚本

标签 linux bash

我试图基本上查看一个目录,该目录具有每个用户的子目录。说 user1 和 user2 等等。我想基本上遍历每个目录并查找早于特定日期的文件并将其记录下来。我可以使用 ls -d/directory/*/获取目录列表。我知道我可以使用 find ./-type f -mtime +30 来查找超过 30 天未修改的文件。我无法弄清楚如何使用 find 所以每次通过它都会看起来像 find ./user1 -type f -mtime30 然后 ./user2 等等。

#!/bin/bash
LIST="$(ls -d /Volumes/db_backups/*/)"
for i in "$LIST";do
        fold=`basename $i`
        echo $fold
        ModList=$(find $i -type f -mtime +30 >$fold.out )
        $ModList
done

更新:我能够获得文件列表。但是,我不想循环遍历特定的子目录并向每个用户发送电子邮件。例如,我有一个文件被记录为/dir1/dir2/user1/file.txt。我将如何获取 user1 列表并将其放入 user1.out 然后放入 user2.out 等等?

最佳答案

您可以提供到 find 的多个路径:

find directory/* -type f ...

此处 directory/* 将扩展为 directory/user1 directory/user2,实际上为 find 提供了多个路径。

编辑再想一想,您应该可以只使用find directory -type f(帽子提示@anubhava)。

关于linux - 循环的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20999967/

相关文章:

c - 防止 OpenGL 缓冲帧

linux - 命令替换适用于命令行但不适用于脚本

c - 我的信号处理程序示例不工作

regex - 如何对 "s3cmd ls"的输出进行排序

linux - Jenkins:在 bash 脚本中插入特殊字符

linux - 如何使用 sed 在 FIRST 和 LAST 匹配模式之前插入一行

linux - 安装 Arch 能否阻止我的笔记本电脑卡在启动屏幕上?

android -/proc/PID/exe 符号链接(symbolic link)的替代方案,用于通过 PID 检索另一个进程的完整路径

Git 日志表格格式

Bash 的 cat、while、let 和 pipe 导致奇怪的作用域行为