remove all of a file type from a directory and its children

后端 未结 6 1712
南笙
南笙 2021-02-01 15:36

I was under impression that

rm -r *.xml

would remove all file from parent and child however:

*.xml: No such file or directory
         


        
6条回答
  •  梦谈多话
    2021-02-01 15:56

    Reading this answer on finding empty directories unix, I just learned about the -delete action:

    -delete
              Delete  files; true if removal succeeded.  If the removal failed, an error message is issued.  If -delete fails, find's exit status will be nonzero (when it even‐
              tually exits).  Use of -delete automatically turns on the -depth option.
    
              Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the start‐
              ing  points  you  specified.   When  testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid
              later surprises.  Because -delete implies -depth, you cannot usefully use -prune and -delete together.
    

    Source: man find

    That means, you can also delete all xml-files recursively like this:

    find . -name "*.xml" -type f -delete
    

提交回复
热议问题