使用 bash 脚本删除 5 天前的文件
Delete files that are 5 days old using bash script
我目前使用搜索目录并删除 5 天旧文件的命令。
find /path/to/files* -mtime +5 -exec rm {} \;
我从命令行 运行 它工作正常。但是当我把它放在 .sh
文件中时,它说 find /path/to/files*: No such file or directory
.
shell脚本中只有两行:
#! /usr/bin/env bash
find /path/to/files* -mtime +5 -exec rm {} \;
我如何重写脚本才能使其正常工作?
`
如果当前没有与通配符匹配的文件,则会发生错误,大概是因为 none 在您之前删除它们后已创建。
find
的参数应该是包含文件的目录,而不是文件名本身,因为 find
会自动搜索目录。如果要限制文件名,请使用 -name
选项指定通配符。
如果您不想进入子目录,请使用 -maxdepth
选项。
find /path/to -maxdepth 1 -type f -name 'files*' -mtime +5 -delete
这个有效:
#! /usr/bin/env bash
find /home/ubuntu/directory -type f -name 'filename*' -mtime +4 -delete
这是一个例子:
find /home/ubuntu/processed -type f -name 'output*' -mtime +4 -delete
我目前使用搜索目录并删除 5 天旧文件的命令。
find /path/to/files* -mtime +5 -exec rm {} \;
我从命令行 运行 它工作正常。但是当我把它放在 .sh
文件中时,它说 find /path/to/files*: No such file or directory
.
shell脚本中只有两行:
#! /usr/bin/env bash
find /path/to/files* -mtime +5 -exec rm {} \;
我如何重写脚本才能使其正常工作? `
如果当前没有与通配符匹配的文件,则会发生错误,大概是因为 none 在您之前删除它们后已创建。
find
的参数应该是包含文件的目录,而不是文件名本身,因为 find
会自动搜索目录。如果要限制文件名,请使用 -name
选项指定通配符。
如果您不想进入子目录,请使用 -maxdepth
选项。
find /path/to -maxdepth 1 -type f -name 'files*' -mtime +5 -delete
这个有效:
#! /usr/bin/env bash
find /home/ubuntu/directory -type f -name 'filename*' -mtime +4 -delete
这是一个例子:
find /home/ubuntu/processed -type f -name 'output*' -mtime +4 -delete