删除目录中包含特定名称的所有文件 [英] remove all files containing a certain name within a directory

查看:43
本文介绍了删除目录中包含特定名称的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下目录,其中包含以下文件:

I have the following directory and it has the following files:

/tmp/test/file1.txt
/tmp/test/file1.txt.backup
/tmp/test/mywords.csv

如何使用 file 模块来删除 file1* 文件?

How do I use the file module to just remove file1* files?

推荐答案

edit: Ansible 2.0 现在已经发布,所以之前的答案应该有效,你现在也可以 遍历文件团.请注意,这仅适用于您在本地运行 Ansible 的情况:

edit: Ansible 2.0 has been released now, so the previous answer should work, and you can also now loop over fileglobs. Note this only works if you are running Ansible locally:

- file:
    path: "{{item}}"
    state: absent
  with_fileglob:
    - /tmp/test/file*

原答案:

我目前无法找到一种方法来做到这一点,而不是放到 shell带有 with_items

I can't find a way to do this currently without dropping to the shell, however if you can drop to the shell to gather a list of files that match the pattern and save that as a variable, then you can loop over the file module with with_items

Ansible 2.0 将包含一个查找"模块,可以为您获取列表:http://docs.ansible.com/ansible/find_module.html

Ansible 2.0 will include a "find" module that would get the list for you: http://docs.ansible.com/ansible/find_module.html

这里有一些应该在 ansible 2.0 中完成的任务,但我没有安装它,所以我没有测试它并且可能错误地访问了 find 模块的结果.

Here's a couple of tasks that should do it in ansible 2.0, but I don't have it installed so I haven't tested it and may be accessing the results of the find module incorrectly.

- find:
    paths: "/tmp/test"
    patterns: "file*"
  register: result

- file:
    path: "{{item.path}}" #correction code
    state: absent
  with_items: result.files

这篇关于删除目录中包含特定名称的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆