Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to list and delete .php, .xml files. For this I am using find command.

To list : find . -type f -name \*.php -print0

To delete : find . -type f -name \*.php -print0 | xargs -0 rm -r

As I run find two times. Is it possible to store result of first command and then reuse in second ?


share|improve this question
up vote 7 down vote accepted
find . -type f -name '*.php' -print0 | tee list | tr \\0 \\n
xargs -r0 rm -f < list

That's assuming you want to see the list before deciding to delete them. If not, you can simply do:

find . -name '*.php' -type f -print -delete

(note that -print0, -delete, -r, -0 are not standard but supported by the GNU implementation)

Also beware that while find -delete is relatively safe, in the first solution, someone could rename directories to symlinks to some sensitive areas between the time you run the find and xargs command and make you delete files that you didn't intend to. Running find twice, the second time with -delete would avoid that.

share|improve this answer
It works. Thanks a lot ! – Novice User Oct 3 '13 at 11:20

Since you need .php and .xml files the find command should look like:

find . \( -name '*.php' -o -name '*.xml' \) -type f -print -exec rm {} +

The + instrucs find to use rm only once per chunk of files. If we had used \; we'd have run one rm command for each file.

share|improve this answer
Ahh...cool way ..actually I have a pretty long script with many files like xml, php, SVN folders etc. For brevity I had mentioned only couple of them. – Novice User Oct 3 '13 at 11:40
Nice fast and short! – holasz Oct 3 '13 at 11:47
Note that -exec rm {} + is a speed up over -exec rm {} \;, but not over -print0 | xargs -r0 rm nor -delete but is less safe than -execdir rm {} \; or -delete because of the race condition mentioned in my answer. – Stéphane Chazelas Oct 3 '13 at 12:02
Well, since you are invoking tee tr and xargs meaning that three different external programs are spawned, the command will not be fast as you think. Also the OP asks for the .xml files to be included which is not covered in your answer. I'm very curious to see a benchmark on the commands above. – val0x00ff Oct 3 '13 at 12:19
I would recommend to first use "echo rm" instead of directly "rm" and only take out the "echo" once one it is sure that it's safe enough ^^. I prefer Stephane's answer for the safety side. – Olivier Dulac Oct 3 '13 at 15:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.