我有一些问题似乎无法克服。我有一个包含很多文件夹的文件夹。我想删除所有早于三天的文件夹,但是我至少要保留10个文件夹。
我提出了这个半工作的代码,我想提出一些建议来解决这个问题。
---
- hosts: all
tasks:
# find all files that are older than three
- find:
paths: "/Users/asteen/Downloads/sites/"
age: "3d"
file_type: directory
register: dirsOlderThan3d
# find all files that are in the directory
- find:
paths: "/Users/asteen/Downloads/sites/"
file_type: directory
register: allDirs
# delete all files that are older than three days, but keep a minimum of 10 files
- file:
path: "{{ item.path }}"
state: absent
with_items: "{{ dirsOlderThan3d.files }}"
when: allDirs.files > 10 and not item[0].exists ... item[9].exists
Run Code Online (Sandbox Code Playgroud)
您只需要过滤3天以上的文件列表:
---
- hosts: all
tasks:
- name: find all files that are older than three
find:
paths: "/Users/asteen/Downloads/sites/"
age: "3d"
file_type: directory
register: dirsOlderThan3d
- name: remove older than 3 days but first ten newest
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ (dirsOlderThan3d.files | sort(attribute='ctime'))[10:] | list }}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3224 次 |
| 最近记录: |