2021年7月23日 星期五

[ Bash 範例代碼 ] Delete old files

 Source From Here

Let's create testing old files:
# touch -d "Thu, 1 March 2018 12:30:00" a
...

# find ./ -mtime +90 -exec ls -l {} \;
-rw-r--r-- 1 root root 0 Mar 1 2018 ./c
-rw-r--r-- 1 root root 0 Mar 1 2018 ./b
-rw-r--r-- 1 root root 0 Mar 1 2018 ./a

03-1_delete_old_files.sh
  1. #!/bin/bash  
  2. # Author: John Lee  
  3. # Date: 2021/07/24  
  4. # Description: This script will delete files older than 90 days  
  5. # Modified: 2021/07/24  
  6. #  
  7. # More:  
  8. #   Use below command to create old file for testing  
  9. #   touch -d "Thu, 1 March 2018 12:30:00" a  
  10. TARGET_PATH=${TARGET_PATH:-`pwd`}  
  11. for of in `find ${TARGET_PATH} -mtime +90`  
  12. do  
  13.   fname=$(basename $of)  
  14.   # mv $of "/tmp/${fname}" <--- testing  
  15.   rm $of "/tmp/${fname}"  
  16.   if [ $? -eq 0 ]; then  
  17.     echo "Deleted ${fname} done!"  
  18.   else  
  19.     echo "Fail to delete ${fname}!"  
  20.   fi  
  21. done  
Execution outpupt:
# ./03-1_delete_old_files.sh
Deleted c done!
Deleted b done!
Deleted a done!


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...