简体   繁体   English

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

[英]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.

相关问题 创建 Unix shell 脚本以将非空文件从源目录移动到目标目录并为其添加时间戳 - Create Unix shell script to move non empty files from Source directory to Target directory and add timestamp to them Shell脚本可在某些修改日期之后查找文件,并按修改日期的顺序对其进行排序 - Shell script to find files after certain modified date and sort them in order of modified date Shell Script,将每 2 个文件移动到每个目录 - Shell Script, move each 2 files to each directory Shell脚本查找cmin在目录而不是文件上求值 - Shell script find cmin evaluates on directory, not on files Shell脚本在文件列表中查找单词,所有这些单词均作为参数给出 - shell script to find a word in a list of files, all of them given as parameters 使用shell脚本查找文件并将其移动到适当的目录 - Find and move files to appropriate directories using shell script shell脚本,用于根据目录中的现有文件创建文件 - shell script to create files based on existing files in a directory Shell脚本-将文件移动到文件夹 - Shell script - move files into folder 如何查找今天创建并在 5 分钟之前随时修改的所有文件? - How to find all the files created today and modified anytime before 5 minutes? 如何在 Linux 中查找最近或今天修改过的文件 - How to Find Recent or Today’s Modified Files in Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM