Delete a list of files with find and grep

后端 未结 9 1666
一个人的身影
一个人的身影 2021-01-30 08:44

I want to delete all files which have names containing a specific word, e.g. \"car\". So far, I came up with this:

find|grep car

How do I pass

相关标签:
9条回答
  • 2021-01-30 09:11
    find . -name '*car*' -exec rm -f {} \;
    

    or pass the output of your pipeline to xargs:

    find | grep car | xargs rm -f
    

    Note that these are very blunt tools, and you are likely to remove files that you did not intend to remove. Also, no effort is made here to deal with files that contain characters such as whitespace (including newlines) or leading dashes. Be warned.

    0 讨论(0)
  • 2021-01-30 09:13

    when find | grep car | xargs rm -f get results:

    /path/to/car  
    /path/to/car copy  
    

    some files which contain whitespace will not be removed.

    So my answer is:

    find | grep car | while read -r line ; do 
      rm -rf "${line}"
    done
    

    So the file contains whitespace could be removed.

    0 讨论(0)
  • 2021-01-30 09:15

    This finds a file with matching pattern (*.xml) and greps its contents for matching string (exclude="1") and deletes that file if a match is found.

    find . -type f -name "*.xml" -exec grep exclude=\"1\" {} \; -exec rm {} \;
    
    0 讨论(0)
提交回复
热议问题