我有两个目录。
/application/inbox /application/unresponsive
该应用程序在收件箱中查找* .txt文件并使用它们。 应用程序将定期将这些文件中的条目保存到无响应文件夹中的date命名(2009-07-31)文件中。
我想设置一个每天工作一次的cron作业,将最旧的文件从无响应的文件夹中移动到收件箱中,添加一个* .txt扩展名,以便应用程序将其拾取。
未经testing,可能是越野车:
#!/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
如果你希望它与空间(更健壮)的文件。 您应该循环使用基本的最小/最大algorithm的-nt(基于修改时间的最新)或-ot(最早)的比较运算符。 这是一个很棒的BashFAQ的例子:
files=(*) newest=${f[0]} for f in "${files[@]}"; do if [[ $f -nt $newest ]]; then newest=$f fi done
所以你的例子会(未经testing):
files=(/application/inbox/*) oldest=${f[0]} for f in "${files[@]}"; do if [[ $f -ot $oldest ]]; then oldest=$f fi mv "$oldest" /application/unresponsive/ done
学习如何不使用ls的输出更好,我想。 这里是链接到论点为什么。 但我留给你决定是否是真的或只是伪unix-古鲁废话:-)