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

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

问题描述

我有以下目录,并且有以下

I have the following directory and it has the following

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

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

推荐答案

Ansible 2.0现在已经发布,因此前面的答案应该可行,您现在也可以

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中,但是,如果可以拖放到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天全站免登陆