![](/img/trans.png)
[英]Create Unix shell script to move non empty files from Source directory to Target directory and add timestamp to them
[英]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.