Unix: How to delete files listed in a file

后端 未结 13 1644
遥遥无期
遥遥无期 2020-12-04 06:31

I have a long text file with list of file masks I want to delete

Example:

/tmp/aaa.jpg
/var/www1/*
/var/www/qwerty.php

I need delet

13条回答
  •  感动是毒
    2020-12-04 07:17

    Use this:

    while IFS= read -r file ; do rm -- "$file" ; done < delete.list
    

    If you need glob expansion you can omit quoting $file:

    IFS=""
    while read -r file ; do rm -- $file ; done < delete.list
    

    But be warned that file names can contain "problematic" content and I would use the unquoted version. Imagine this pattern in the file

    *
    */*
    */*/*
    

    This would delete quite a lot from the current directory! I would encourage you to prepare the delete list in a way that glob patterns aren't required anymore, and then use quoting like in my first example.

提交回复
热议问题