简体   繁体   English

在Linux中删除* .txt文件之前如何计算和添加到文件?

[英]how to count and add to file before delete *.txt files in linux?

I have a few *.txt files in a folder. 我的文件夹中有几个* .txt文件。 I want to count and add to deleted_files_count.log file before delete files by using this command: 我想在使用以下命令删除文件之前先计数并添加到Deleted_files_count.log文件:

find ./*.txt -type f -mmin +10 -exec rm {} \;

For example folder has 3 *.txt files and content of deleted_files_count.log file is 5, content of deleted_files_count.log should be 8 after delete. 例如,文件夹中有3个* .txt文件,而delete_files_count.log文件的内容为5,则删除后delete_files_count.log文件的内容应为8。 Can anybody help me? 有谁能够帮助我? Thanks. 谢谢。

You can try this: 您可以尝试以下方法:

cnt=`cat deleted_files_count.log` ; for i in `find ./*.txt -type f -mmin +10` ; do rm "$i" ; cnt=$(($cnt+1)) ; done ; echo $cnt > deleted_files_count.log 

This doesn't check the content of the log file nor if it exists at all. 这不会检查日志文件的内容,也不会检查它的存在。 If you want to properly handle possible errors, you will have to improve it a bit, but as long as you know that deleted_files_count.log exists and contains a number, it will do the trick. 如果要正确处理可能的错误,则必须对其进行一些改进,但是只要知道Deleted_files_count.log存在并且包含数字,它就可以解决问题。

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

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