Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing files with rm using find and xargs

Tags:

linux

xargs

rm

When I do

rm file.txt

or

rm *.txt

I'm prompted for each file, since I didn't specify the -f option to rm.

But when I do this:

find . -type f -name '*.txt' | xargs rm

the files are removed without the confirmation.

What is the logics behind this? Is it possible to find the reason in some documentation? I cannot explain why this would be the case.

like image 906
Martin G Avatar asked Mar 16 '17 07:03

Martin G


People also ask

How do I delete files with xargs?

If you want to use xargs command to delete these files just pipe it to xargs command with rm function as its argument. In the above case, xargs command will construct separate rm statements for each file name passed to it by the result of find command. That's it.


1 Answers

You have an alias set for the rm command to 'rm -i'. Therefore if you invoke the command directly as in

rm file.txt

or

rm *.txt

the alias will be expanded. If you will call it with xargs as in

find . -type f -name '*.txt' | xargs rm

The rm is passed as a simple string argument to xargs and is later invoked by xargs without alias substitution of the shell. You alias is probably defined in ~/.bashrc, in case you want to remove it.

like image 146
Harald Gliebe Avatar answered Sep 19 '22 05:09

Harald Gliebe