[英]Removing all .csv files except the latest file in Linux
I'm trying to remove the files in a directory called Date_Sources
. 我正在尝试删除名为Date_Sources
的目录中的文件。 This directory has 5 subfolders in it, called Test1
Test2
... Test5
. 此目录中有5个子文件夹,称为Test1
Test2
... Test5
。
I know how to remove the files every 90 days : 我知道如何每90天删除一次文件:
find /home/deployer/Data_Sources/.../.. -mtime +90 -type f -exec rm -r '{}' \;
How can I make sure my script will remove all the files after 90 days EXCEPT the newest file (Per each subdirectory)? 除最新文件 (每个子目录)外,如何确保我的脚本在90天后将删除所有文件?
So the script has to go to Date_Sources/Test1
, Data_sources/Test2
, ..., etc. and make sure all the files are removed after 90 days except the newest one. 因此,脚本必须转到Date_Sources/Test1
, Data_sources/Test2
,...等,并确保除最新文件外,所有文件均在90天后删除。
Something like: 就像是:
find /home/deployer/Data_Sources -maxdepth 1 -mindepth 1 -type d > afile
while IFS= read -r subdir
do
find "$subdir" -mtime +90 -type f -printf "%T+\t%p\n" \
| sort \
| head -n-1 \
| sed 's|[[:blank:]]\+| |' \
| cut -f 2 -d " " > bfile
while IFS= read -r each
do
rm -vf "$each"
done < bfile
done < afile
rm -f bfile afile
Explanation: 说明:
find all directories in /home/deployer/Data_Sources
within the immediate directory ( -maxdepth 1
) and excluding itself ( -mindepth 1
), and dump this to a file called afile
在直接目录( -maxdepth 1
)内并排除自身( -mindepth 1
)的/home/deployer/Data_Sources
找到所有目录,并将其转储到名为afile
find /home/deployer/Data_Sources -maxdepth 1 -mindepth 1 -type d > afile
Read each line from afile
and store in environment variable subdir
(See the done
line of this loop to see that afile
is being piped to the while
loop) 从afile
读取每一行,并将其存储在环境变量subdir
(请参见此循环的done
行,以查看将afile
通过管道传递到while
循环中)
while IFS= read -r subdir
do
Run a find
command on each subdir
, listing only files older than 90 days (and print the timestamp with the file) 在每个subdir
上运行find
命令,仅列出90天以上的文件(并使用该文件打印时间戳)
find "$subdir" -mtime +90 -type f -printf "%T+\t%p\n" \
sort
the output of the find
sort
find
结果进行sort
| sort \
grab all but the last line of the sort
output (the last line is the newest file) 抓住除sort
输出的最后一行以外的所有内容(最后一行是最新文件)
| head -n-1 \
replace all multiple whitespaces to a single space 将所有多个空格替换为一个空格
| sed 's|[[:blank:]]\+| |' \
grab the second column from the output, defining space as delimiter; 从输出中获取第二列,将空格定义为定界符; all dumped to a file called bfile
. 全部都转储到名为bfile
的文件中。 (Could probably use a tab as a delimiter somehow and not use the sed
above, but I wasn't sure how to specify it so it would work; -d "\\t"
certainly didn't do the trick) (可能以某种方式使用制表符作为定界符,而不使用上面的sed
,但我不确定如何指定它,以便它能正常工作; -d "\\t"
当然不能解决问题)
| cut -f 2 -d " " > bfile
Now, read bfile
line-by-line and store in each
environment variable (Again: See the done line to show bfile
being piped in) 现在, bfile
读取bfile
并将其存储在each
环境变量中(再次:查看完成的行以显示bfile
被管道输入)
while IFS= read -r each
do
Actually do the removal of the old files (being verbose and suppressing prompts) 实际上是删除旧文件(冗长且不显示提示)
rm -vf "$each"
done < bfile
done < afile
Remove the temporary files bfile
and afile
删除临时文件bfile
和afile
rm -f bfile afile
Note: 注意:
-Updated to remove use of for
loops to properly handle special characters and spaces in paths; -更新以删除使用for
循环来正确处理路径中的特殊字符和空格; also double quotes on variables (as suggested by @mklement0) 也对变量加双引号(如@ mklement0所建议)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.