remove all of a file type from a directory and its

2019-03-10 18:26发布

I was under impression that

rm -r *.xml

would remove all file from parent and child however:

*.xml: No such file or directory

6条回答
对你真心纯属浪费
2楼-- · 2019-03-10 18:55

I'm assuming you want to remove all *.xml files recursively (within current and all sub directories). To do that, use find:

find . -name "*.xml" -exec rm {} \;

On a side note, recursive deletion scares me. On my saner days, I tend to precede that step with:

find . -name "*.xml" 

(without the -exec bit) just to see what might get deleted before taking the leap. I advice you do the same. Your files will thank you.

查看更多
干净又极端
3楼-- · 2019-03-10 18:59

ZSH recursive globbing to the rescue!

Invoke zsh: zsh

Be sure you're in the dir you intend to be in: cd wherever

List first: ls **/*.xml

Remove: rm **/*.xml

I'll resist the strong temptation to bash on bash, and just point to the relevant zsh docs on the topic here.

查看更多
一夜七次
4楼-- · 2019-03-10 19:06

An easy way to do is

rm -f *.xml

This will remove all .xml files from current directory.

查看更多
萌系小妹纸
5楼-- · 2019-03-10 19:10

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
查看更多
Explosion°爆炸
6楼-- · 2019-03-10 19:12

more beautiful way, although this one is less supported in unix systems:

rm -rf */*.xml

this will remove xml files from all sub-directories of you current directory.

查看更多
We Are One
7楼-- · 2019-03-10 19:13

The man page of rm says:

 -r, -R, --recursive
          remove directories and their contents recursively

This means the flag -r is expecting a directory. But *.xml is not a directory.

If you want to remove the all .xml files from current directory recursively below is the command:

find . -name "*.xml" -type f|xargs rm -f
查看更多
登录 后发表回答