繁体   English   中英

Shell脚本 - 查找今天修改的文件,创建目录并将其移动到那里

[英]Shell script - Find files modified today, create directory, and move them there

我想知道是否有一种简单而简洁的方法来编写一个shell脚本,该脚本将通过一系列目录( ,一个类中的每个学生一个),确定是否在该目录中有任何文件在其中被修改最后一天,只有在这种情况下,脚本才会创建一个子目录并将文件复制到那里。 因此,如果目录在过去24小时内没有修改过任何文件,那么它将保持不变。 我最初的想法是这样的:

#!/bin/sh
cd /path/people/ #this directory has multiple subdirectories

for i in `ls`
do
   if find ./$i -mtime -1  -type f  then 
     mkdir ./$i/updated_files
     #code to copy the files to the newly created directory
   fi
done

但是,这似乎为所有子目录创建/ updated_files,而不仅仅是最近修改过的文件。

更重要的使用find可能会让您的工作变得更轻松。 就像是

find /path/people -mtime -1 -type f -printf "mkdir --parents %h/updated_files\n" | sort | uniq | sh 

问题是您假设如果找不到任何查找命令将失败。 即使找不到匹配项,退出代码也为零(成功)。

就像是

UPDATEDFILES=`find ./$i -mtime -1  -type f`
[ -z "$UPDATEDFILES" ] && continue
mkdir ...
cp ...
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM