[英]Shell script - Find files modified today, create directory, and move them there
I was wondering if there is a simple and concise way of writing a shell script that would go through a series of directories, ( ie , one for each student in a class), determine if within that directory there are any files that were modified within the last day, and only in that case the script would create a subdirectory and copy the files there. 我想知道是否有一种简单而简洁的方法来编写一个shell脚本,该脚本将通过一系列目录( 即 ,一个类中的每个学生一个),确定是否在该目录中有任何文件在其中被修改最后一天,只有在这种情况下,脚本才会创建一个子目录并将文件复制到那里。 So if the directory had no files modified in the last 24h, it would remain untouched. 因此,如果目录在过去24小时内没有修改过任何文件,那么它将保持不变。 My initial thought was this: 我最初的想法是这样的:
#!/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
However, that seems to create /updated_files for all subdirectories, not just the ones that have recently modified files. 但是,这似乎为所有子目录创建/ updated_files,而不仅仅是最近修改过的文件。
Heavier use of find
will probably make your job much easier. 更重要的使用find
可能会让您的工作变得更轻松。 Something like 就像是
find /path/people -mtime -1 -type f -printf "mkdir --parents %h/updated_files\n" | sort | uniq | sh
The problem is that you are assuming the find command will fail if it finds nothing. 问题是您假设如果找不到任何查找命令将失败。 The exit code is zero (success) even if it finds nothing that matches. 即使找不到匹配项,退出代码也为零(成功)。
Something like 就像是
UPDATEDFILES=`find ./$i -mtime -1 -type f`
[ -z "$UPDATEDFILES" ] && continue
mkdir ...
cp ...
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.