通过 cron 移动最旧的文件

joe*_*ert 2 unix filesystems cron

我有两个目录。

/application/inbox
/application/unresponsive
Run Code Online (Sandbox Code Playgroud)

该应用程序在收件箱中查找 *.txt 文件并使用它们。应用程序会定期将这些文件中的条目保存到无响应文件夹中以日期命名 (2009-07-31) 的文件中。

我想设置一个每天运行一次的 cron 作业,将最旧的文件从无响应的邮箱移动到收件箱,添加一个 *.txt 扩展名,以便应用程序接收它。

mar*_*ton 7

未经测试,可能有问题:

#!/bin/sh

# last file in list sorted newest->oldest
OLDEST=$(ls -t /application/unresponsive | tail -1)

# make sure $OLDEST isn't empty string
if [ -n $OLDEST ]; then
    # quote in case of spaces and remove directory name
    mv "$OLDEST" /application/inbox/$(basename "$OLDEST").txt
fi
Run Code Online (Sandbox Code Playgroud)

  • 哦,好接近。$OLDEST 将包含 /application/unresponsive/2009-07-31,因此您的 mv 将尝试将文件保存到 /application/inbox//application/unresponsive/2009-07-31.txt,这无疑会失败。使用 bash 模式匹配运算符应该很容易修复,但我永远不记得它们走哪条路了。 (2认同)
  • 2. 使用 basename 来获取没有目录的基本文件名。例如。'mv "$OLDEST" /application/inbox/$(basename "$OLDEST").txt' (2认同)