33

I was under impression that

rm -r *.xml

would remove all file from parent and child however:

*.xml: No such file or directory
keyser
  • 18,829
  • 16
  • 59
  • 101
Will
  • 8,246
  • 16
  • 60
  • 92

6 Answers6

53

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
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 4
    No need to invoke xargs; use `find -exec`. – paulmelnikow Aug 17 '11 at 16:58
  • 1
    @noa..i know...There are numerous ways to do a similar thing in unix.Its up to the OP to chose which one to use.I had just given what i regularly use. – Vijay Aug 17 '11 at 17:01
  • 6
    @noa Actually `xargs` is more efficient. `-exec` calls `rm` for every matching file, whereas `xargs` does it in batches. This can make a big difference when there are lots of files. – suvayu Aug 17 '11 at 19:33
  • Interesting. That makes sense. – paulmelnikow Aug 17 '11 at 23:31
  • `xargs` can be more efficient for *expensive* operations if you use the `-P` options to run tasks in parallel, but probably won't make much difference in this case. However, I agree with @Rahul that it's up to users what they prefer, and one can argue that the syntax for `find...|xargs...` is easier to remember than `find .. -exec ... {} \;`. (hence +1) – Shawn Chin Aug 19 '11 at 09:21
30

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.

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
8

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
Community
  • 1
  • 1
madc
  • 1,674
  • 2
  • 26
  • 41
  • 1
    This should be the top voted answer, much easier than piping to xargs – skd Oct 28 '19 at 16:15
  • @skd nothing is more easier than `rm -rf */*.xml` for me, so I'd pick my own answer, but it's not allowed :D – holms Oct 03 '21 at 00:49
4

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.

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
holms
  • 9,112
  • 14
  • 65
  • 95
2

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.

mattmc3
  • 17,595
  • 7
  • 83
  • 103
-1

An easy way to do is

rm -f *.xml

This will remove all .xml files from current directory.

Daga
  • 334
  • 2
  • 14